add basic settings screen (+ navigation!)

This commit is contained in:
dece 2022-01-10 15:53:53 +01:00
parent 37832f1d8b
commit 70beae84e7
7 changed files with 82 additions and 21 deletions

View file

@ -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'

View file

@ -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<HistoryEntry>
suspend fun getAll(): List<HistoryEntry>
@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()
}

View file

@ -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<Preference>("home_set")?.setOnPreferenceClickListener {
lifecycleScope.launch(Dispatchers.IO) {
val lastEntry = History.getLast()
launch(Dispatchers.Main) {
if (lastEntry != null)
findPreference<EditTextPreference>("home")?.text = lastEntry.uri
else
toast(requireContext(), R.string.no_current_url)
}
}
true
}
}
}

View file

@ -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()

View file

@ -7,11 +7,9 @@
<fragment
android:id="@+id/pageViewFragment"
android:name="dev.lowrespalmtree.comet.PageViewFragment"
android:label="PageViewFragment" >
</fragment>
android:label="PageViewFragment" />
<fragment
android:id="@+id/settingsFragment"
android:name="dev.lowrespalmtree.comet.SettingsFragment"
android:label="SettingsFragment">
</fragment>
android:label="SettingsFragment" />
</navigation>

View file

@ -4,18 +4,29 @@
<string name="url">URL</string>
<string name="settings">Settings</string>
<string name="history">History</string>
<!-- Preference Titles -->
<string name="messages_header">Messages</string>
<string name="sync_header">Sync</string>
<!-- Messages Preferences -->
<!-- Preferences General -->
<string name="pref_general_header">General</string>
<string name="pref_home_title">Home page</string>
<string name="pref_home_set">Set last visited page as home page</string>
<string name="no_current_url">No current URL.</string>
<!-- Preference Protocol -->
<string name="pref_protocol_header">Protocol</string>
<string name="pref_connection_timeout_title">Connection timeout (seconds)</string>
<string name="pref_read_timeout_title">Read timeout (seconds)</string>
<!-- Newly added -->
<string name="signature_title">Your signature</string>
<string name="reply_title">Default reply action</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>
</resources>

View file

@ -1,12 +1,17 @@
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory app:title="@string/messages_header">
<PreferenceCategory app:title="@string/pref_general_header">
<EditTextPreference
app:key="signature"
app:title="@string/signature_title"
app:key="home"
app:title="@string/pref_home_title"
app:useSimpleSummaryProvider="true" />
<Preference
app:key="home_set"
app:title="@string/pref_home_set" />
<ListPreference
app:defaultValue="reply"
app:entries="@array/reply_entries"
@ -17,7 +22,23 @@
</PreferenceCategory>
<PreferenceCategory app:title="@string/sync_header">
<PreferenceCategory app:title="@string/pref_protocol_header">
<SeekBarPreference
app:key="connection_timeout"
app:title="@string/pref_connection_timeout_title"
app:seekBarIncrement="1"
app:showSeekBarValue="true"
android:max="60"
android:defaultValue="10" />
<SeekBarPreference
app:key="read_timeout"
app:title="@string/pref_read_timeout_title"
app:seekBarIncrement="1"
app:showSeekBarValue="true"
android:max="60"
android:defaultValue="10" />
<SwitchPreferenceCompat
app:key="sync"