Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
lulululbj committed Dec 28, 2023
1 parent 7c31005 commit 72ae10e
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 33 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ dependencies {

implementation(project(":luyao_ktx"))

implementation("androidx.core:core-splashscreen:1.0.1")
implementation("com.airbnb.android:lottie-compose:5.2.0")

val composeBom = platform("androidx.compose:compose-bom:2023.08.00")
implementation(composeBom)
androidTestImplementation(composeBom)
Expand All @@ -117,6 +120,7 @@ dependencies {
implementation("com.google.accompanist:accompanist-systemuicontroller:$accompanistVersion")



// val roomVersion = "2.6.0"
// implementation("androidx.room:room-runtime:$roomVersion")
// implementation("androidx.room:room-ktx:$roomVersion")
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
xmlns:tools="http://schemas.android.com/tools">

<application
android:name=".WanApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Wanandroid"
android:theme="@style/Theme.Wanandroid.Splash"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Wanandroid">
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
92 changes: 64 additions & 28 deletions app/src/main/java/luyao/wanandroid/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,80 @@ package luyao.wanandroid
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.activity.viewModels
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import luyao.wanandroid.ui.theme.WanandroidTheme
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import luyao.ktx.model.UiState
import luyao.wanandroid.model.AppConfig
import luyao.wanandroid.model.DarkThemeConfig
import luyao.wanandroid.vm.SplashVM

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

private val splashVM by viewModels<SplashVM>()

override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)

var appState: UiState<AppConfig> by mutableStateOf(UiState.Loading)

lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
splashVM.appState.onEach {
appState = it
}.collect()
}
}

splashScreen.setKeepOnScreenCondition {
when (appState) {
UiState.Loading -> true
else -> false
}
}

WindowCompat.setDecorFitsSystemWindows(window, false)

setContent {
WanandroidTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
val systemUiController = rememberSystemUiController()
val darkTheme = shouldUseDarkTheme(appState)

DisposableEffect(systemUiController, darkTheme) {
systemUiController.systemBarsDarkContentEnabled = !darkTheme
onDispose { }
}

}
}
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
WanandroidTheme {
Greeting("Android")
fun shouldUseDarkTheme(uiState: UiState<AppConfig>) =
when (uiState) {
UiState.Loading -> isSystemInDarkTheme()
is UiState.Success -> {
when (uiState.data.darkThemeConfig) {
DarkThemeConfig.LIGHT -> false
DarkThemeConfig.DARK -> true
DarkThemeConfig.FOLLOW_SYSTEM -> isSystemInDarkTheme()
}
}
else -> false
}
}

23 changes: 23 additions & 0 deletions app/src/main/java/luyao/wanandroid/WanApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package luyao.wanandroid

import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import dagger.hilt.android.HiltAndroidApp
import luyao.ktx.app.AppInit
import luyao.wanandroid.model.MMKVConstants
import javax.inject.Inject

@HiltAndroidApp
class WanApplication : Application() {

companion object {
lateinit var App: Application
}

override fun onCreate() {
super.onCreate()
App = this
AppInit.init(this)
AppCompatDelegate.setDefaultNightMode(MMKVConstants.nightMode)
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/luyao/wanandroid/model/AppConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package luyao.wanandroid.model

/**
* Description:
* Author: luyao
* Date: 2023/11/15 13:37
*/
data class AppConfig(
val darkThemeConfig: DarkThemeConfig
)
10 changes: 10 additions & 0 deletions app/src/main/java/luyao/wanandroid/model/DarkThemeConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package luyao.wanandroid.model

/**
* Description:
* Author: luyao
* Date: 2023/11/15 13:37
*/
enum class DarkThemeConfig {
FOLLOW_SYSTEM, LIGHT, DARK
}
16 changes: 16 additions & 0 deletions app/src/main/java/luyao/wanandroid/model/WanConstants.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package luyao.wanandroid.model

import androidx.appcompat.app.AppCompatDelegate
import luyao.ktx.util.MMKVDelegate

/**
* Description:
* Author: luyao
* Date: 2023/11/15 11:08
*/

object MMKVConstants {

var nightMode by MMKVDelegate("night_mode", AppCompatDelegate.MODE_NIGHT_NO) // 夜间模式

}
42 changes: 42 additions & 0 deletions app/src/main/java/luyao/wanandroid/ui/SplashPage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package luyao.wanandroid.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.animateLottieCompositionAsState
import com.airbnb.lottie.compose.rememberLottieComposition
import luyao.wanandroid.R

/**
* Description:
* Author: luyao
* Date: 2023/11/15 13:24
*/
@Composable
fun SplashPage(onFinish: () -> Unit) {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.White)
) {
val composition by rememberLottieComposition(spec = LottieCompositionSpec.RawRes(R.raw.android_logo))
val animationState = animateLottieCompositionAsState(composition = composition)
LottieAnimation(composition = composition, progress = { animationState.progress })
if (animationState.isAtEnd && animationState.isPlaying) {
onFinish()
}
}
}

@Preview
@Composable
fun SplashPagePreview() {
SplashPage {}
}
39 changes: 39 additions & 0 deletions app/src/main/java/luyao/wanandroid/vm/SplashVM.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package luyao.wanandroid.vm

import androidx.appcompat.app.AppCompatDelegate
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.stateIn
import luyao.ktx.base.BaseVM
import luyao.ktx.model.UiState
import luyao.wanandroid.model.AppConfig
import luyao.wanandroid.model.DarkThemeConfig
import luyao.wanandroid.model.MMKVConstants

/**
* Description:
* Author: luyao
* Date: 2023/11/15 13:35
*/
class SplashVM : BaseVM() {

val appState: StateFlow<UiState<AppConfig>> = flow<UiState<AppConfig>> {
val config = AppConfig(getDarkThemeConfig())
emit(UiState.Success(config))
}.stateIn(
scope = viewModelScope,
initialValue = UiState.Loading,
started = SharingStarted.WhileSubscribed(5000)
)


private fun getDarkThemeConfig() =
when (MMKVConstants.nightMode) {
AppCompatDelegate.MODE_NIGHT_NO -> DarkThemeConfig.LIGHT
AppCompatDelegate.MODE_NIGHT_YES -> DarkThemeConfig.DARK
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM -> DarkThemeConfig.FOLLOW_SYSTEM
else -> DarkThemeConfig.FOLLOW_SYSTEM
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/luyao/wanandroid/vm/WanVM.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package luyao.wanandroid.vm

import luyao.ktx.base.BaseVM

/**
* Description:
* Author: luyao
* Date: 2023/11/15 11:14
*/
class WanVM : BaseVM() {


}
Loading

0 comments on commit 72ae10e

Please sign in to comment.