-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from ouchadam/tech/profile-reducer
Porting ProfileViewModel to reducer
- Loading branch information
Showing
16 changed files
with
358 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
features/profile/src/main/kotlin/app/dapk/st/profile/ProfileModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
package app.dapk.st.profile | ||
|
||
import app.dapk.st.core.JobBag | ||
import app.dapk.st.core.ProvidableModule | ||
import app.dapk.st.core.createStateViewModel | ||
import app.dapk.st.core.extensions.ErrorTracker | ||
import app.dapk.st.engine.ChatEngine | ||
import app.dapk.st.profile.state.ProfileState | ||
import app.dapk.st.profile.state.ProfileUseCase | ||
import app.dapk.st.profile.state.profileReducer | ||
|
||
class ProfileModule( | ||
private val chatEngine: ChatEngine, | ||
private val errorTracker: ErrorTracker, | ||
) : ProvidableModule { | ||
|
||
fun profileViewModel(): ProfileViewModel { | ||
return ProfileViewModel(chatEngine, errorTracker) | ||
fun profileState(): ProfileState { | ||
return createStateViewModel { profileReducer(chatEngine, errorTracker, ProfileUseCase(chatEngine, errorTracker), JobBag()) } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,30 +20,34 @@ import androidx.compose.ui.unit.dp | |
import app.dapk.st.core.Lce | ||
import app.dapk.st.core.LifecycleEffect | ||
import app.dapk.st.core.components.CenteredLoading | ||
import app.dapk.st.core.page.PageAction | ||
import app.dapk.st.design.components.* | ||
import app.dapk.st.engine.RoomInvite | ||
import app.dapk.st.engine.RoomInvite.InviteMeta | ||
import app.dapk.st.profile.state.Page | ||
import app.dapk.st.profile.state.ProfileAction | ||
import app.dapk.st.profile.state.ProfileState | ||
import app.dapk.st.settings.SettingsActivity | ||
|
||
@Composable | ||
fun ProfileScreen(viewModel: ProfileViewModel, onTopLevelBack: () -> Unit) { | ||
fun ProfileScreen(viewModel: ProfileState, onTopLevelBack: () -> Unit) { | ||
viewModel.ObserveEvents() | ||
|
||
LifecycleEffect( | ||
onStart = { viewModel.start() }, | ||
onStop = { viewModel.stop() } | ||
onStart = { viewModel.dispatch(ProfileAction.ComponentLifecycle.Visible) }, | ||
onStop = { viewModel.dispatch(ProfileAction.ComponentLifecycle.Gone) } | ||
) | ||
|
||
val context = LocalContext.current | ||
|
||
val onNavigate: (SpiderPage<out Page>?) -> Unit = { | ||
when (it) { | ||
null -> onTopLevelBack() | ||
else -> viewModel.goTo(it) | ||
else -> viewModel.dispatch(PageAction.GoTo(it)) | ||
} | ||
} | ||
|
||
Spider(currentPage = viewModel.state.page, onNavigate = onNavigate) { | ||
Spider(currentPage = viewModel.current.state1.page, onNavigate = onNavigate) { | ||
item(Page.Routes.profile) { | ||
ProfilePage(context, viewModel, it) | ||
} | ||
|
@@ -54,7 +58,7 @@ fun ProfileScreen(viewModel: ProfileViewModel, onTopLevelBack: () -> Unit) { | |
} | ||
|
||
@Composable | ||
private fun ProfilePage(context: Context, viewModel: ProfileViewModel, profile: Page.Profile) { | ||
private fun ProfilePage(context: Context, viewModel: ProfileState, profile: Page.Profile) { | ||
Box( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
|
@@ -67,7 +71,7 @@ private fun ProfilePage(context: Context, viewModel: ProfileViewModel, profile: | |
|
||
when (val state = profile.content) { | ||
is Lce.Loading -> CenteredLoading() | ||
is Lce.Error -> GenericError { viewModel.start() } | ||
is Lce.Error -> GenericError { viewModel.dispatch(ProfileAction.ComponentLifecycle.Visible) } | ||
is Lce.Content -> { | ||
val configuration = LocalConfiguration.current | ||
val content = state.value | ||
|
@@ -111,15 +115,15 @@ private fun ProfilePage(context: Context, viewModel: ProfileViewModel, profile: | |
TextRow( | ||
title = "Invitations", | ||
content = "${content.invitationsCount} pending", | ||
onClick = { viewModel.goToInvitations() } | ||
onClick = { viewModel.dispatch(ProfileAction.GoToInvitations) } | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun SpiderItemScope.Invitations(viewModel: ProfileViewModel, invitations: Page.Invitations) { | ||
private fun SpiderItemScope.Invitations(viewModel: ProfileState, invitations: Page.Invitations) { | ||
when (val state = invitations.content) { | ||
is Lce.Loading -> CenteredLoading() | ||
is Lce.Content -> { | ||
|
@@ -133,11 +137,11 @@ private fun SpiderItemScope.Invitations(viewModel: ProfileViewModel, invitations | |
TextRow(title = text, includeDivider = false) { | ||
Spacer(modifier = Modifier.height(4.dp)) | ||
Row { | ||
Button(modifier = Modifier.weight(1f), onClick = { viewModel.rejectRoomInvite(it.roomId) }) { | ||
Button(modifier = Modifier.weight(1f), onClick = { viewModel.dispatch(ProfileAction.RejectRoomInvite(it.roomId)) }) { | ||
Text("Reject".uppercase()) | ||
} | ||
Spacer(modifier = Modifier.fillMaxWidth(0.1f)) | ||
Button(modifier = Modifier.weight(1f), onClick = { viewModel.acceptRoomInvite(it.roomId) }) { | ||
Button(modifier = Modifier.weight(1f), onClick = { viewModel.dispatch(ProfileAction.AcceptRoomInvite(it.roomId)) }) { | ||
Text("Accept".uppercase()) | ||
} | ||
} | ||
|
@@ -154,7 +158,7 @@ private fun SpiderItemScope.Invitations(viewModel: ProfileViewModel, invitations | |
private fun RoomInvite.inviterName() = this.from.displayName?.let { "$it (${this.from.id.value})" } ?: this.from.id.value | ||
|
||
@Composable | ||
private fun ProfileViewModel.ObserveEvents() { | ||
private fun ProfileState.ObserveEvents() { | ||
// StartObserving { | ||
// [email protected] { | ||
// when (it) { | ||
|
Oops, something went wrong.