Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh incidents #122

Merged
merged 16 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.crisiscleanup.core.common.AppEnv
import com.crisiscleanup.core.common.LocationProvider
import com.crisiscleanup.core.common.PermissionManager
import com.crisiscleanup.core.common.PermissionStatus
import com.crisiscleanup.core.common.event.AuthEventBus
import com.crisiscleanup.core.common.event.AccountEventBus
import com.crisiscleanup.core.common.log.TagLogger
import com.crisiscleanup.core.common.sync.SyncPuller
import com.crisiscleanup.core.common.sync.SyncPusher
Expand Down Expand Up @@ -148,13 +148,17 @@ class AppSyncer @Inject constructor() : SyncPuller, SyncPusher {
}

@Singleton
class AppAuthEventBus @Inject constructor() : AuthEventBus {
class AppAccountEventBus @Inject constructor() : AccountEventBus {
override val logouts = MutableStateFlow(false)
override val refreshedTokens = MutableStateFlow(false)
override val inactiveOrganizations = MutableStateFlow(0L)

override fun onLogout() {}

override fun onTokensRefreshed() {}

override fun onAccountInactiveOrganization(accountId: Long) {}
override fun clearAccountInactiveOrganization() {}
}

@Singleton
Expand Down Expand Up @@ -206,7 +210,7 @@ interface AppModule {
fun bindsSyncPusher(syncer: AppSyncer): SyncPusher

@Binds
fun bindsAuthEventBus(eventBus: AppAuthEventBus): AuthEventBus
fun bindsAccountEventBus(eventBus: AppAccountEventBus): AccountEventBus

@Binds
fun bindsLocationProvider(provider: AppLocationProvider): LocationProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import com.crisiscleanup.sandbox.MultiImageViewModel
fun MultiImageRoute(
onBack: () -> Unit = {},
) {
MultiImageView(onBack = onBack)
MultiImageView(onBack)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun MultiImageView(
viewModel: MultiImageViewModel = hiltViewModel(),
onBack: () -> Unit = {},
viewModel: MultiImageViewModel = hiltViewModel(),
) {
var showPhotosGrid by remember { mutableStateOf(false) }
val hidePhotosGridOnBack = remember(viewModel) {
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ plugins {

android {
defaultConfig {
val buildVersion = 223
val buildVersion = 233
applicationId = "com.crisiscleanup"
versionCode = buildVersion
versionName = "0.9.${buildVersion - 168}"
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/crisiscleanup/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ import com.crisiscleanup.core.common.NetworkMonitor
import com.crisiscleanup.core.common.PermissionManager
import com.crisiscleanup.core.common.PhoneNumberPicker
import com.crisiscleanup.core.common.VisualAlertManager
import com.crisiscleanup.core.common.event.AuthEventBus
import com.crisiscleanup.core.common.event.AccountEventBus
import com.crisiscleanup.core.common.event.TrimMemoryEventManager
import com.crisiscleanup.core.common.log.AppLogger
import com.crisiscleanup.core.common.log.CrisisCleanupLoggers
import com.crisiscleanup.core.common.log.Logger
import com.crisiscleanup.core.common.sync.SyncPuller
import com.crisiscleanup.core.data.repository.AppMetricsRepository
import com.crisiscleanup.core.data.repository.EndOfLifeRepository
import com.crisiscleanup.core.data.repository.LanguageTranslationsRepository
import com.crisiscleanup.core.data.repository.LocalAppMetricsRepository
import com.crisiscleanup.core.designsystem.theme.CrisisCleanupTheme
import com.crisiscleanup.core.designsystem.theme.navigationContainerColor
import com.crisiscleanup.core.model.data.DarkThemeConfig
Expand Down Expand Up @@ -68,7 +68,7 @@ class MainActivity : ComponentActivity() {
private val viewModel: MainActivityViewModel by viewModels()

@Inject
internal lateinit var authEventBus: AuthEventBus
internal lateinit var accountEventBus: AccountEventBus

@Inject
internal lateinit var syncPuller: SyncPuller
Expand All @@ -90,7 +90,7 @@ class MainActivity : ComponentActivity() {
internal lateinit var endOfLifeRepository: EndOfLifeRepository

@Inject
internal lateinit var appMetricsRepository: AppMetricsRepository
internal lateinit var appMetricsRepository: LocalAppMetricsRepository

@Inject
internal lateinit var languageTranslationsRepository: LanguageTranslationsRepository
Expand Down
58 changes: 26 additions & 32 deletions app/src/main/java/com/crisiscleanup/MainActivityViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.crisiscleanup.core.common.KeyResourceTranslator
import com.crisiscleanup.core.common.NetworkMonitor
import com.crisiscleanup.core.common.TutorialDirector
import com.crisiscleanup.core.common.Tutorials
import com.crisiscleanup.core.common.event.AuthEventBus
import com.crisiscleanup.core.common.event.AccountEventBus
import com.crisiscleanup.core.common.event.ExternalEventBus
import com.crisiscleanup.core.common.event.UserPersistentInvite
import com.crisiscleanup.core.common.log.AppLogger
Expand Down Expand Up @@ -44,7 +44,6 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
Expand All @@ -56,7 +55,6 @@ import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.datetime.Clock
import java.util.concurrent.atomic.AtomicReference
import javax.inject.Inject
Expand All @@ -80,7 +78,7 @@ class MainActivityViewModel @Inject constructor(
private val appEnv: AppEnv,
firebaseAnalytics: FirebaseAnalytics,
externalEventBus: ExternalEventBus,
private val authEventBus: AuthEventBus,
private val accountEventBus: AccountEventBus,
private val networkMonitor: NetworkMonitor,
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
@Logger(CrisisCleanupLoggers.App) private val logger: AppLogger,
Expand Down Expand Up @@ -190,9 +188,8 @@ class MainActivityViewModel @Inject constructor(
initialValue = UserPersistentInvite(0, ""),
started = SharingStarted.WhileSubscribed(),
)

val isSwitchingToProduction: StateFlow<Boolean>
val productionSwitchMessage: StateFlow<String>
var showInactiveOrganization by mutableStateOf(false)
private set

val menuTutorialStep = menuTutorialDirector.tutorialStep

Expand All @@ -208,10 +205,11 @@ class MainActivityViewModel @Inject constructor(
logger.setAccountId(it.id.toString())
} else {
if (!it.hasAcceptedTerms) {
authEventBus.onLogout()
accountEventBus.onLogout()
}
}
}
.flowOn(ioDispatcher)
.launchIn(viewModelScope)

incidentSelector.incidentId
Expand Down Expand Up @@ -239,29 +237,25 @@ class MainActivityViewModel @Inject constructor(
.onEach {
isAcceptingTerms = false

withContext(ioDispatcher) {
isFetchingTermsAcceptance.value = true
try {
accountDataRefresher.updateAcceptedTerms()
} finally {
isFetchingTermsAcceptance.value = false
}
isFetchingTermsAcceptance.value = true
try {
accountDataRefresher.updateAcceptedTerms()
} finally {
isFetchingTermsAcceptance.value = false
}
}
.flowOn(ioDispatcher)
.launchIn(viewModelScope)

val switchProductionApiManager = SwitchProductionApiManager(
appMetricsRepository,
appDataRepository,
logger,
viewModelScope,
)
viewState
.filter { it is MainActivityViewState.Success }
.onEach { switchProductionApiManager.switchToProduction() }
accountEventBus.inactiveOrganizations
.throttleLatest(5_000)
.filter { it > 0 }
.onEach {
showInactiveOrganization = true
appDataRepository.clearAppData()
}
.flowOn(ioDispatcher)
.launchIn(viewModelScope)
isSwitchingToProduction = switchProductionApiManager.isSwitchingToProduction
productionSwitchMessage = switchProductionApiManager.productionSwitchMessage
}

private fun sync(cancelOngoing: Boolean) {
Expand All @@ -281,15 +275,14 @@ class MainActivityViewModel @Inject constructor(

fun onRejectTerms() {
acceptTermsErrorMessage = ""
authEventBus.onLogout()
accountEventBus.onLogout()
}

fun onAcceptTerms() {
acceptTermsErrorMessage = ""

if (!isAcceptingTerms) {
acceptTermsErrorMessage =
translator("termsConditionsModal.must_check_box")
acceptTermsErrorMessage = translator("termsConditionsModal.must_check_box")
return
}

Expand Down Expand Up @@ -329,9 +322,10 @@ class MainActivityViewModel @Inject constructor(
}
}

fun closeMenuTutorial() {
menuTutorialDirector.skipTutorial()
setMenuTutorialDone()
fun acknowledgeInactiveOrganization() {
showInactiveOrganization = false
accountEventBus.onLogout()
accountEventBus.clearAccountInactiveOrganization()
}
}

Expand Down
64 changes: 0 additions & 64 deletions app/src/main/java/com/crisiscleanup/SwitchProductionApiManager.kt

This file was deleted.

Loading
Loading