Store "last 5 share topics"
This commit is contained in:
parent
3dcf4939c8
commit
8e333e55bc
7 changed files with 58 additions and 27 deletions
|
@ -12,8 +12,7 @@ class Application : Application() {
|
|||
Database.getInstance(this)
|
||||
}
|
||||
val repository by lazy {
|
||||
val sharedPrefs = applicationContext.getSharedPreferences(Repository.SHARED_PREFS_ID, Context.MODE_PRIVATE)
|
||||
val repository = Repository.getInstance(sharedPrefs, database)
|
||||
val repository = Repository.getInstance(applicationContext)
|
||||
if (repository.getRecordLogs()) {
|
||||
Log.setRecord(true)
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import androidx.annotation.WorkerThread
|
|||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.lifecycle.*
|
||||
import io.heckel.ntfy.util.Log
|
||||
import io.heckel.ntfy.util.validUrl
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
|
||||
|
@ -346,6 +347,18 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas
|
|||
return false
|
||||
}
|
||||
|
||||
fun getLastShareTopics(): List<String> {
|
||||
val topics = sharedPrefs.getString(SHARED_PREFS_LAST_TOPICS, "") ?: ""
|
||||
return topics.split("\n").filter { validUrl(it) }
|
||||
}
|
||||
|
||||
fun addLastShareTopic(topic: String) {
|
||||
val topics = (getLastShareTopics() + topic).takeLast(LAST_TOPICS_COUNT)
|
||||
sharedPrefs.edit()
|
||||
.putString(SHARED_PREFS_LAST_TOPICS, topics.joinToString(separator = "\n"))
|
||||
.apply()
|
||||
}
|
||||
|
||||
private fun toSubscriptionList(list: List<SubscriptionWithMetadata>): List<Subscription> {
|
||||
return list.map { s ->
|
||||
val connectionState = connectionStates.getOrElse(s.id) { ConnectionState.NOT_APPLICABLE }
|
||||
|
@ -422,6 +435,9 @@ 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_LAST_TOPICS = "LastTopics"
|
||||
|
||||
private const val LAST_TOPICS_COUNT = 5
|
||||
|
||||
const val MUTED_UNTIL_SHOW_ALL = 0L
|
||||
const val MUTED_UNTIL_FOREVER = 1L
|
||||
|
@ -456,7 +472,7 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas
|
|||
return getInstance(sharedPrefs, database)
|
||||
}
|
||||
|
||||
fun getInstance(sharedPrefs: SharedPreferences, database: Database): Repository {
|
||||
private fun getInstance(sharedPrefs: SharedPreferences, database: Database): Repository {
|
||||
return synchronized(Repository::class) {
|
||||
val newInstance = instance ?: Repository(sharedPrefs, database)
|
||||
instance = newInstance
|
||||
|
|
|
@ -43,9 +43,7 @@ class NotificationFragment : DialogFragment() {
|
|||
}
|
||||
|
||||
// Dependencies
|
||||
val database = Database.getInstance(requireActivity().applicationContext)
|
||||
val sharedPrefs = requireActivity().getSharedPreferences(Repository.SHARED_PREFS_ID, Context.MODE_PRIVATE)
|
||||
repository = Repository.getInstance(sharedPrefs, database)
|
||||
repository = Repository.getInstance(requireContext())
|
||||
|
||||
// Build root view
|
||||
val view = requireActivity().layoutInflater.inflate(R.layout.fragment_notification_dialog, null)
|
||||
|
|
|
@ -28,7 +28,7 @@ class ShareActivity : AppCompatActivity() {
|
|||
// File to share
|
||||
private var fileUri: Uri? = null
|
||||
|
||||
// List of base URLs used, excluding app_base_url
|
||||
// Lazy-loaded things from Repository
|
||||
private lateinit var baseUrls: List<String>
|
||||
|
||||
// UI elements
|
||||
|
@ -115,6 +115,10 @@ class ShareActivity : AppCompatActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
// Populate "last topics"
|
||||
val lastTopics = repository.getLastShareTopics()
|
||||
Log.d(TAG, "last topics: $lastTopics")
|
||||
|
||||
// Incoming intent
|
||||
val intent = intent ?: return
|
||||
if (intent.action != Intent.ACTION_SEND) return
|
||||
|
@ -229,14 +233,11 @@ class ShareActivity : AppCompatActivity() {
|
|||
topic = topic,
|
||||
user = user,
|
||||
message = message,
|
||||
title = "",
|
||||
priority = 3,
|
||||
tags = emptyList(),
|
||||
delay = "",
|
||||
body = body, // May be null
|
||||
filename = filename, // May be empty
|
||||
)
|
||||
runOnUiThread {
|
||||
repository.addLastShareTopic(topicUrl(baseUrl, topic))
|
||||
finish()
|
||||
Toast
|
||||
.makeText(this@ShareActivity, getString(R.string.share_successful), Toast.LENGTH_LONG)
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
android:paddingBottom="2dp"
|
||||
android:text="@string/share_content_title"
|
||||
android:textAlignment="viewStart"
|
||||
android:textAppearance="@style/TextAppearance.Material3.TitleMedium"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Large"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" android:paddingStart="2dp"/>
|
||||
<com.google.android.material.imageview.ShapeableImageView
|
||||
|
@ -73,9 +73,9 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:paddingTop="5dp"
|
||||
android:paddingBottom="3dp"
|
||||
android:text="Share to topic"
|
||||
android:text="@string/share_topic_title"
|
||||
android:textAlignment="viewStart"
|
||||
android:textAppearance="@style/TextAppearance.Material3.TitleMedium"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Large"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/share_content_file_box" android:layout_marginTop="15dp" android:paddingStart="2dp"/>
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
|
@ -124,6 +124,21 @@
|
|||
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||
/>
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
<TextView
|
||||
android:id="@+id/share_last_title"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="5dp"
|
||||
android:paddingBottom="3dp"
|
||||
android:text="@string/share_previous_topics"
|
||||
android:textAlignment="viewStart"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Large"
|
||||
android:paddingStart="2dp" app:layout_constraintTop_toBottomOf="@id/share_base_url_layout" app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="15dp"/>
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content" android:id="@+id/share_last_layout" app:layout_constraintTop_toBottomOf="@id/share_last_title" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="5dp">
|
||||
</LinearLayout>
|
||||
<TextView
|
||||
android:text="Unable to resolve host example.com"
|
||||
android:layout_width="0dp"
|
||||
|
@ -133,7 +148,7 @@
|
|||
android:paddingEnd="4dp"
|
||||
android:textAppearance="@style/DangerText"
|
||||
app:layout_constraintStart_toEndOf="@id/share_error_image"
|
||||
android:layout_marginTop="5dp" app:layout_constraintTop_toBottomOf="@id/share_base_url_layout"/>
|
||||
android:layout_marginTop="10dp" app:layout_constraintTop_toBottomOf="@id/share_last_title"/>
|
||||
<ImageView
|
||||
android:layout_width="20dp"
|
||||
android:layout_height="20dp" app:srcCompat="@drawable/ic_error_red_24dp"
|
||||
|
|
|
@ -195,6 +195,8 @@
|
|||
<string name="share_content_image_error">Cannot read image: %1$s</string>
|
||||
<string name="share_content_file_text">A file was shared with you</string>
|
||||
<string name="share_content_file_error">Cannot read file infos: %1$s</string>
|
||||
<string name="share_topic_title">Share to</string>
|
||||
<string name="share_previous_topics">Last topics</string>
|
||||
<string name="share_successful">Message successfully published</string>
|
||||
|
||||
<!-- Notification dialog -->
|
||||
|
|
Loading…
Reference in a new issue