Add settings

This commit is contained in:
2019-09-25 18:59:20 +02:00
committed by Matte23
parent 0e9ce948ba
commit d3915dc020
10 changed files with 176 additions and 8 deletions

View File

@@ -47,6 +47,7 @@ dependencies {
implementation 'androidx.navigation:navigation-ui-ktx:2.1.0' implementation 'androidx.navigation:navigation-ui-ktx:2.1.0'
implementation "androidx.work:work-runtime-ktx:2.2.0" implementation "androidx.work:work-runtime-ktx:2.2.0"
implementation "androidx.room:room-runtime:2.2.0-rc01" implementation "androidx.room:room-runtime:2.2.0-rc01"
implementation 'androidx.preference:preference:1.1.0'
kapt "androidx.room:room-compiler:2.2.0-rc01" kapt "androidx.room:room-compiler:2.2.0-rc01"
implementation "androidx.room:room-ktx:2.2.0-rc01" implementation "androidx.room:room-ktx:2.2.0-rc01"
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'

View File

@@ -12,6 +12,10 @@
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:parentActivityName=".MainActivity"
android:label="@string/title_settings"></activity>
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:label="@string/app_name"> android:label="@string/app_name">
@@ -25,6 +29,7 @@
android:name=".LicensesActivity" android:name=".LicensesActivity"
android:label="@string/title_licenses" android:label="@string/title_licenses"
android:parentActivityName=".MainActivity" /> android:parentActivityName=".MainActivity" />
<receiver android:name=".BootCompleteReceiver"> <receiver android:name=".BootCompleteReceiver">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.BOOT_COMPLETED" />

View File

@@ -103,6 +103,10 @@ class MainActivity : AppCompatActivity(), CircularLetterAdapter.AdapterCallback
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle item selection // Handle item selection
return when (item.itemId) { return when (item.itemId) {
R.id.menu_main_settings -> {
startActivity(Intent(this, SettingsActivity::class.java))
true
}
R.id.menu_main_about -> { R.id.menu_main_about -> {
showInfoDialog() showInfoDialog()
true true

View File

@@ -0,0 +1,76 @@
/*
* Circolapp
* Copyright (C) 2019 Matteo Schiff
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp
import android.os.Bundle
import android.text.InputType
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.EditTextPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import net.underdesk.circolapp.works.PollWork
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.settings_activity)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
val darkThemePreference = findPreference<Preference>("dark_theme")
val listener =
Preference.OnPreferenceChangeListener { _, value ->
when (value) {
"auto" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
"enabled" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
"disabled" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
true
}
darkThemePreference?.onPreferenceChangeListener = listener
val pollIntervalPreference = findPreference<EditTextPreference>("poll_interval")
pollIntervalPreference?.setOnBindEditTextListener { editText ->
editText.inputType = InputType.TYPE_CLASS_NUMBER
}
val notificationPreference =
findPreference<SwitchPreferenceCompat>("notify_new_circulars")
val notificationPrefChangedListener =
Preference.OnPreferenceChangeListener { _, _ ->
activity?.let { PollWork.enqueue(it) }
true
}
pollIntervalPreference?.onPreferenceChangeListener = notificationPrefChangedListener
notificationPreference?.onPreferenceChangeListener = notificationPrefChangedListener
}
}
}

View File

@@ -27,6 +27,7 @@ import android.net.Uri
import android.os.Build import android.os.Build
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import androidx.preference.PreferenceManager
import androidx.work.* import androidx.work.*
import net.underdesk.circolapp.R import net.underdesk.circolapp.R
import net.underdesk.circolapp.data.AppDatabase import net.underdesk.circolapp.data.AppDatabase
@@ -46,13 +47,13 @@ class PollWork(appContext: Context, workerParams: WorkerParameters) :
private const val repeatIntervalMin: Long = 15 private const val repeatIntervalMin: Long = 15
private const val flexIntervalMin: Long = 10 private const val flexIntervalMin: Long = 10
private fun getPollWorkRequest(): PeriodicWorkRequest { private fun getPollWorkRequest(repeatInterval: Long): PeriodicWorkRequest {
val constraints = Constraints.Builder() val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED) .setRequiredNetworkType(NetworkType.CONNECTED)
.build() .build()
return PeriodicWorkRequestBuilder<PollWork>( return PeriodicWorkRequestBuilder<PollWork>(
repeatIntervalMin, repeatInterval,
TimeUnit.MINUTES, TimeUnit.MINUTES,
flexIntervalMin, flexIntervalMin,
TimeUnit.MINUTES TimeUnit.MINUTES
@@ -60,12 +61,24 @@ class PollWork(appContext: Context, workerParams: WorkerParameters) :
} }
fun enqueue(context: Context) { fun enqueue(context: Context) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
if (sharedPreferences.getBoolean("notify_new_circulars", true)) {
WorkManager.getInstance(context) WorkManager.getInstance(context)
.enqueueUniquePeriodicWork( .enqueueUniquePeriodicWork(
pollWorkName, pollWorkName,
ExistingPeriodicWorkPolicy.KEEP, ExistingPeriodicWorkPolicy.KEEP,
getPollWorkRequest() getPollWorkRequest(
sharedPreferences.getString(
"poll_interval",
null
)?.toLong() ?: repeatIntervalMin
) )
)
} else {
WorkManager.getInstance(context)
.cancelUniqueWork(pollWorkName)
}
} }
} }

View File

@@ -0,0 +1,9 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

View File

@@ -8,6 +8,9 @@
android:title="@string/menu_search" android:title="@string/menu_search"
app:actionViewClass="androidx.appcompat.widget.SearchView" app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="ifRoom" /> app:showAsAction="ifRoom" />
<item
android:id="@+id/menu_main_settings"
android:title="@string/title_settings" />
<item <item
android:id="@+id/menu_main_about" android:id="@+id/menu_main_about"
android:title="@string/menu_about" /> android:title="@string/menu_about" />

View File

@@ -0,0 +1,13 @@
<resources>
<string-array name="dark_theme_entries">
<item>Automatic</item>
<item>Enabled</item>
<item>Disabled</item>
</string-array>
<string-array name="dark_theme_values">
<item>auto</item>
<item>enabled</item>
<item>disabled</item>
</string-array>
</resources>

View File

@@ -4,10 +4,19 @@
<string name="title_favourites">Favourites</string> <string name="title_favourites">Favourites</string>
<string name="title_reminders">Reminders</string> <string name="title_reminders">Reminders</string>
<string name="title_licenses">Third party licenses</string> <string name="title_licenses">Third party licenses</string>
<string name="title_settings">Settings</string>
<string name="menu_about">About</string> <string name="menu_about">About</string>
<string name="menu_search">Search</string> <string name="menu_search">Search</string>
<string name="preferences_general_header">General</string>
<string name="preference_notifications_header">Notifications</string>
<string name="preferences_dark_theme">Dark theme</string>
<string name="preferences_notify_new_circulars">Show notifications</string>
<string name="preferences_poll_interval">Synchronization interval</string>
<string name="preferences_notify_new_circulars_summary">Show a notification when a new circular is published</string>
<string name="preferences_poll_interval_summary">How long to wait before checking if new circulars are published</string>
<string name="notification_title">Circular letter number %1$d</string> <string name="notification_title">Circular letter number %1$d</string>
<string name="notification_summary_title">New circulars published</string> <string name="notification_summary_title">New circulars published</string>
<string name="notification_summary">New circulars</string> <string name="notification_summary">New circulars</string>

View File

@@ -0,0 +1,35 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/preferences_general_header">
<ListPreference
app:defaultValue="auto"
app:entries="@array/dark_theme_entries"
app:entryValues="@array/dark_theme_values"
app:key="dark_theme"
app:title="@string/preferences_dark_theme"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/preference_notifications_header">
<SwitchPreferenceCompat
android:defaultValue="true"
android:summary="@string/preferences_notify_new_circulars_summary"
app:key="notify_new_circulars"
app:title="@string/preferences_notify_new_circulars" />
<EditTextPreference
android:defaultValue="15"
android:dependency="notify_new_circulars"
android:key="poll_interval"
android:selectAllOnFocus="true"
android:singleLine="true"
android:summary="@string/preferences_poll_interval_summary"
android:title="@string/preferences_poll_interval" />
</PreferenceCategory>
</PreferenceScreen>