Finishes the migration of the service manager to the Application class

This commit is contained in:
Vitor Pamplona
2024-08-26 18:21:53 -04:00
parent 356740479c
commit 66ed26dcea
3 changed files with 4 additions and 11 deletions
@@ -26,7 +26,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.adaptive.calculateDisplayFeatures import com.google.accompanist.adaptive.calculateDisplayFeatures
import com.vitorpamplona.amethyst.ServiceManager
import com.vitorpamplona.amethyst.ui.screen.AccountScreen import com.vitorpamplona.amethyst.ui.screen.AccountScreen
import com.vitorpamplona.amethyst.ui.screen.AccountStateViewModel import com.vitorpamplona.amethyst.ui.screen.AccountStateViewModel
import com.vitorpamplona.amethyst.ui.screen.SharedPreferencesViewModel import com.vitorpamplona.amethyst.ui.screen.SharedPreferencesViewModel
@@ -56,13 +55,9 @@ fun prepareSharedViewModel(act: MainActivity): SharedPreferencesViewModel {
} }
@Composable @Composable
fun AppScreen( fun AppScreen(sharedPreferencesViewModel: SharedPreferencesViewModel) {
sharedPreferencesViewModel: SharedPreferencesViewModel,
serviceManager: ServiceManager,
) {
AmethystTheme(sharedPreferencesViewModel) { AmethystTheme(sharedPreferencesViewModel) {
val accountStateViewModel: AccountStateViewModel = viewModel() val accountStateViewModel: AccountStateViewModel = viewModel()
accountStateViewModel.serviceManager = serviceManager
LaunchedEffect(key1 = Unit) { LaunchedEffect(key1 = Unit) {
accountStateViewModel.tryLoginExistingAccountAsync() accountStateViewModel.tryLoginExistingAccountAsync()
@@ -78,7 +78,7 @@ class MainActivity : AppCompatActivity() {
setContent { setContent {
val sharedPreferencesViewModel = prepareSharedViewModel(act = this) val sharedPreferencesViewModel = prepareSharedViewModel(act = this)
AppScreen(sharedPreferencesViewModel = sharedPreferencesViewModel, serviceManager = Amethyst.instance.serviceManager) AppScreen(sharedPreferencesViewModel = sharedPreferencesViewModel)
} }
} }
@@ -25,8 +25,8 @@ import androidx.compose.runtime.Stable
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.AccountInfo import com.vitorpamplona.amethyst.AccountInfo
import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.ServiceManager
import com.vitorpamplona.amethyst.model.AccountSettings import com.vitorpamplona.amethyst.model.AccountSettings
import com.vitorpamplona.amethyst.model.DefaultChannels import com.vitorpamplona.amethyst.model.DefaultChannels
import com.vitorpamplona.amethyst.model.DefaultDMRelayList import com.vitorpamplona.amethyst.model.DefaultDMRelayList
@@ -66,8 +66,6 @@ val EMAIL_PATTERN = Pattern.compile(".+@.+\\.[a-z]+")
@Stable @Stable
class AccountStateViewModel : ViewModel() { class AccountStateViewModel : ViewModel() {
var serviceManager: ServiceManager? = null
private val _accountContent = MutableStateFlow<AccountState>(AccountState.Loading) private val _accountContent = MutableStateFlow<AccountState>(AccountState.Loading)
val accountContent = _accountContent.asStateFlow() val accountContent = _accountContent.asStateFlow()
@@ -86,7 +84,7 @@ class AccountStateViewModel : ViewModel() {
private suspend fun requestLoginUI() { private suspend fun requestLoginUI() {
_accountContent.update { AccountState.LoggedOff } _accountContent.update { AccountState.LoggedOff }
viewModelScope.launch(Dispatchers.IO) { serviceManager?.pauseForGoodAndClearAccount() } viewModelScope.launch(Dispatchers.IO) { Amethyst.instance.serviceManager.pauseForGoodAndClearAccount() }
} }
suspend fun loginAndStartUI( suspend fun loginAndStartUI(