Add preference activity.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-12-29 14:30:04 +01:00
parent 968edfbb67
commit 3b7b71ce77
13 changed files with 211 additions and 31 deletions

View File

@@ -81,6 +81,7 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
implementation 'androidx.fragment:fragment:1.4.1' implementation 'androidx.fragment:fragment:1.4.1'
implementation 'androidx.preference:preference:1.1.1'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.4' androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'

View File

@@ -11,10 +11,19 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity
android:name=".SettingsActivity"
android:exported="false"
android:label="@string/title_activity_settings">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<service <service
android:name=".Tichu" android:name=".Tichu"
android:enabled="true" android:enabled="true"
android:exported="true"></service> android:exported="true" />
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"

View File

@@ -110,8 +110,8 @@ class MainActivity : AppCompatActivity() {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu) menuInflater.inflate(R.menu.menu_main, menu)
menu.findItem(R.id.action_screenOn).isChecked = //menu.findItem(R.id.action_screenOn).isChecked =
this.getSharedPreferences("Settings", MODE_PRIVATE).getBoolean("Screen_On", false) // this.getSharedPreferences("Settings", MODE_PRIVATE).getBoolean("Screen_On", false)
return true return true
} }
@@ -142,19 +142,24 @@ class MainActivity : AppCompatActivity() {
} }
true true
} }
R.id.action_theme -> { R.id.settings -> {
chooseThemeDialog() val i = Intent(this, SettingsActivity::class.java)
true startActivity(i)
}
R.id.action_language -> {
chooseLanguageDialog()
true
}
R.id.action_screenOn -> {
item.isChecked = !item.isChecked
keepScreenOn(item.isChecked)
true true
} }
//R.id.action_theme -> {
// chooseThemeDialog()
// true
//}
//R.id.action_language -> {
// chooseLanguageDialog()
// true
//}
//R.id.action_screenOn -> {
// item.isChecked = !item.isChecked
// keepScreenOn(item.isChecked)
// true
//}
else -> super.onOptionsItemSelected(item) else -> super.onOptionsItemSelected(item)
} }
} }

View File

@@ -0,0 +1,61 @@
package me.zobrist.tichucounter
import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceFragmentCompat
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import me.zobrist.tichucounter.databinding.SettingsActivityBinding
@AndroidEntryPoint
class SettingsActivity : AppCompatActivity() {
private lateinit var binding: SettingsActivityBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = SettingsActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
if (savedInstanceState == null) {
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
}
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.title = resources.getString(R.string.settings)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
val i = Intent(this, MainActivity::class.java)
navigateUpTo(i)
true
}
else -> super.onOptionsItemSelected(item)
}
}
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
}
}
}

View File

@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/colorPrimary" android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>

View File

@@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="24dp"
android:tint="#FFFFFF" android:viewportHeight="24"
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/colorPrimary" android:pathData="M12.5,8c-2.65,0 -5.05,0.99 -6.9,2.6L2,7v9h9l-3.62,-3.62c1.39,-1.16 3.16,-1.88 5.12,-1.88 3.54,0 6.55,2.31 7.6,5.5l2.37,-0.78C21.08,11.03 17.15,8 12.5,8z"/>
</vector>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@@ -4,26 +4,21 @@
tools:context="me.zobrist.tichucounter.MainActivity"> tools:context="me.zobrist.tichucounter.MainActivity">
<item <item
android:id="@+id/action_undo" android:id="@+id/action_undo"
android:icon="@android:drawable/ic_menu_revert" android:icon="@drawable/ic_baseline_undo_24"
android:orderInCategory="5" android:orderInCategory="5"
android:title="@string/undo" /> android:title="@string/undo"
app:showAsAction="ifRoom"/>
<item <item
android:id="@+id/action_clear" android:id="@+id/action_clear"
android:icon="@drawable/ic_baseline_add_24"
android:checkable="false" android:checkable="false"
android:orderInCategory="10" android:orderInCategory="10"
android:title="@string/clear" android:title="@string/clear"
app:showAsAction="ifRoom" />
<item
android:id="@+id/settings"
android:checkable="false"
android:orderInCategory="10"
android:title="@string/settings"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/action_theme"
android:orderInCategory="15"
android:title="@string/choose_theme_text" />
<item
android:id="@+id/action_language"
android:orderInCategory="16"
android:title="@string/choose_language_text" />
<item
android:id="@+id/action_screenOn"
android:checkable="true"
android:orderInCategory="20"
android:title="@string/keep_screen_on" />
</menu> </menu>

View File

@@ -14,4 +14,5 @@
<string name="german">Detusch</string> <string name="german">Detusch</string>
<string name="light">Hell</string> <string name="light">Hell</string>
<string name="dark">Dunkel</string> <string name="dark">Dunkel</string>
<string name="settings">Einstellungen</string>
</resources> </resources>

View File

@@ -0,0 +1,25 @@
<resources>
<!-- Theme Preference -->
<string-array name="theme_entries">
<item>@string/dark</item>
<item>@string/light</item>
<item>@string/android_default_text</item>
</string-array>
<string-array name="theme_values">
<item>dark</item>
<item>light</item>
<item>default</item>
</string-array>
<!-- Language Preference -->
<string-array name="language_entries">
<item>@string/german</item>
<item>@string/english</item>
</string-array>
<string-array name="language_values">
<item>german</item>
<item>english</item>
</string-array>
</resources>

View File

@@ -18,4 +18,21 @@
<string name="german">German</string> <string name="german">German</string>
<string name="light">Light</string> <string name="light">Light</string>
<string name="dark">Dark</string> <string name="dark">Dark</string>
<string name="title_activity_settings">SettingsActivity</string>
<!-- Preference Titles -->
<string name="messages_header">Messages</string>
<string name="sync_header">Sync</string>
<!-- Messages Preferences -->
<string name="signature_title">Your signature</string>
<string name="reply_title">Theme</string>
<!-- Sync Preferences -->
<string name="sync_title">Sync email periodically</string>
<string name="attachment_title">Download incoming attachments</string>
<string name="attachment_summary_on">Automatically download attachments for incoming emails
</string>
<string name="attachment_summary_off">Only download attachments when manually requested</string>
<string name="settings">Settings</string>
</resources> </resources>

View File

@@ -1,6 +1,6 @@
<resources> <resources>
<!-- Base application theme. --> <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight"> <style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. --> <!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
@@ -9,7 +9,7 @@
</style> </style>
<style name="AppTheme.NoActionBar"> <style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item> <item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item> <item name="windowNoTitle">true</item>
</style> </style>

View File

@@ -0,0 +1,27 @@
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/messages_header">
<ListPreference
app:defaultValue="default"
app:entries="@array/theme_entries"
app:entryValues="@array/theme_values"
app:key="theme"
app:title="@string/choose_theme_text"
app:useSimpleSummaryProvider="true" />
<ListPreference
app:defaultValue="reply"
app:entries="@array/language_entries"
app:entryValues="@array/language_values"
app:key="language"
app:title="@string/choose_language_text"
app:useSimpleSummaryProvider="true" />
<SwitchPreferenceCompat
app:key="screen"
app:title="@string/keep_screen_on" />
</PreferenceCategory>
</PreferenceScreen>