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)
|
Database.getInstance(this)
|
||||||
}
|
}
|
||||||
val repository by lazy {
|
val repository by lazy {
|
||||||
val sharedPrefs = applicationContext.getSharedPreferences(Repository.SHARED_PREFS_ID, Context.MODE_PRIVATE)
|
val repository = Repository.getInstance(applicationContext)
|
||||||
val repository = Repository.getInstance(sharedPrefs, database)
|
|
||||||
if (repository.getRecordLogs()) {
|
if (repository.getRecordLogs()) {
|
||||||
Log.setRecord(true)
|
Log.setRecord(true)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import androidx.annotation.WorkerThread
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import androidx.lifecycle.*
|
import androidx.lifecycle.*
|
||||||
import io.heckel.ntfy.util.Log
|
import io.heckel.ntfy.util.Log
|
||||||
|
import io.heckel.ntfy.util.validUrl
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import java.util.concurrent.atomic.AtomicLong
|
import java.util.concurrent.atomic.AtomicLong
|
||||||
|
|
||||||
|
@ -346,6 +347,18 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas
|
||||||
return false
|
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> {
|
private fun toSubscriptionList(list: List<SubscriptionWithMetadata>): List<Subscription> {
|
||||||
return list.map { s ->
|
return list.map { s ->
|
||||||
val connectionState = connectionStates.getOrElse(s.id) { ConnectionState.NOT_APPLICABLE }
|
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_BATTERY_OPTIMIZATIONS_REMIND_TIME = "BatteryOptimizationsRemindTime"
|
||||||
const val SHARED_PREFS_UNIFIED_PUSH_ENABLED = "UnifiedPushEnabled"
|
const val SHARED_PREFS_UNIFIED_PUSH_ENABLED = "UnifiedPushEnabled"
|
||||||
const val SHARED_PREFS_UNIFIED_PUSH_BASE_URL = "UnifiedPushBaseURL"
|
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_SHOW_ALL = 0L
|
||||||
const val MUTED_UNTIL_FOREVER = 1L
|
const val MUTED_UNTIL_FOREVER = 1L
|
||||||
|
@ -456,7 +472,7 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas
|
||||||
return getInstance(sharedPrefs, database)
|
return getInstance(sharedPrefs, database)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getInstance(sharedPrefs: SharedPreferences, database: Database): Repository {
|
private fun getInstance(sharedPrefs: SharedPreferences, database: Database): Repository {
|
||||||
return synchronized(Repository::class) {
|
return synchronized(Repository::class) {
|
||||||
val newInstance = instance ?: Repository(sharedPrefs, database)
|
val newInstance = instance ?: Repository(sharedPrefs, database)
|
||||||
instance = newInstance
|
instance = newInstance
|
||||||
|
|
|
@ -43,9 +43,7 @@ class NotificationFragment : DialogFragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dependencies
|
// Dependencies
|
||||||
val database = Database.getInstance(requireActivity().applicationContext)
|
repository = Repository.getInstance(requireContext())
|
||||||
val sharedPrefs = requireActivity().getSharedPreferences(Repository.SHARED_PREFS_ID, Context.MODE_PRIVATE)
|
|
||||||
repository = Repository.getInstance(sharedPrefs, database)
|
|
||||||
|
|
||||||
// Build root view
|
// Build root view
|
||||||
val view = requireActivity().layoutInflater.inflate(R.layout.fragment_notification_dialog, null)
|
val view = requireActivity().layoutInflater.inflate(R.layout.fragment_notification_dialog, null)
|
||||||
|
|
|
@ -28,7 +28,7 @@ class ShareActivity : AppCompatActivity() {
|
||||||
// File to share
|
// File to share
|
||||||
private var fileUri: Uri? = null
|
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>
|
private lateinit var baseUrls: List<String>
|
||||||
|
|
||||||
// UI elements
|
// UI elements
|
||||||
|
@ -115,6 +115,10 @@ class ShareActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Populate "last topics"
|
||||||
|
val lastTopics = repository.getLastShareTopics()
|
||||||
|
Log.d(TAG, "last topics: $lastTopics")
|
||||||
|
|
||||||
// Incoming intent
|
// Incoming intent
|
||||||
val intent = intent ?: return
|
val intent = intent ?: return
|
||||||
if (intent.action != Intent.ACTION_SEND) return
|
if (intent.action != Intent.ACTION_SEND) return
|
||||||
|
@ -229,14 +233,11 @@ class ShareActivity : AppCompatActivity() {
|
||||||
topic = topic,
|
topic = topic,
|
||||||
user = user,
|
user = user,
|
||||||
message = message,
|
message = message,
|
||||||
title = "",
|
|
||||||
priority = 3,
|
|
||||||
tags = emptyList(),
|
|
||||||
delay = "",
|
|
||||||
body = body, // May be null
|
body = body, // May be null
|
||||||
filename = filename, // May be empty
|
filename = filename, // May be empty
|
||||||
)
|
)
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
|
repository.addLastShareTopic(topicUrl(baseUrl, topic))
|
||||||
finish()
|
finish()
|
||||||
Toast
|
Toast
|
||||||
.makeText(this@ShareActivity, getString(R.string.share_successful), Toast.LENGTH_LONG)
|
.makeText(this@ShareActivity, getString(R.string.share_successful), Toast.LENGTH_LONG)
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
android:paddingBottom="2dp"
|
android:paddingBottom="2dp"
|
||||||
android:text="@string/share_content_title"
|
android:text="@string/share_content_title"
|
||||||
android:textAlignment="viewStart"
|
android:textAlignment="viewStart"
|
||||||
android:textAppearance="@style/TextAppearance.Material3.TitleMedium"
|
android:textAppearance="@style/TextAppearance.AppCompat.Large"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" android:paddingStart="2dp"/>
|
app:layout_constraintTop_toTopOf="parent" android:paddingStart="2dp"/>
|
||||||
<com.google.android.material.imageview.ShapeableImageView
|
<com.google.android.material.imageview.ShapeableImageView
|
||||||
|
@ -73,9 +73,9 @@
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:paddingTop="5dp"
|
android:paddingTop="5dp"
|
||||||
android:paddingBottom="3dp"
|
android:paddingBottom="3dp"
|
||||||
android:text="Share to topic"
|
android:text="@string/share_topic_title"
|
||||||
android:textAlignment="viewStart"
|
android:textAlignment="viewStart"
|
||||||
android:textAppearance="@style/TextAppearance.Material3.TitleMedium"
|
android:textAppearance="@style/TextAppearance.AppCompat.Large"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@id/share_content_file_box" android:layout_marginTop="15dp" android:paddingStart="2dp"/>
|
app:layout_constraintTop_toBottomOf="@id/share_content_file_box" android:layout_marginTop="15dp" android:paddingStart="2dp"/>
|
||||||
<com.google.android.material.textfield.TextInputEditText
|
<com.google.android.material.textfield.TextInputEditText
|
||||||
|
@ -124,6 +124,21 @@
|
||||||
android:textAppearance="?android:attr/textAppearanceMedium"
|
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||||
/>
|
/>
|
||||||
</com.google.android.material.textfield.TextInputLayout>
|
</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
|
<TextView
|
||||||
android:text="Unable to resolve host example.com"
|
android:text="Unable to resolve host example.com"
|
||||||
android:layout_width="0dp"
|
android:layout_width="0dp"
|
||||||
|
@ -133,7 +148,7 @@
|
||||||
android:paddingEnd="4dp"
|
android:paddingEnd="4dp"
|
||||||
android:textAppearance="@style/DangerText"
|
android:textAppearance="@style/DangerText"
|
||||||
app:layout_constraintStart_toEndOf="@id/share_error_image"
|
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
|
<ImageView
|
||||||
android:layout_width="20dp"
|
android:layout_width="20dp"
|
||||||
android:layout_height="20dp" app:srcCompat="@drawable/ic_error_red_24dp"
|
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_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_text">A file was shared with you</string>
|
||||||
<string name="share_content_file_error">Cannot read file infos: %1$s</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>
|
<string name="share_successful">Message successfully published</string>
|
||||||
|
|
||||||
<!-- Notification dialog -->
|
<!-- Notification dialog -->
|
||||||
|
|
Loading…
Add table
Reference in a new issue