Refactors AppScreen out of Activity.
This commit is contained in:
@@ -0,0 +1,86 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2024 Vitor Pamplona
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
* this software and associated documentation files (the "Software"), to deal in
|
||||||
|
* the Software without restriction, including without limitation the rights to use,
|
||||||
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||||
|
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||||
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.vitorpamplona.amethyst.ui
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.Surface
|
||||||
|
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
|
||||||
|
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
|
import com.google.accompanist.adaptive.calculateDisplayFeatures
|
||||||
|
import com.vitorpamplona.amethyst.ServiceManager
|
||||||
|
import com.vitorpamplona.amethyst.ui.screen.AccountScreen
|
||||||
|
import com.vitorpamplona.amethyst.ui.screen.AccountStateViewModel
|
||||||
|
import com.vitorpamplona.amethyst.ui.screen.SharedPreferencesViewModel
|
||||||
|
import com.vitorpamplona.amethyst.ui.theme.AmethystTheme
|
||||||
|
|
||||||
|
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
|
||||||
|
@Composable
|
||||||
|
fun prepareSharedViewModel(act: MainActivity): SharedPreferencesViewModel {
|
||||||
|
val sharedPreferencesViewModel: SharedPreferencesViewModel = viewModel()
|
||||||
|
|
||||||
|
val displayFeatures = calculateDisplayFeatures(act)
|
||||||
|
val windowSizeClass = calculateWindowSizeClass(act)
|
||||||
|
|
||||||
|
println("ZZAAPP Launcher $act.isOnMobileDataState $sharedPreferencesViewModel $displayFeatures $windowSizeClass")
|
||||||
|
|
||||||
|
LaunchedEffect(key1 = sharedPreferencesViewModel) {
|
||||||
|
sharedPreferencesViewModel.init()
|
||||||
|
sharedPreferencesViewModel.updateDisplaySettings(windowSizeClass, displayFeatures)
|
||||||
|
}
|
||||||
|
|
||||||
|
LaunchedEffect(act.isOnMobileDataState) {
|
||||||
|
sharedPreferencesViewModel.updateConnectivityStatusState(act.isOnMobileDataState)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sharedPreferencesViewModel
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun AppScreen(
|
||||||
|
sharedPreferencesViewModel: SharedPreferencesViewModel,
|
||||||
|
serviceManager: ServiceManager,
|
||||||
|
) {
|
||||||
|
Log.d("onDispose", "ZZAAPP Launcher AppScreen $sharedPreferencesViewModel $serviceManager")
|
||||||
|
AmethystTheme(sharedPreferencesViewModel) {
|
||||||
|
// A surface container using the 'background' color from the theme
|
||||||
|
Surface(
|
||||||
|
modifier = Modifier.fillMaxSize(),
|
||||||
|
color = MaterialTheme.colorScheme.background,
|
||||||
|
) {
|
||||||
|
val accountStateViewModel: AccountStateViewModel = viewModel()
|
||||||
|
accountStateViewModel.serviceManager = serviceManager
|
||||||
|
|
||||||
|
LaunchedEffect(key1 = Unit) {
|
||||||
|
accountStateViewModel.tryLoginExistingAccountAsync()
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("onDispose", "ZZAAPP Launcher AccountScreen $accountStateViewModel $sharedPreferencesViewModel")
|
||||||
|
|
||||||
|
AccountScreen(accountStateViewModel, sharedPreferencesViewModel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,16 +33,7 @@ import androidx.activity.compose.setContent
|
|||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.Surface
|
|
||||||
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
|
|
||||||
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
|
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.ui.Modifier
|
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
|
||||||
import com.google.accompanist.adaptive.calculateDisplayFeatures
|
|
||||||
import com.vitorpamplona.amethyst.LocalPreferences
|
import com.vitorpamplona.amethyst.LocalPreferences
|
||||||
import com.vitorpamplona.amethyst.ServiceManager
|
import com.vitorpamplona.amethyst.ServiceManager
|
||||||
import com.vitorpamplona.amethyst.model.LocalCache
|
import com.vitorpamplona.amethyst.model.LocalCache
|
||||||
@@ -53,10 +44,6 @@ import com.vitorpamplona.amethyst.ui.components.DEFAULT_MUTED_SETTING
|
|||||||
import com.vitorpamplona.amethyst.ui.components.keepPlayingMutex
|
import com.vitorpamplona.amethyst.ui.components.keepPlayingMutex
|
||||||
import com.vitorpamplona.amethyst.ui.navigation.Route
|
import com.vitorpamplona.amethyst.ui.navigation.Route
|
||||||
import com.vitorpamplona.amethyst.ui.navigation.debugState
|
import com.vitorpamplona.amethyst.ui.navigation.debugState
|
||||||
import com.vitorpamplona.amethyst.ui.screen.AccountScreen
|
|
||||||
import com.vitorpamplona.amethyst.ui.screen.AccountStateViewModel
|
|
||||||
import com.vitorpamplona.amethyst.ui.screen.SharedPreferencesViewModel
|
|
||||||
import com.vitorpamplona.amethyst.ui.theme.AmethystTheme
|
|
||||||
import com.vitorpamplona.quartz.encoders.Nip19Bech32
|
import com.vitorpamplona.quartz.encoders.Nip19Bech32
|
||||||
import com.vitorpamplona.quartz.encoders.Nip47WalletConnect
|
import com.vitorpamplona.quartz.encoders.Nip47WalletConnect
|
||||||
import com.vitorpamplona.quartz.events.ChannelCreateEvent
|
import com.vitorpamplona.quartz.events.ChannelCreateEvent
|
||||||
@@ -76,14 +63,13 @@ import java.util.Timer
|
|||||||
import kotlin.concurrent.schedule
|
import kotlin.concurrent.schedule
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
private val isOnMobileDataState = mutableStateOf(false)
|
val isOnMobileDataState = mutableStateOf(false)
|
||||||
private val isOnWifiDataState = mutableStateOf(false)
|
private val isOnWifiDataState = mutableStateOf(false)
|
||||||
|
|
||||||
// Service Manager is only active when the activity is active.
|
// Service Manager is only active when the activity is active.
|
||||||
val serviceManager = ServiceManager()
|
val serviceManager = ServiceManager()
|
||||||
private var shouldPauseService = true
|
private var shouldPauseService = true
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
|
|
||||||
@RequiresApi(Build.VERSION_CODES.R)
|
@RequiresApi(Build.VERSION_CODES.R)
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@@ -91,36 +77,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
Log.d("Lifetime Event", "MainActivity.onCreate")
|
Log.d("Lifetime Event", "MainActivity.onCreate")
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
val sharedPreferencesViewModel: SharedPreferencesViewModel = viewModel()
|
val sharedPreferencesViewModel = prepareSharedViewModel(act = this)
|
||||||
|
AppScreen(sharedPreferencesViewModel = sharedPreferencesViewModel, serviceManager = serviceManager)
|
||||||
val displayFeatures = calculateDisplayFeatures(this)
|
|
||||||
val windowSizeClass = calculateWindowSizeClass(this)
|
|
||||||
|
|
||||||
LaunchedEffect(key1 = sharedPreferencesViewModel) {
|
|
||||||
sharedPreferencesViewModel.init()
|
|
||||||
sharedPreferencesViewModel.updateDisplaySettings(windowSizeClass, displayFeatures)
|
|
||||||
}
|
|
||||||
|
|
||||||
LaunchedEffect(isOnMobileDataState) {
|
|
||||||
sharedPreferencesViewModel.updateConnectivityStatusState(isOnMobileDataState)
|
|
||||||
}
|
|
||||||
|
|
||||||
AmethystTheme(sharedPreferencesViewModel) {
|
|
||||||
// A surface container using the 'background' color from the theme
|
|
||||||
Surface(
|
|
||||||
modifier = Modifier.fillMaxSize(),
|
|
||||||
color = MaterialTheme.colorScheme.background,
|
|
||||||
) {
|
|
||||||
val accountStateViewModel: AccountStateViewModel = viewModel()
|
|
||||||
accountStateViewModel.serviceManager = serviceManager
|
|
||||||
|
|
||||||
LaunchedEffect(key1 = Unit) {
|
|
||||||
accountStateViewModel.tryLoginExistingAccountAsync()
|
|
||||||
}
|
|
||||||
|
|
||||||
AccountScreen(accountStateViewModel, sharedPreferencesViewModel)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user