Implement theme-choosing using a Spinner

This commit is contained in:
Finn M Glas 2020-08-18 21:01:32 +02:00
parent badbba535b
commit 70114fc9d3
No known key found for this signature in database
GPG key ID: 902A30146014DFBF
3 changed files with 77 additions and 73 deletions

View file

@ -1,6 +1,7 @@
package com.finnmglas.launcher.settings.launcher
import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
@ -100,7 +101,6 @@ class SettingsFragmentLauncher : Fragment(), UIObject {
.putInt(PREF_VIBRANT, vibrantColor)
.apply()
saveTheme("custom")
intendedSettingsPause = true
activity!!.recreate()
}
@ -109,36 +109,36 @@ class SettingsFragmentLauncher : Fragment(), UIObject {
}
override fun applyTheme() {
// Hide 'select' button for the selected theme or allow customisation
when (getSavedTheme(context!!)) {
"dark" -> settings_theme_dark_button_select.visibility = View.INVISIBLE
"finn" -> settings_theme_finn_button_select.visibility = View.INVISIBLE
"custom" ->
settings_theme_custom_button_select.text = getString(R.string.settings_select_image)
}
setSwitchColor(settings_launcher_switch_screen_timeout, vibrantColor)
settings_launcher_container.setBackgroundColor(dominantColor)
setButtonColor(settings_theme_finn_button_select, vibrantColor)
setButtonColor(settings_theme_dark_button_select, vibrantColor)
setButtonColor(settings_theme_custom_button_select, vibrantColor)
}
override fun setOnClicks() {
// Theme changing buttons
settings_theme_dark_button_select.setOnClickListener {
resetToDarkTheme(activity!!)
}
settings_theme_finn_button_select.setOnClickListener {
resetToDefaultTheme(activity!!)
}
settings_theme_custom_button_select.setOnClickListener {
resetToCustomTheme(activity!!)
}
settings_launcher_switch_screen_timeout.setOnClickListener { // Toggle screen timeout
launcherPreferences.edit()
.putBoolean(PREF_SCREEN_TIMEOUT_DISABLED,
!launcherPreferences.getBoolean(PREF_SCREEN_TIMEOUT_DISABLED, false))
.apply()
setWindowFlags(activity!!.window)
}
}
fun resetToCustomTheme(context: Activity) {
intendedSettingsPause = true
saveTheme("custom") // TODO: Fix the bug this creates (displays custom theme without chosen img)
// Request permission (on newer APIs)
if (Build.VERSION.SDK_INT >= 23) {
when {
ContextCompat.checkSelfPermission(context!!,
ContextCompat.checkSelfPermission(context,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
-> letUserPickImage(true)
shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)
@ -151,15 +151,6 @@ class SettingsFragmentLauncher : Fragment(), UIObject {
}
else letUserPickImage()
}
settings_launcher_switch_screen_timeout.setOnClickListener { // Toggle screen timeout
launcherPreferences.edit()
.putBoolean(PREF_SCREEN_TIMEOUT_DISABLED,
!launcherPreferences.getBoolean(PREF_SCREEN_TIMEOUT_DISABLED, false))
.apply()
setWindowFlags(activity!!.window)
}
}
override fun adjustLayout() {
// visually load settings
@ -187,5 +178,36 @@ class SettingsFragmentLauncher : Fragment(), UIObject {
}
}
// Load values into the theme spinner
val staticThemeAdapter = ArrayAdapter.createFromResource(
activity!!, R.array.settings_themes,
android.R.layout.simple_spinner_item )
staticThemeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
settings_launcher_theme_spinner.adapter = staticThemeAdapter
var themeInt = when (getSavedTheme(activity!!)) {
"finn" -> 0
"dark" -> 1
"custom" -> 2
else -> 0
};
settings_launcher_theme_spinner.setSelection(themeInt)
settings_launcher_theme_spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
when (position) {
0 -> if (getSavedTheme(activity!!) != "finn") resetToDefaultTheme(activity!!)
1 -> if (getSavedTheme(activity!!) != "dark") resetToDarkTheme(activity!!)
2 -> if (getSavedTheme(activity!!) != "custom") resetToCustomTheme(activity!!)
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
}
}

View file

@ -85,10 +85,24 @@
android:id="@+id/settings_launcher_theme_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/settings_launcher_time_formats"
android:entries="@array/settings_themes"
android:spinnerMode="dropdown" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16sp"
android:orientation="horizontal">
<Button
android:id="@+id/settings_theme_custom_button_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose a wallpaper"
android:textAllCaps="false" />
</LinearLayout>
<LinearLayout
android:id="@+id/settings_launcher_section_options"
android:layout_width="match_parent"
@ -141,43 +155,4 @@
</LinearLayout>
<androidx.cardview.widget.CardView
android:id="@+id/settings_theme_dark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
app:layout_constraintTop_toBottomOf="@+id/settings_launcher_switch_screen_timeout"
tools:layout_editor_absoluteX="32dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/settings_theme_dark_button_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dark"
android:textAllCaps="false"
app:layout_constraintStart_toEndOf="@+id/settings_theme_finn_button_select" />
<Button
android:id="@+id/settings_theme_finn_button_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default"
android:textAllCaps="false"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/settings_theme_custom_button_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wallpaper"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>

View file

@ -48,6 +48,13 @@
<string name="settings_select_theme">Select</string>
<string name="settings_select_image">Change Image</string>
<string name="settings_theme_examples">Examples</string>
<string-array name="settings_themes">
<item>Default</item>
<item>Dark</item>
<item>Custom</item>
</string-array>
<string name="settings_launcher_disable_timeout">Keep screen on</string>
<string name="settings_launcher_time_format">Date format</string>