WIP default server

This commit is contained in:
Philipp Heckel 2022-02-14 22:49:06 -05:00
parent 36bc5854b3
commit 53ab3ad694
8 changed files with 127 additions and 12 deletions

View file

@ -320,6 +320,23 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas
}
}
fun getDefaultBaseUrl(): String? {
return sharedPrefs.getString(SHARED_PREFS_DEFAULT_BASE_URL, null)
}
fun setDefaultBaseUrl(baseUrl: String) {
if (baseUrl == "") {
sharedPrefs
.edit()
.remove(SHARED_PREFS_DEFAULT_BASE_URL)
.apply()
} else {
sharedPrefs.edit()
.putString(SHARED_PREFS_DEFAULT_BASE_URL, baseUrl)
.apply()
}
}
fun isGlobalMuted(): Boolean {
val mutedUntil = getGlobalMutedUntil()
return mutedUntil == 1L || (mutedUntil > 1L && mutedUntil > System.currentTimeMillis()/1000)
@ -435,6 +452,7 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas
const val SHARED_PREFS_BATTERY_OPTIMIZATIONS_REMIND_TIME = "BatteryOptimizationsRemindTime"
const val SHARED_PREFS_UNIFIED_PUSH_ENABLED = "UnifiedPushEnabled"
const val SHARED_PREFS_UNIFIED_PUSH_BASE_URL = "UnifiedPushBaseURL"
const val SHARED_PREFS_DEFAULT_BASE_URL = "DefaultBaseURL"
const val SHARED_PREFS_LAST_TOPICS = "LastTopics"
private const val LAST_TOPICS_COUNT = 3

View file

@ -7,7 +7,6 @@ import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.TypedValue
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
@ -30,6 +29,7 @@ class AddFragment : DialogFragment() {
private lateinit var repository: Repository
private lateinit var subscribeListener: SubscribeListener
private lateinit var appBaseUrl: String
private lateinit var subscribeView: View
private lateinit var loginView: View
@ -56,8 +56,6 @@ class AddFragment : DialogFragment() {
private lateinit var loginErrorText: TextView
private lateinit var loginErrorTextImage: View
private lateinit var baseUrls: List<String> // List of base URLs already used, excluding app_base_url
interface SubscribeListener {
fun onSubscribe(topic: String, baseUrl: String, instant: Boolean)
}
@ -73,6 +71,7 @@ class AddFragment : DialogFragment() {
}
// Dependencies (Fragments need a default constructor)
appBaseUrl = getString(R.string.app_base_url)
repository = Repository.getInstance(requireActivity())
// Build root view
@ -121,12 +120,15 @@ class AddFragment : DialogFragment() {
// Add baseUrl auto-complete behavior
lifecycleScope.launch(Dispatchers.IO) {
val appBaseUrl = getString(R.string.app_base_url)
baseUrls = repository.getSubscriptions()
val defaultBaseUrl = repository.getDefaultBaseUrl()
val baseUrlsRaw = repository.getSubscriptions()
.groupBy { it.baseUrl }
.map { it.key }
.filterNot { it == appBaseUrl }
.sorted()
val baseUrls = if (defaultBaseUrl != null) {
(baseUrlsRaw.filterNot { it == defaultBaseUrl } + appBaseUrl).sorted()
} else {
baseUrlsRaw.filterNot { it == appBaseUrl }.sorted()
}
val activity = activity ?: return@launch // We may have pressed "Cancel"
activity.runOnUiThread {
initBaseUrlDropdown(baseUrls, subscribeBaseUrlText, subscribeBaseUrlLayout)
@ -205,7 +207,6 @@ class AddFragment : DialogFragment() {
}
validateInputSubscribeView()
}
subscribeUseAnotherServerCheckbox.isChecked = this::baseUrls.isInitialized && baseUrls.count() == 1
// Focus topic text (keyboard is shown too, see above)
subscribeTopicText.requestFocus()
@ -365,7 +366,7 @@ class AddFragment : DialogFragment() {
return if (subscribeUseAnotherServerCheckbox.isChecked) {
subscribeBaseUrlText.text.toString()
} else {
getString(R.string.app_base_url)
return repository.getDefaultBaseUrl() ?: appBaseUrl
}
}

View file

@ -325,6 +325,28 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
}
}
// Default Base URL
val defaultBaseUrlPrefId = context?.getString(R.string.settings_advanced_default_base_url_key) ?: return
val defaultBaseUrl: EditTextPreference? = findPreference(defaultBaseUrlPrefId)
defaultBaseUrl?.text = repository.getDefaultBaseUrl() ?: ""
defaultBaseUrl?.preferenceDataStore = object : PreferenceDataStore() {
override fun putString(key: String, value: String?) {
val baseUrl = value ?: return
repository.setDefaultBaseUrl(baseUrl)
}
override fun getString(key: String, defValue: String?): String? {
return repository.getDefaultBaseUrl()
}
}
defaultBaseUrl?.summaryProvider = Preference.SummaryProvider<EditTextPreference> { pref ->
if (TextUtils.isEmpty(pref.text)) {
getString(R.string.settings_advanced_default_base_url_default_summary, appBaseUrl)
} else {
pref.text
}
}
// Broadcast enabled
val broadcastEnabledPrefId = context?.getString(R.string.settings_advanced_broadcast_key) ?: return
val broadcastEnabled: SwitchPreference? = findPreference(broadcastEnabledPrefId)

View file

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This is a slightly edited copy of the original Android project layout
to make wrapping the summary line work.
ntfy EDIT:
This is a slightly edited copy of the original Android project layout
to make wrapping the summary line work.
~ Copyright (C) 2015 The Android Open Source Project
~

View file

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
ntfy EDIT:
This is a slightly edited copy of the original Android project layout
to reduce the marginBottom of the message to something reasonable (was: 48dp).
~ Copyright (C) 2015 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License
-->
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="48dp"
android:layout_marginBottom="48dp"
android:overScrollMode="ifContentScrolls">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@android:id/message"
style="?android:attr/textAppearanceSmall"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?android:attr/textColorSecondary"/>
<EditText
android:id="@android:id/edit"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight = "48dp" />
</LinearLayout>
</ScrollView>

View file

@ -297,8 +297,13 @@
<string name="settings_unified_push_enabled_summary_off">Apps cannot use ntfy as distributor</string>
<string name="settings_unified_push_base_url_key">UnifiedPushBaseURL</string>
<string name="settings_unified_push_base_url_title">Server URL</string>
<string name="settings_unified_push_base_url_message">Set the root server URL to be used for new UnifiedPush topics here.</string>
<string name="settings_unified_push_base_url_default_summary">%1$s (default)</string>
<string name="settings_advanced_header">Advanced</string>
<string name="settings_advanced_default_base_url_key">DefaultBaseURL</string>
<string name="settings_advanced_default_base_url_title">Default Server URL</string>
<string name="settings_advanced_default_base_url_message">Set the default server URL to be used for new topics here. Topics on other hosts can still be subscribed to using the "use another server" checkbox.</string>
<string name="settings_advanced_default_base_url_default_summary">%1$s (default)</string>
<string name="settings_advanced_broadcast_key">BroadcastEnabled</string>
<string name="settings_advanced_broadcast_title">Broadcast messages</string>
<string name="settings_advanced_broadcast_summary_enabled">Apps can receive incoming notifications as broadcasts</string>

View file

@ -58,9 +58,16 @@
<EditTextPreference
app:key="@string/settings_unified_push_base_url_key"
app:title="@string/settings_unified_push_base_url_title"
app:dependency="@string/settings_unified_push_enabled_key"/>
app:dependency="@string/settings_unified_push_enabled_key"
app:dialogLayout="@layout/preference_dialog_edittext_edited"
app:dialogMessage="@string/settings_unified_push_base_url_message"/>
</PreferenceCategory>
<PreferenceCategory app:title="@string/settings_advanced_header">
<EditTextPreference
app:key="@string/settings_advanced_default_base_url_key"
app:title="@string/settings_advanced_default_base_url_title"
app:dialogLayout="@layout/preference_dialog_edittext_edited"
app:dialogMessage="@string/settings_advanced_default_base_url_message"/>
<SwitchPreference
app:key="@string/settings_advanced_broadcast_key"
app:title="@string/settings_advanced_broadcast_title"

View file

@ -6,3 +6,6 @@ Features:
Bug fixes:
* Do not attempt to download attachments if they are already expired (#135)
* Fixed crash in AddFragment as seen per stack trace in Play Console (no ticket)
Other thanks:
* Thanks to @rogeliodh for testing