diff --git a/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt index 01e82ad..9bf7b2f 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt @@ -115,6 +115,10 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback { onRefreshClick() true } + R.id.detail_menu_clear -> { + onClearClick() + true + } R.id.detail_menu_unsubscribe -> { onDeleteClick() true @@ -123,7 +127,22 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback { } } + private fun onTestClick() { + Log.d(TAG, "Sending test notification to ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}") + + val message = getString(R.string.detail_test_message, Date().toString()) + val successFn = { _: String -> } + val failureFn = { error: VolleyError -> + Toast + .makeText(this, getString(R.string.detail_test_message_error, error.message), Toast.LENGTH_LONG) + .show() + } + api.publish(subscriptionBaseUrl, subscriptionTopic, message, successFn, failureFn) + } + private fun onRefreshClick() { + Log.d(TAG, "Fetching cached notifications for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}") + val activity = this val successFn = { notifications: List -> lifecycleScope.launch(Dispatchers.IO) { @@ -147,15 +166,16 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback { api.poll(subscriptionId, subscriptionBaseUrl, subscriptionTopic, successFn, failureFn) } - private fun onTestClick() { - val message = getString(R.string.detail_test_message, Date().toString()) - val successFn = { _: String -> } - val failureFn = { error: VolleyError -> - Toast - .makeText(this, getString(R.string.detail_test_message_error, error.message), Toast.LENGTH_LONG) - .show() - } - api.publish(subscriptionBaseUrl, subscriptionTopic, message, 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() { diff --git a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt index 487db92..5efe727 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt @@ -8,15 +8,21 @@ import android.net.Uri import android.os.Bundle import android.util.Log import android.view.* +import android.widget.Toast import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat +import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.RecyclerView import com.google.firebase.messaging.FirebaseMessaging import io.heckel.ntfy.R import io.heckel.ntfy.app.Application +import io.heckel.ntfy.data.Notification import io.heckel.ntfy.data.Subscription import io.heckel.ntfy.data.topicShortUrl +import io.heckel.ntfy.msg.ApiService +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import java.util.* import kotlin.random.Random @@ -24,15 +30,20 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback { private val viewModel by viewModels { SubscriptionsViewModelFactory((application as Application).repository) } + private val repository by lazy { (application as Application).repository } private lateinit var mainList: RecyclerView private lateinit var adapter: MainAdapter private lateinit var fab: View private var actionMode: ActionMode? = null + private lateinit var api: ApiService // Context-dependent override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main_activity) + // Dependencies that depend on Context + api = ApiService(this) + // Action bar title = getString(R.string.main_action_bar_title) @@ -90,8 +101,10 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback { } private fun onSubscribe(topic: String, baseUrl: String) { + // FIXME ignores baseUrl Log.d(TAG, "Adding subscription ${topicShortUrl(baseUrl, topic)}") + // Add subscription to database val subscription = Subscription( id = Random.nextLong(), baseUrl = baseUrl, @@ -100,6 +113,8 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback { lastActive = Date().time/1000 ) viewModel.add(subscription) + + // Subscribe to Firebase topic FirebaseMessaging .getInstance() .subscribeToTopic(topic) @@ -110,9 +125,17 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback { Log.e(TAG, "Subscribing to topic failed: $it") } - // FIXME ignores baseUrl + // Fetch cached messages + val successFn = { notifications: List -> + lifecycleScope.launch(Dispatchers.IO) { + notifications.forEach { repository.addNotification(it) } + } + Unit + } + api.poll(subscription.id, subscription.baseUrl, subscription.topic, successFn, { _ -> }) - onSubscriptionItemClick(subscription) // Add to detail view after adding it + // Switch to detail view after adding it + onSubscriptionItemClick(subscription) } private fun onSubscriptionItemClick(subscription: Subscription) { diff --git a/app/src/main/res/menu/detail_action_bar_menu.xml b/app/src/main/res/menu/detail_action_bar_menu.xml index 852ef14..3f3e160 100644 --- a/app/src/main/res/menu/detail_action_bar_menu.xml +++ b/app/src/main/res/menu/detail_action_bar_menu.xml @@ -1,5 +1,6 @@ + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7819b7b..86285ef 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -42,6 +42,9 @@ To send notifications to this topic, simply PUT or POST to the topic URL. $ curl -d \"Hi\" %1$s ]]> For more detailed instructions, check out the ntfy.sh website and documentation. + Do you really want to delete all of the messages you received? + Permanently delete + Cancel Do you really want to unsubscribe from this topic and delete all of the messages you received? Permanently delete Cancel @@ -52,8 +55,9 @@ Could not refresh topic: %1$s - Force refresh Send test notification + Fetch cached notifications + Clear notifications Unsubscribe