Remove clear; add force refresh all

This commit is contained in:
Philipp Heckel 2021-11-10 21:16:00 -05:00
parent 14946b64e6
commit 9cdc73592c
8 changed files with 28 additions and 27 deletions

View file

@ -70,7 +70,7 @@ interface NotificationDao {
@Query("SELECT id FROM notification WHERE subscriptionId = :subscriptionId") @Query("SELECT id FROM notification WHERE subscriptionId = :subscriptionId")
fun listIds(subscriptionId: Long): List<String> fun listIds(subscriptionId: Long): List<String>
@Insert @Insert(onConflict = OnConflictStrategy.IGNORE)
fun add(notification: Notification) fun add(notification: Notification)
@Query("DELETE FROM notification WHERE id = :notificationId") @Query("DELETE FROM notification WHERE id = :notificationId")

View file

@ -1,6 +1,5 @@
package io.heckel.ntfy.ui package io.heckel.ntfy.ui
import android.R.attr.label
import android.app.AlertDialog import android.app.AlertDialog
import android.content.ClipData import android.content.ClipData
import android.content.ClipboardManager import android.content.ClipboardManager
@ -30,7 +29,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.util.* import java.util.*
class DetailActivity : AppCompatActivity(), ActionMode.Callback { class DetailActivity : AppCompatActivity(), ActionMode.Callback {
private val viewModel by viewModels<DetailViewModel> { private val viewModel by viewModels<DetailViewModel> {
DetailViewModelFactory((application as Application).repository) DetailViewModelFactory((application as Application).repository)
@ -115,10 +113,6 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback {
onRefreshClick() onRefreshClick()
true true
} }
R.id.detail_menu_clear -> {
onClearClick()
true
}
R.id.detail_menu_unsubscribe -> { R.id.detail_menu_unsubscribe -> {
onDeleteClick() onDeleteClick()
true true
@ -166,18 +160,6 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback {
api.poll(subscriptionId, subscriptionBaseUrl, subscriptionTopic, successFn, failureFn) api.poll(subscriptionId, subscriptionBaseUrl, subscriptionTopic, successFn, failureFn)
} }
private fun onClearClick() {
val builder = AlertDialog.Builder(this)
builder
.setMessage(R.string.detail_clear_dialog_message)
.setPositiveButton(R.string.detail_clear_dialog_permanently_delete) { _, _ ->
viewModel.removeAll(subscriptionId)
}
.setNegativeButton(R.string.detail_clear_dialog_cancel) { _, _ -> /* Do nothing */ }
.create()
.show()
}
private fun onDeleteClick() { private fun onDeleteClick() {
Log.d(TAG, "Deleting subscription ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}") Log.d(TAG, "Deleting subscription ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")

View file

@ -12,6 +12,7 @@ import android.widget.Toast
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.asFlow
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.messaging.FirebaseMessaging import com.google.firebase.messaging.FirebaseMessaging
@ -22,6 +23,7 @@ import io.heckel.ntfy.data.Subscription
import io.heckel.ntfy.data.topicShortUrl import io.heckel.ntfy.data.topicShortUrl
import io.heckel.ntfy.msg.ApiService import io.heckel.ntfy.msg.ApiService
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.util.* import java.util.*
import kotlin.random.Random import kotlin.random.Random
@ -83,6 +85,10 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback {
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) { return when (item.itemId) {
R.id.main_menu_refresh -> {
refreshAllSubscriptions()
true
}
R.id.main_menu_source -> { R.id.main_menu_source -> {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.main_menu_source_url)))) startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.main_menu_source_url))))
true true
@ -152,6 +158,24 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback {
} }
} }
private fun refreshAllSubscriptions() {
lifecycleScope.launch(Dispatchers.IO) {
val successFn = { notifications: List<Notification> ->
lifecycleScope.launch(Dispatchers.IO) {
notifications.forEach {
repository.addNotification(it)
}
}
Unit
}
repository.getAllSubscriptions().asFlow().collect { subscriptions ->
subscriptions.forEach { subscription ->
api.poll(subscription.id, subscription.baseUrl, subscription.topic, successFn, { _ -> })
}
}
}
}
private fun startDetailView(subscription: Subscription) { private fun startDetailView(subscription: Subscription) {
Log.d(TAG, "Entering detail view for subscription $subscription") Log.d(TAG, "Entering detail view for subscription $subscription")

View file

@ -1,6 +1,5 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/detail_menu_test" android:title="@string/detail_menu_test"/> <item android:id="@+id/detail_menu_test" android:title="@string/detail_menu_test"/>
<item android:id="@+id/detail_menu_refresh" android:title="@string/detail_menu_refresh"/> <item android:id="@+id/detail_menu_refresh" android:title="@string/detail_menu_refresh"/>
<item android:id="@+id/detail_menu_clear" android:title="@string/detail_menu_clear"/>
<item android:id="@+id/detail_menu_unsubscribe" android:title="@string/detail_menu_unsubscribe"/> <item android:id="@+id/detail_menu_unsubscribe" android:title="@string/detail_menu_unsubscribe"/>
</menu> </menu>

View file

@ -1,4 +1,5 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/main_menu_refresh" android:title="@string/main_menu_refresh"/>
<item android:id="@+id/main_menu_source" android:title="@string/main_menu_source_title"/> <item android:id="@+id/main_menu_source" android:title="@string/main_menu_source_title"/>
<item android:id="@+id/main_menu_website" android:title="@string/main_menu_website_title"/> <item android:id="@+id/main_menu_website" android:title="@string/main_menu_website_title"/>
</menu> </menu>

View file

@ -9,6 +9,7 @@
<!-- Main activity: Action bar --> <!-- Main activity: Action bar -->
<string name="main_action_bar_title">Subscribed topics</string> <string name="main_action_bar_title">Subscribed topics</string>
<string name="main_menu_refresh">Force refresh</string>
<string name="main_menu_source_title">Show source &amp; license</string> <string name="main_menu_source_title">Show source &amp; license</string>
<string name="main_menu_source_url">https://heckel.io/ntfy-android</string> <string name="main_menu_source_url">https://heckel.io/ntfy-android</string>
<string name="main_menu_website_title">Visit ntfy.sh</string> <string name="main_menu_website_title">Visit ntfy.sh</string>
@ -20,8 +21,6 @@
<string name="main_action_mode_delete_dialog_cancel">Cancel</string> <string name="main_action_mode_delete_dialog_cancel">Cancel</string>
<!-- Main activity: List and such --> <!-- Main activity: List and such -->
<string name="main_item_status_connecting">connecting …</string>
<string name="main_item_status_reconnecting">reconnecting …</string>
<string name="main_item_status_text_one">%1$d notification received</string> <string name="main_item_status_text_one">%1$d notification received</string>
<string name="main_item_status_text_not_one">%1$d notifications received</string> <string name="main_item_status_text_not_one">%1$d notifications received</string>
<string name="main_add_button_description">Add subscription</string> <string name="main_add_button_description">Add subscription</string>
@ -42,9 +41,6 @@
<string name="detail_how_to_intro">To send notifications to this topic, simply PUT or POST to the topic URL.</string> <string name="detail_how_to_intro">To send notifications to this topic, simply PUT or POST to the topic URL.</string>
<string name="detail_how_to_example"><![CDATA[ Example (using curl):<br/><tt>$ curl -d \"Hi\" %1$s</tt> ]]></string> <string name="detail_how_to_example"><![CDATA[ Example (using curl):<br/><tt>$ curl -d \"Hi\" %1$s</tt> ]]></string>
<string name="detail_how_to_link">For more detailed instructions, check out the ntfy.sh website and documentation.</string> <string name="detail_how_to_link">For more detailed instructions, check out the ntfy.sh website and documentation.</string>
<string name="detail_clear_dialog_message">Do you really want to delete all of the messages you received?</string>
<string name="detail_clear_dialog_permanently_delete">Permanently delete</string>
<string name="detail_clear_dialog_cancel">Cancel</string>
<string name="detail_delete_dialog_message">Do you really want to unsubscribe from this topic and delete all of the messages you received?</string> <string name="detail_delete_dialog_message">Do you really want to unsubscribe from this topic and delete all of the messages you received?</string>
<string name="detail_delete_dialog_permanently_delete">Permanently delete</string> <string name="detail_delete_dialog_permanently_delete">Permanently delete</string>
<string name="detail_delete_dialog_cancel">Cancel</string> <string name="detail_delete_dialog_cancel">Cancel</string>
@ -57,8 +53,7 @@
<!-- Detail activity: Action bar --> <!-- Detail activity: Action bar -->
<string name="detail_menu_test">Send test notification</string> <string name="detail_menu_test">Send test notification</string>
<string name="detail_menu_refresh">Fetch cached notifications</string> <string name="detail_menu_refresh">Force refresh</string>
<string name="detail_menu_clear">Clear notifications</string>
<string name="detail_menu_unsubscribe">Unsubscribe</string> <string name="detail_menu_unsubscribe">Unsubscribe</string>
<!-- Detail activity: Action mode --> <!-- Detail activity: Action mode -->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 123 KiB

After

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1,022 KiB

After

Width:  |  Height:  |  Size: 224 KiB