From 70beae84e780dadc47220d710c69caea7a6c7d4b Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 10 Jan 2022 15:53:53 +0100 Subject: [PATCH] add basic settings screen (+ navigation!) --- app/build.gradle | 3 +- .../java/dev/lowrespalmtree/comet/History.kt | 15 ++++++--- .../lowrespalmtree/comet/SettingsFragment.kt | 20 +++++++++++- .../java/dev/lowrespalmtree/comet/UiUtils.kt | 7 +++++ app/src/main/res/navigation/main.xml | 6 ++-- app/src/main/res/values/strings.xml | 21 ++++++++++--- app/src/main/res/xml/root_preferences.xml | 31 ++++++++++++++++--- 7 files changed, 82 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/dev/lowrespalmtree/comet/UiUtils.kt diff --git a/app/build.gradle b/app/build.gradle index 5692731..aa74573 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,10 +47,11 @@ dependencies { implementation 'androidx.fragment:fragment-ktx:1.4.0' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' - implementation "androidx.room:room-runtime:$room_version" implementation "androidx.navigation:navigation-fragment-ktx:$nav_version" implementation "androidx.navigation:navigation-ui-ktx:$nav_version" implementation 'androidx.preference:preference-ktx:1.1.1' + implementation "androidx.room:room-runtime:$room_version" + implementation "androidx.room:room-ktx:$room_version" implementation 'com.google.android.material:material:1.4.0' kapt "androidx.room:room-compiler:$room_version" testImplementation 'junit:junit:4.13.2' diff --git a/app/src/main/java/dev/lowrespalmtree/comet/History.kt b/app/src/main/java/dev/lowrespalmtree/comet/History.kt index 300c900..4fa2f05 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/History.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/History.kt @@ -13,19 +13,22 @@ object History { @Dao interface HistoryEntryDao { @Query("SELECT * FROM HistoryEntry WHERE :uri = uri LIMIT 1") - fun get(uri: String): HistoryEntry? + suspend fun get(uri: String): HistoryEntry? @Query("SELECT * FROM HistoryEntry ORDER BY lastVisit DESC") - fun getAll(): List + suspend fun getAll(): List + + @Query("SELECT * FROM HistoryEntry ORDER BY lastVisit DESC LIMIT 1") + suspend fun getLast(): HistoryEntry? @Insert(onConflict = OnConflictStrategy.IGNORE) - fun insert(vararg entries: HistoryEntry) + suspend fun insert(vararg entries: HistoryEntry) @Update - fun update(vararg entries: HistoryEntry) + suspend fun update(vararg entries: HistoryEntry) } - fun record(uri: String, title: String?) { + suspend fun record(uri: String, title: String?) { val now = System.currentTimeMillis() val dao = Database.INSTANCE.historyEntryDao() val entry = dao.get(uri) @@ -34,4 +37,6 @@ object History { else dao.update(entry.also { it.title = title; it.lastVisit = now }) } + + suspend fun getLast(): HistoryEntry? = Database.INSTANCE.historyEntryDao().getLast() } \ No newline at end of file diff --git a/app/src/main/java/dev/lowrespalmtree/comet/SettingsFragment.kt b/app/src/main/java/dev/lowrespalmtree/comet/SettingsFragment.kt index 789c228..e5882d8 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/SettingsFragment.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/SettingsFragment.kt @@ -1,11 +1,29 @@ package dev.lowrespalmtree.comet import android.os.Bundle +import android.util.Log +import androidx.lifecycle.lifecycleScope +import androidx.preference.EditTextPreference +import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat +import androidx.preference.SeekBarPreference +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch class SettingsFragment : PreferenceFragmentCompat() { - override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.root_preferences, rootKey) + findPreference("home_set")?.setOnPreferenceClickListener { + lifecycleScope.launch(Dispatchers.IO) { + val lastEntry = History.getLast() + launch(Dispatchers.Main) { + if (lastEntry != null) + findPreference("home")?.text = lastEntry.uri + else + toast(requireContext(), R.string.no_current_url) + } + } + true + } } } \ No newline at end of file diff --git a/app/src/main/java/dev/lowrespalmtree/comet/UiUtils.kt b/app/src/main/java/dev/lowrespalmtree/comet/UiUtils.kt new file mode 100644 index 0000000..622e19c --- /dev/null +++ b/app/src/main/java/dev/lowrespalmtree/comet/UiUtils.kt @@ -0,0 +1,7 @@ +package dev.lowrespalmtree.comet + +import android.content.Context +import android.widget.Toast + +fun toast(context: Context, stringId: Int, length: Int = Toast.LENGTH_SHORT) = + Toast.makeText(context, stringId, length).show() \ No newline at end of file diff --git a/app/src/main/res/navigation/main.xml b/app/src/main/res/navigation/main.xml index 4644685..db1d14f 100644 --- a/app/src/main/res/navigation/main.xml +++ b/app/src/main/res/navigation/main.xml @@ -7,11 +7,9 @@ - + android:label="PageViewFragment" /> - + android:label="SettingsFragment" /> \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5624b64..df98894 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,18 +4,29 @@ URL Settings History - - Messages - Sync - + + General + Home page + Set last visited page as home page + No current URL. + + + + Protocol + Connection timeout (seconds) + Read timeout (seconds) + + + Your signature Default reply action - Sync email periodically Download incoming attachments Automatically download attachments for incoming emails Only download attachments when manually requested + + \ No newline at end of file diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 61dfdf3..583fe0b 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -1,12 +1,17 @@ - + - + + + - + + + + +