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

Feat: Implemented splash screen #84

Merged
merged 6 commits into from
Nov 6, 2023
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
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ dependencies {
"fullImplementation"(libs.firebase.analytics)
"fullImplementation"(libs.firebase.crashlytics)

// Splash Screen
implementation(libs.splashscreen)

}

if (!buildFoss){
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<activity
android:name=".view.activity.MainActivity"
android:exported="true"
android:theme="@style/Theme.FoodExpirationDates">
android:theme="@style/Theme.CustomSplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ import com.lorenzovainigli.foodexpirationdates.model.NotificationManager
import com.lorenzovainigli.foodexpirationdates.view.composable.activity.MainActivityLayout
import com.lorenzovainigli.foodexpirationdates.viewmodel.ExpirationDatesViewModel
import dagger.hilt.android.AndroidEntryPoint
import androidx.activity.viewModels
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

private val viewModel: ExpirationDatesViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {

val splashScreen = installSplashScreen()
splashScreen.setKeepOnScreenCondition { viewModel.isSplashScreenLoading.value }

super.onCreate(savedInstanceState)
DaggerAppComponent.builder()
.appModule(AppModule())
Expand All @@ -27,7 +35,6 @@ class MainActivity : ComponentActivity() {
override fun onResume() {
super.onResume()
setContent {
val viewModel: ExpirationDatesViewModel = viewModel()
val items by viewModel.getDates().collectAsState(emptyList())
MainActivityLayout(
context = this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import javax.inject.Inject
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.State
import kotlinx.coroutines.async
import kotlinx.coroutines.delay

@HiltViewModel
class ExpirationDatesViewModel @Inject constructor(
Expand All @@ -17,9 +22,18 @@ class ExpirationDatesViewModel @Inject constructor(
private var expirationDates : Flow<List<ExpirationDate>> = flowOf(emptyList())
private var expirationDate : ExpirationDate? = null

private val _isSplashScreenLoading: MutableState<Boolean> = mutableStateOf(value = true)
val isSplashScreenLoading: State<Boolean> = _isSplashScreenLoading

fun getDates(): Flow<List<ExpirationDate>> {
viewModelScope.launch {
expirationDates = repository.getAll()
_isSplashScreenLoading.value = true
val deferred = async {
expirationDates = repository.getAll()
}
deferred.await()
delay(1000)
_isSplashScreenLoading.value = false
}
return expirationDates
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_splash.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:height="100dp"
android:width="100dp"
android:drawable="@drawable/fed_icon"
android:gravity="center"
/>
</layer-list>
6 changes: 6 additions & 0 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
<resources>

<style name="Theme.FoodExpirationDates" parent="android:Theme.Material.Light.NoActionBar" />

<style name="Theme.CustomSplashScreenTheme" parent="Theme.SplashScreen">
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_splash</item>
<item name="postSplashScreenTheme">@style/Theme.FoodExpirationDates</item>
</style>

</resources>
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test-runner = "1.5.2"
ui-test-junit4 = "1.5.3"
uiautomator = "2.2.0"
work-runtime-ktx = "2.8.1"
splashscreen = "1.0.1"

[libraries]
accompanist-systemuicontroller = { module = "com.google.accompanist:accompanist-systemuicontroller", version.ref = "accompanist-systemuicontroller" }
Expand Down Expand Up @@ -65,6 +66,7 @@ ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4", versi
ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
splashscreen = { module = "androidx.core:core-splashscreen", version.ref = "splashscreen" }

[plugins]
app-cash-paparazzi = { id = "app.cash.paparazzi", version.ref = "paparazzi"}
Expand Down