API modules do not need to setup dependency injection.
Move the implementation to the impl modules.
This commit is contained in:
committed by
Benoit Marty
parent
9eb66afd87
commit
b3ec256dfa
@@ -1,5 +1,3 @@
|
||||
import extension.setupDependencyInjection
|
||||
|
||||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
* Copyright 2022-2024 New Vector Ltd.
|
||||
@@ -16,8 +14,6 @@ android {
|
||||
namespace = "io.element.android.services.appnavstate.api"
|
||||
}
|
||||
|
||||
setupDependencyInjection()
|
||||
|
||||
dependencies {
|
||||
implementation(libs.coroutines.core)
|
||||
implementation(libs.androidx.lifecycle.runtime)
|
||||
|
||||
@@ -8,63 +8,36 @@
|
||||
|
||||
package io.element.android.services.appnavstate.api
|
||||
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.Inject
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.matrix.api.room.JoinedRoom
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
/**
|
||||
* Holds the active rooms for a given session so they can be reused instead of instantiating new ones.
|
||||
*/
|
||||
@SingleIn(AppScope::class)
|
||||
@Inject
|
||||
class ActiveRoomsHolder {
|
||||
private val rooms = ConcurrentHashMap<SessionId, MutableSet<JoinedRoom>>()
|
||||
|
||||
interface ActiveRoomsHolder {
|
||||
/**
|
||||
* Adds a new held room for the given sessionId.
|
||||
*/
|
||||
fun addRoom(room: JoinedRoom) {
|
||||
val roomsForSessionId = rooms.getOrPut(key = room.sessionId, defaultValue = { mutableSetOf() })
|
||||
if (roomsForSessionId.none { it.roomId == room.roomId }) {
|
||||
// We don't want to add the same room multiple times
|
||||
roomsForSessionId.add(room)
|
||||
}
|
||||
}
|
||||
fun addRoom(room: JoinedRoom)
|
||||
|
||||
/**
|
||||
* Returns the last room added for the given [sessionId] or null if no room was added.
|
||||
*/
|
||||
fun getActiveRoom(sessionId: SessionId): JoinedRoom? {
|
||||
return rooms[sessionId]?.lastOrNull()
|
||||
}
|
||||
fun getActiveRoom(sessionId: SessionId): JoinedRoom?
|
||||
|
||||
/**
|
||||
* Returns an active room associated to the given [sessionId], with the given [roomId], or null if none match.
|
||||
*/
|
||||
fun getActiveRoomMatching(sessionId: SessionId, roomId: RoomId): JoinedRoom? {
|
||||
return rooms[sessionId]?.find { it.roomId == roomId }
|
||||
}
|
||||
fun getActiveRoomMatching(sessionId: SessionId, roomId: RoomId): JoinedRoom?
|
||||
|
||||
/**
|
||||
* Removes any room matching the provided [sessionId] and [roomId].
|
||||
*/
|
||||
fun removeRoom(sessionId: SessionId, roomId: RoomId) {
|
||||
val roomsForSessionId = rooms[sessionId] ?: return
|
||||
roomsForSessionId.removeIf { it.roomId == roomId }
|
||||
}
|
||||
fun removeRoom(sessionId: SessionId, roomId: RoomId)
|
||||
|
||||
/**
|
||||
* Clears all the rooms for the given sessionId.
|
||||
*/
|
||||
fun clear(sessionId: SessionId) {
|
||||
val activeRooms = rooms.remove(sessionId) ?: return
|
||||
for (room in activeRooms) {
|
||||
// Destroy the room to reset the live timelines
|
||||
room.destroy()
|
||||
}
|
||||
}
|
||||
fun clear(sessionId: SessionId)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
* Copyright 2025 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.services.appnavstate.impl
|
||||
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.ContributesBinding
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
import io.element.android.libraries.matrix.api.core.RoomId
|
||||
import io.element.android.libraries.matrix.api.core.SessionId
|
||||
import io.element.android.libraries.matrix.api.room.JoinedRoom
|
||||
import io.element.android.services.appnavstate.api.ActiveRoomsHolder
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
@SingleIn(AppScope::class)
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultActiveRoomsHolder : ActiveRoomsHolder {
|
||||
private val rooms = ConcurrentHashMap<SessionId, MutableSet<JoinedRoom>>()
|
||||
|
||||
override fun addRoom(room: JoinedRoom) {
|
||||
val roomsForSessionId = rooms.getOrPut(key = room.sessionId, defaultValue = { mutableSetOf() })
|
||||
if (roomsForSessionId.none { it.roomId == room.roomId }) {
|
||||
// We don't want to add the same room multiple times
|
||||
roomsForSessionId.add(room)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getActiveRoom(sessionId: SessionId): JoinedRoom? {
|
||||
return rooms[sessionId]?.lastOrNull()
|
||||
}
|
||||
|
||||
override fun getActiveRoomMatching(sessionId: SessionId, roomId: RoomId): JoinedRoom? {
|
||||
return rooms[sessionId]?.find { it.roomId == roomId }
|
||||
}
|
||||
|
||||
override fun removeRoom(sessionId: SessionId, roomId: RoomId) {
|
||||
val roomsForSessionId = rooms[sessionId] ?: return
|
||||
roomsForSessionId.removeIf { it.roomId == roomId }
|
||||
}
|
||||
|
||||
override fun clear(sessionId: SessionId) {
|
||||
val activeRooms = rooms.remove(sessionId) ?: return
|
||||
for (room in activeRooms) {
|
||||
// Destroy the room to reset the live timelines
|
||||
room.destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user