|
16 | 16 |
|
17 | 17 | package im.vector.app
|
18 | 18 |
|
19 |
| -import androidx.lifecycle.DefaultLifecycleObserver |
20 | 19 | import androidx.lifecycle.LifecycleOwner
|
21 | 20 | import arrow.core.Option
|
22 | 21 | import im.vector.app.core.di.ActiveSessionHolder
|
@@ -46,54 +45,60 @@ import javax.inject.Singleton
|
46 | 45 |
|
47 | 46 | /**
|
48 | 47 | * This class handles the global app state.
|
49 |
| - * It requires to be added to ProcessLifecycleOwner.get().lifecycle |
| 48 | + * It is required that this class is added as an observer to ProcessLifecycleOwner.get().lifecycle in [VectorApplication] |
50 | 49 | */
|
51 |
| -// TODO Keep this class for now, will maybe be used fro Space |
52 | 50 | @Singleton
|
53 |
| -class AppStateHandler @Inject constructor( |
| 51 | +class SpaceStateHandlerImpl @Inject constructor( |
54 | 52 | private val sessionDataSource: ActiveSessionDataSource,
|
55 | 53 | private val uiStateRepository: UiStateRepository,
|
56 | 54 | private val activeSessionHolder: ActiveSessionHolder,
|
57 | 55 | private val analyticsTracker: AnalyticsTracker
|
58 |
| -) : DefaultLifecycleObserver { |
| 56 | +) : SpaceStateHandler { |
59 | 57 |
|
60 | 58 | private val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
|
61 | 59 | private val selectedSpaceDataSource = BehaviorDataSource<Option<RoomSummary>>(Option.empty())
|
62 |
| - |
63 |
| - val selectedSpaceFlow = selectedSpaceDataSource.stream() |
64 |
| - |
| 60 | + private val selectedSpaceFlow = selectedSpaceDataSource.stream() |
65 | 61 | private val spaceBackstack = ArrayDeque<String?>()
|
66 | 62 |
|
67 |
| - fun getCurrentSpace(): RoomSummary? { |
| 63 | + override fun getCurrentSpace(): RoomSummary? { |
68 | 64 | return selectedSpaceDataSource.currentValue?.orNull()?.let { spaceSummary ->
|
69 | 65 | activeSessionHolder.getSafeActiveSession()?.roomService()?.getRoomSummary(spaceSummary.roomId)
|
70 | 66 | }
|
71 | 67 | }
|
72 | 68 |
|
73 |
| - fun setCurrentSpace(spaceId: String?, session: Session? = null, persistNow: Boolean = false, isForwardNavigation: Boolean = true) { |
| 69 | + override fun setCurrentSpace( |
| 70 | + spaceId: String?, |
| 71 | + session: Session?, |
| 72 | + persistNow: Boolean, |
| 73 | + isForwardNavigation: Boolean, |
| 74 | + ) { |
| 75 | + val activeSession = session ?: activeSessionHolder.getSafeActiveSession() ?: return |
74 | 76 | val currentSpace = selectedSpaceDataSource.currentValue?.orNull()
|
75 |
| - val uSession = session ?: activeSessionHolder.getSafeActiveSession() ?: return |
76 |
| - if (currentSpace != null && spaceId == currentSpace.roomId) return |
77 |
| - val spaceSum = spaceId?.let { uSession.getRoomSummary(spaceId) } |
| 77 | + val spaceSummary = spaceId?.let { activeSession.getRoomSummary(spaceId) } |
| 78 | + val sameSpaceSelected = currentSpace != null && spaceId == currentSpace.roomId |
| 79 | + |
| 80 | + if (sameSpaceSelected) { |
| 81 | + return |
| 82 | + } |
78 | 83 |
|
79 | 84 | if (isForwardNavigation) {
|
80 | 85 | spaceBackstack.addLast(currentSpace?.roomId)
|
81 | 86 | }
|
82 | 87 |
|
83 | 88 | if (persistNow) {
|
84 |
| - uiStateRepository.storeSelectedSpace(spaceSum?.roomId, uSession.sessionId) |
| 89 | + uiStateRepository.storeSelectedSpace(spaceSummary?.roomId, activeSession.sessionId) |
85 | 90 | }
|
86 | 91 |
|
87 |
| - if (spaceSum == null) { |
| 92 | + if (spaceSummary == null) { |
88 | 93 | selectedSpaceDataSource.post(Option.empty())
|
89 | 94 | } else {
|
90 |
| - selectedSpaceDataSource.post(Option.just(spaceSum)) |
| 95 | + selectedSpaceDataSource.post(Option.just(spaceSummary)) |
91 | 96 | }
|
92 | 97 |
|
93 | 98 | if (spaceId != null) {
|
94 |
| - uSession.coroutineScope.launch(Dispatchers.IO) { |
| 99 | + activeSession.coroutineScope.launch(Dispatchers.IO) { |
95 | 100 | tryOrNull {
|
96 |
| - uSession.getRoom(spaceId)?.membershipService()?.loadRoomMembersIfNeeded() |
| 101 | + activeSession.getRoom(spaceId)?.membershipService()?.loadRoomMembersIfNeeded() |
97 | 102 | }
|
98 | 103 | }
|
99 | 104 | }
|
@@ -122,9 +127,11 @@ class AppStateHandler @Inject constructor(
|
122 | 127 | }.launchIn(session.coroutineScope)
|
123 | 128 | }
|
124 | 129 |
|
125 |
| - fun getSpaceBackstack() = spaceBackstack |
| 130 | + override fun getSpaceBackstack() = spaceBackstack |
| 131 | + |
| 132 | + override fun getSelectedSpaceFlow() = selectedSpaceFlow |
126 | 133 |
|
127 |
| - fun safeActiveSpaceId(): String? { |
| 134 | + override fun getSafeActiveSpaceId(): String? { |
128 | 135 | return selectedSpaceDataSource.currentValue?.orNull()?.roomId
|
129 | 136 | }
|
130 | 137 |
|
|
0 commit comments