Skip to content

Commit

Permalink
🚀 初回起動時の読み込み処理をカイゼン
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsutakein committed Nov 21, 2023
1 parent 8f7f5c9 commit 63d5f87
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
49 changes: 17 additions & 32 deletions app/android/src/main/java/club/nito/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,38 @@ import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import club.nito.core.model.AuthStatus
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

private val viewModel: MainActivityViewModel by viewModels()

@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()

super.onCreate(savedInstanceState)

var uiState: MainActivityUiState by mutableStateOf(MainActivityUiState.Loading)
val uiState = mutableStateOf<MainActivityUiState>(MainActivityUiState.Loading)

// Update the uiState
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState
.onEach {
uiState = it
}
.collect()
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect {
uiState.value = it
}
}
}

splashScreen.setKeepOnScreenCondition {
when (uiState) {
MainActivityUiState.Loading -> true
is MainActivityUiState.Success -> false
}
uiState.value !is MainActivityUiState.Success
}

enableEdgeToEdge(
Expand All @@ -68,19 +55,17 @@ class MainActivity : ComponentActivity() {
window.isNavigationBarContrastEnforced = false

setContent {
val windowSize = calculateWindowSizeClass(this)
NitoApp(
windowSize = windowSize,
authStatus = shouldAuthStatus(uiState),
)
when (val state = uiState.value) {
MainActivityUiState.Loading -> {}

is MainActivityUiState.Success -> {
val windowSize = calculateWindowSizeClass(this)
NitoApp(
windowSize = windowSize,
authStatus = state.authStatus,
)
}
}
}
}
}

@Composable
private fun shouldAuthStatus(
uiState: MainActivityUiState,
): AuthStatus? = when (uiState) {
MainActivityUiState.Loading -> null
is MainActivityUiState.Success -> uiState.authStatus
}
2 changes: 1 addition & 1 deletion app/android/src/main/java/club/nito/app/NitoApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.koin.compose.KoinApplication
@Composable
fun NitoApp(
windowSize: WindowSizeClass,
authStatus: AuthStatus?,
authStatus: AuthStatus,
modifier: Modifier = Modifier,
) {
KoinApplication(
Expand Down
6 changes: 2 additions & 4 deletions app/android/src/main/java/club/nito/app/NitoNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const val rootNavigationRoute = "root_route"
@Composable
fun NitoNavHost(
windowSize: WindowSizeClass,
authStatus: AuthStatus?,
authStatus: AuthStatus,
modifier: Modifier = Modifier,
navController: NavHostController = rememberNavController(),
startDestination: String = rootNavigationRoute,
Expand Down Expand Up @@ -72,7 +72,7 @@ fun NitoNavHost(
}

private fun NavGraphBuilder.root(
authStatus: AuthStatus?,
authStatus: AuthStatus,
navController: NavHostController,
) = composable(rootNavigationRoute) {
LaunchedEffect(authStatus) {
Expand All @@ -92,8 +92,6 @@ private fun NavGraphBuilder.root(
}
},
)

else -> {}
}
}
}

0 comments on commit 63d5f87

Please sign in to comment.