Skip to content

Commit

Permalink
welcome: smoother start (fixes #1862) (#2091)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <dogi@users.noreply.github.com>
Okuro3499 and dogi authored Oct 14, 2024
1 parent 2450a56 commit bb47a51
Showing 6 changed files with 72 additions and 69 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -95,6 +95,7 @@ dependencies {
implementation 'me.toptas.fancyshowcase:fancyshowcaseview:1.3.3'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.connectbot:sshlib:2.2.9'
implementation "androidx.core:core-splashscreen:1.0.1"

def lifecycle_version = "2.8.6"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
10 changes: 6 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -34,11 +34,13 @@
android:theme="@style/MyMaterialTheme.Base"
android:usesCleartextTraffic="true"
tools:replace="android:allowBackup">

<activity android:name=".IntroActivity" />
<activity
android:name=".SplashScreenActivity"
android:exported="true"
android:screenOrientation="portrait">
android:screenOrientation="portrait"
android:theme="@style/Theme.App.Starting">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
@@ -56,9 +58,9 @@
android:theme="@style/Theme.AppCompat"
android:windowSoftInputMode="stateAlwaysVisible|adjustResize" />

<service android:name=".utils.GPSService"
android:foregroundServiceType="location"
/>
<service
android:name=".utils.GPSService"
android:foregroundServiceType="location" />

<activity
android:name=".InitialActivity"
70 changes: 36 additions & 34 deletions app/src/main/kotlin/io/treehouses/remote/SplashScreenActivity.kt
Original file line number Diff line number Diff line change
@@ -10,82 +10,84 @@ import android.util.DisplayMetrics
import android.view.WindowManager
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.preference.PreferenceManager
import io.treehouses.remote.utils.SaveUtils

import io.treehouses.remote.databinding.ActivitySplashScreenBinding
import io.treehouses.remote.utils.SaveUtils.Screens.FIRST_TIME

class SplashScreenActivity : AppCompatActivity() {
lateinit var activitySplashBinding: ActivitySplashScreenBinding
private var logoAnimation: Animation? = null
private var textAnimation: Animation? = null
private var logo: ImageView? = null
private var logoText: TextView? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val preferences = PreferenceManager.getDefaultSharedPreferences(this@SplashScreenActivity)
activitySplashBinding = ActivitySplashScreenBinding.inflate(layoutInflater)

// Install the splash screen
val splashScreen = installSplashScreen()

// Apply customizations (e.g., night mode, font scale)
nightMode()
adjustFontScale(resources.configuration, fontSize())

val preferences = PreferenceManager.getDefaultSharedPreferences(this)
if (preferences.getBoolean("splashScreen", true)) {
setContentView(R.layout.activity_splash_screen)
logo = findViewById(R.id.splash_logo)
logoAnimation = AnimationUtils.loadAnimation(this, R.anim.splash_logo_anim)
logo?.animation = logoAnimation
logoText = findViewById(R.id.logo_text)
// Keep the splash screen for the duration
splashScreen.setKeepOnScreenCondition { true }
setContentView(activitySplashBinding.root)
textAnimation = AnimationUtils.loadAnimation(this, R.anim.splash_text_anim)
logoText?.animation = textAnimation
Handler(Looper.getMainLooper()).postDelayed({ goToNextActivity() }, SPLASH_TIME_OUT.toLong())
} else { goToNextActivity() }
activitySplashBinding.logoText.animation = textAnimation
goToNextActivityAfterDelay()
} else {
goToNextActivity()
}
}

fun adjustFontScale(configuration: Configuration?, fontSize: Int) {
private fun adjustFontScale(configuration: Configuration?, fontSize: Int) {
configuration?.let {
it.fontScale = 0.05F*fontSize.toFloat()
it.fontScale = 0.05F * fontSize.toFloat()
val metrics: DisplayMetrics = resources.displayMetrics
val wm: WindowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
wm.defaultDisplay.getMetrics(metrics)
metrics.scaledDensity = configuration.fontScale * metrics.density

baseContext.applicationContext.createConfigurationContext(it)
baseContext.resources.displayMetrics.setTo(metrics)

}
}

private fun goToNextActivityAfterDelay() {
Handler(Looper.getMainLooper()).postDelayed({ goToNextActivity() }, SPLASH_TIME_OUT.toLong())
}

private fun goToNextActivity() {
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
if (preferences.getBoolean(SaveUtils.Screens.FIRST_TIME.name, true)) {
if (preferences.getBoolean(FIRST_TIME.name, true)) {
startActivity(Intent(this, IntroActivity::class.java))
val editor = preferences.edit()
editor.putBoolean(SaveUtils.Screens.FIRST_TIME.name, false)
editor.apply()
}
else {
preferences.edit().putBoolean(FIRST_TIME.name, false).apply()
} else {
startActivity(Intent(this@SplashScreenActivity, InitialActivity::class.java))
}
finish()
}

companion object {
private const val SPLASH_TIME_OUT = 2000
}

private fun nightMode() {
val preference = PreferenceManager.getDefaultSharedPreferences(this).getString("dark_mode", "Follow System")
val options = listOf(*resources.getStringArray(R.array.dark_mode_options))
resources.getStringArray(R.array.led_options_commands)
val options = resources.getStringArray(R.array.dark_mode_options).toList()
when (options.indexOf(preference)) {
0 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
1 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
2 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}

private fun fontSize(): Int
{
private fun fontSize(): Int {
return PreferenceManager.getDefaultSharedPreferences(this).getInt("font_size", 18)
}
}

companion object {
private const val SPLASH_TIME_OUT = 2000
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/splash_screen_image.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:drawable="@color/windowBackground" />
<item
android:width="100dp"
android:height="100dp"
android:drawable="@drawable/treehouses2"
android:gravity="center" />
</layer-list>
28 changes: 13 additions & 15 deletions app/src/main/res/layout/activity_splash_screen.xml
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/windowBackground"
android:gravity="center"
android:theme = "@style/AppTheme"
android:background="@color/windowBackground">
android:orientation="horizontal"
android:theme="@style/AppTheme">

<ImageView
android:id="@+id/splash_logo"
android:layout_width="wrap_content"
android:layout_height="96dp"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
app:srcCompat="@drawable/treehouses2" />

<TextView
android:id="@+id/logo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/splash_logo"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:paddingTop="8dp"
android:text="Treehouses"
android:textSize="18sp"
app:fontFamily="@font/roboto_medium"
android:textColor="@color/md_grey_600"
android:paddingTop="8dp"
android:layout_gravity="center"
android:layout_below="@+id/splash_logo"
android:layout_centerHorizontal="true"/>

</RelativeLayout>
android:textSize="18sp"
app:fontFamily="@font/roboto_medium" />
</RelativeLayout>
23 changes: 7 additions & 16 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- Activity themes -->

<!-- Widget styling -->

<style name="Widget" />

<!-- Toolbar -->

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
@@ -28,7 +19,6 @@
</style>

<style name="CardTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
@@ -121,13 +111,14 @@
<item name="android:gravity">center</item>
</style>

<style name="PopupMenu" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:popupBackground">@color/card_background</item>
<style name="Theme.App.Splash" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="android:windowBackground">@color/windowBackground</item>
</style>

<style name="CustomPopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:popupMenuStyle">@style/PopupMenu</item>
<item name="android:textColor">@color/daynight_textColor</item>
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/windowBackground</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/splash_screen_image</item>
<item name="windowSplashScreenAnimationDuration">200</item>
<item name="postSplashScreenTheme">@style/MyMaterialTheme.Base</item>
</style>

</resources>

0 comments on commit bb47a51

Please sign in to comment.