2021-10-31 20:19:25 +01:00
|
|
|
package io.heckel.ntfy.ui
|
|
|
|
|
|
|
|
import android.app.AlertDialog
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
2021-11-01 17:12:36 +01:00
|
|
|
import android.text.Html
|
2021-11-01 14:57:05 +01:00
|
|
|
import android.util.Log
|
2021-11-03 18:56:08 +01:00
|
|
|
import android.view.ActionMode
|
2021-10-31 20:19:25 +01:00
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
2021-11-01 17:12:36 +01:00
|
|
|
import android.widget.TextView
|
2021-11-01 14:57:05 +01:00
|
|
|
import android.widget.Toast
|
2021-10-31 20:19:25 +01:00
|
|
|
import androidx.activity.viewModels
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2021-11-03 18:56:08 +01:00
|
|
|
import androidx.core.content.ContextCompat
|
2021-11-07 19:13:32 +01:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2021-10-31 20:19:25 +01:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-11-07 19:13:32 +01:00
|
|
|
import com.android.volley.VolleyError
|
2021-11-01 14:57:05 +01:00
|
|
|
import com.android.volley.toolbox.StringRequest
|
|
|
|
import com.android.volley.toolbox.Volley
|
2021-10-31 20:19:25 +01:00
|
|
|
import io.heckel.ntfy.R
|
|
|
|
import io.heckel.ntfy.app.Application
|
|
|
|
import io.heckel.ntfy.data.Notification
|
|
|
|
import io.heckel.ntfy.data.topicShortUrl
|
2021-11-01 14:57:05 +01:00
|
|
|
import io.heckel.ntfy.data.topicUrl
|
2021-11-07 19:13:32 +01:00
|
|
|
import io.heckel.ntfy.msg.ApiService
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.flow.collect
|
|
|
|
import kotlinx.coroutines.launch
|
2021-11-01 14:57:05 +01:00
|
|
|
import java.util.*
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
|
2021-11-03 18:56:08 +01:00
|
|
|
class DetailActivity : AppCompatActivity(), ActionMode.Callback {
|
2021-10-31 20:19:25 +01:00
|
|
|
private val viewModel by viewModels<DetailViewModel> {
|
|
|
|
DetailViewModelFactory((application as Application).repository)
|
|
|
|
}
|
2021-11-07 19:13:32 +01:00
|
|
|
private val repository by lazy { (application as Application).repository }
|
|
|
|
private lateinit var api: ApiService // Context-dependent
|
2021-11-03 18:56:08 +01:00
|
|
|
|
|
|
|
// Which subscription are we looking at
|
2021-10-31 20:19:25 +01:00
|
|
|
private var subscriptionId: Long = 0L // Set in onCreate()
|
2021-11-01 14:57:05 +01:00
|
|
|
private var subscriptionBaseUrl: String = "" // Set in onCreate()
|
2021-10-31 20:19:25 +01:00
|
|
|
private var subscriptionTopic: String = "" // Set in onCreate()
|
|
|
|
|
2021-11-03 18:56:08 +01:00
|
|
|
// Action mode stuff
|
|
|
|
private lateinit var mainList: RecyclerView
|
|
|
|
private lateinit var adapter: DetailAdapter
|
|
|
|
private var actionMode: ActionMode? = null
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.detail_activity)
|
|
|
|
supportActionBar?.setDisplayHomeAsUpEnabled(true) // Show 'Back' button
|
|
|
|
|
2021-11-07 19:13:32 +01:00
|
|
|
// Dependencies that depend on Context
|
|
|
|
api = ApiService(this)
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
// Get extras required for the return to the main activity
|
|
|
|
subscriptionId = intent.getLongExtra(MainActivity.EXTRA_SUBSCRIPTION_ID, 0)
|
2021-11-01 14:57:05 +01:00
|
|
|
subscriptionBaseUrl = intent.getStringExtra(MainActivity.EXTRA_SUBSCRIPTION_BASE_URL) ?: return
|
2021-10-31 20:19:25 +01:00
|
|
|
subscriptionTopic = intent.getStringExtra(MainActivity.EXTRA_SUBSCRIPTION_TOPIC) ?: return
|
|
|
|
|
|
|
|
// Set title
|
|
|
|
val subscriptionBaseUrl = intent.getStringExtra(MainActivity.EXTRA_SUBSCRIPTION_BASE_URL) ?: return
|
2021-11-01 17:12:36 +01:00
|
|
|
val topicUrl = topicShortUrl(subscriptionBaseUrl, subscriptionTopic)
|
|
|
|
title = topicUrl
|
|
|
|
|
|
|
|
// Set "how to instructions"
|
|
|
|
val howToExample: TextView = findViewById(R.id.detail_how_to_example)
|
|
|
|
howToExample.linksClickable = true
|
|
|
|
|
|
|
|
val howToText = getString(R.string.detail_how_to_example, topicUrl)
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
|
|
|
|
howToExample.text = Html.fromHtml(howToText, Html.FROM_HTML_MODE_LEGACY)
|
|
|
|
} else {
|
|
|
|
howToExample.text = Html.fromHtml(howToText)
|
|
|
|
}
|
2021-10-31 20:19:25 +01:00
|
|
|
|
|
|
|
// Update main list based on viewModel (& its datasource/livedata)
|
2021-11-01 16:12:09 +01:00
|
|
|
val noEntriesText: View = findViewById(R.id.detail_no_notifications)
|
2021-11-03 18:56:08 +01:00
|
|
|
val onNotificationClick = { n: Notification -> onNotificationClick(n) }
|
|
|
|
val onNotificationLongClick = { n: Notification -> onNotificationLongClick(n) }
|
|
|
|
|
|
|
|
adapter = DetailAdapter(onNotificationClick, onNotificationLongClick)
|
|
|
|
mainList = findViewById(R.id.detail_notification_list)
|
2021-10-31 20:19:25 +01:00
|
|
|
mainList.adapter = adapter
|
|
|
|
|
|
|
|
viewModel.list(subscriptionId).observe(this) {
|
|
|
|
it?.let {
|
|
|
|
adapter.submitList(it as MutableList<Notification>)
|
|
|
|
if (it.isEmpty()) {
|
|
|
|
mainList.visibility = View.GONE
|
|
|
|
noEntriesText.visibility = View.VISIBLE
|
|
|
|
} else {
|
|
|
|
mainList.visibility = View.VISIBLE
|
|
|
|
noEntriesText.visibility = View.GONE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
|
|
menuInflater.inflate(R.menu.detail_action_bar_menu, menu)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
return when (item.itemId) {
|
2021-11-01 14:57:05 +01:00
|
|
|
R.id.detail_menu_test -> {
|
|
|
|
onTestClick()
|
|
|
|
true
|
|
|
|
}
|
2021-11-07 19:13:32 +01:00
|
|
|
R.id.detail_menu_refresh -> {
|
|
|
|
onRefreshClick()
|
|
|
|
true
|
|
|
|
}
|
2021-11-07 19:29:19 +01:00
|
|
|
R.id.detail_menu_clear -> {
|
|
|
|
onClearClick()
|
|
|
|
true
|
|
|
|
}
|
2021-11-03 17:48:13 +01:00
|
|
|
R.id.detail_menu_unsubscribe -> {
|
2021-10-31 20:19:25 +01:00
|
|
|
onDeleteClick()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else -> super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 19:29:19 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-11-07 19:13:32 +01:00
|
|
|
private fun onRefreshClick() {
|
2021-11-07 19:29:19 +01:00
|
|
|
Log.d(TAG, "Fetching cached notifications for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
|
|
|
|
|
2021-11-07 19:13:32 +01:00
|
|
|
val activity = this
|
|
|
|
val successFn = { notifications: List<Notification> ->
|
|
|
|
lifecycleScope.launch(Dispatchers.IO) {
|
|
|
|
val localNotificationIds = repository.getAllNotificationIds(subscriptionId)
|
|
|
|
val newNotifications = notifications.filterNot { localNotificationIds.contains(it.id) }
|
|
|
|
val toastMessage = if (newNotifications.isEmpty()) {
|
|
|
|
getString(R.string.detail_refresh_message_no_results)
|
|
|
|
} else {
|
|
|
|
getString(R.string.detail_refresh_message_result, newNotifications.size)
|
|
|
|
}
|
|
|
|
newNotifications.forEach { repository.addNotification(it) } // The meat!
|
|
|
|
runOnUiThread { Toast.makeText(activity, toastMessage, Toast.LENGTH_LONG).show() }
|
2021-11-01 14:57:05 +01:00
|
|
|
}
|
2021-11-07 19:13:32 +01:00
|
|
|
Unit
|
|
|
|
}
|
|
|
|
val failureFn = { error: Exception ->
|
|
|
|
Toast
|
|
|
|
.makeText(this, getString(R.string.detail_refresh_message_error, error.message), Toast.LENGTH_LONG)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
api.poll(subscriptionId, subscriptionBaseUrl, subscriptionTopic, successFn, failureFn)
|
|
|
|
}
|
|
|
|
|
2021-11-07 19:29:19 +01:00
|
|
|
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()
|
2021-11-01 14:57:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
private fun onDeleteClick() {
|
2021-11-01 14:57:05 +01:00
|
|
|
Log.d(TAG, "Deleting subscription ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
|
|
|
|
|
2021-10-31 20:19:25 +01:00
|
|
|
val builder = AlertDialog.Builder(this)
|
|
|
|
builder
|
|
|
|
.setMessage(R.string.detail_delete_dialog_message)
|
|
|
|
.setPositiveButton(R.string.detail_delete_dialog_permanently_delete) { _, _ ->
|
|
|
|
// Return to main activity
|
|
|
|
val result = Intent()
|
|
|
|
.putExtra(MainActivity.EXTRA_SUBSCRIPTION_ID, subscriptionId)
|
|
|
|
.putExtra(MainActivity.EXTRA_SUBSCRIPTION_TOPIC, subscriptionTopic)
|
|
|
|
setResult(RESULT_OK, result)
|
|
|
|
finish()
|
|
|
|
|
|
|
|
// Delete notifications
|
|
|
|
viewModel.removeAll(subscriptionId)
|
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.detail_delete_dialog_cancel) { _, _ -> /* Do nothing */ }
|
|
|
|
.create()
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onNotificationClick(notification: Notification) {
|
2021-11-03 18:56:08 +01:00
|
|
|
if (actionMode != null) {
|
|
|
|
handleActionModeClick(notification)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onNotificationLongClick(notification: Notification) {
|
|
|
|
if (actionMode == null) {
|
|
|
|
beginActionMode(notification)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun handleActionModeClick(notification: Notification) {
|
|
|
|
adapter.toggleSelection(notification.id)
|
|
|
|
if (adapter.selected.size == 0) {
|
|
|
|
finishActionMode()
|
|
|
|
} else {
|
|
|
|
actionMode!!.title = adapter.selected.size.toString()
|
|
|
|
redrawList()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
|
|
|
|
this.actionMode = mode
|
|
|
|
if (mode != null) {
|
|
|
|
mode.menuInflater.inflate(R.menu.detail_action_mode_menu, menu)
|
|
|
|
mode.title = "1" // One item selected
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
|
|
|
|
return when (item?.itemId) {
|
|
|
|
R.id.detail_action_mode_delete -> {
|
|
|
|
onMultiDeleteClick()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else -> false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onMultiDeleteClick() {
|
|
|
|
Log.d(TAG, "Showing multi-delete dialog for selected items")
|
|
|
|
|
|
|
|
val builder = AlertDialog.Builder(this)
|
|
|
|
builder
|
|
|
|
.setMessage(R.string.detail_action_mode_delete_dialog_message)
|
|
|
|
.setPositiveButton(R.string.detail_action_mode_delete_dialog_permanently_delete) { _, _ ->
|
|
|
|
adapter.selected.map { viewModel.remove(it) }
|
|
|
|
finishActionMode()
|
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.detail_action_mode_delete_dialog_cancel) { _, _ ->
|
|
|
|
finishActionMode()
|
|
|
|
}
|
|
|
|
.create()
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroyActionMode(mode: ActionMode?) {
|
|
|
|
endActionModeAndRedraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun beginActionMode(notification: Notification) {
|
|
|
|
actionMode = startActionMode(this)
|
|
|
|
adapter.selected.add(notification.id)
|
|
|
|
redrawList()
|
|
|
|
|
|
|
|
// Fade status bar color
|
|
|
|
val fromColor = ContextCompat.getColor(this, R.color.primaryColor)
|
|
|
|
val toColor = ContextCompat.getColor(this, R.color.primaryDarkColor)
|
|
|
|
fadeStatusBarColor(window, fromColor, toColor)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun finishActionMode() {
|
|
|
|
actionMode!!.finish()
|
|
|
|
endActionModeAndRedraw()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun endActionModeAndRedraw() {
|
|
|
|
actionMode = null
|
|
|
|
adapter.selected.clear()
|
|
|
|
redrawList()
|
|
|
|
|
|
|
|
// Fade status bar color
|
|
|
|
val fromColor = ContextCompat.getColor(this, R.color.primaryDarkColor)
|
|
|
|
val toColor = ContextCompat.getColor(this, R.color.primaryColor)
|
|
|
|
fadeStatusBarColor(window, fromColor, toColor)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun redrawList() {
|
|
|
|
mainList.adapter = adapter // Oh, what a hack ...
|
2021-11-01 14:57:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val TAG = "NtfyDetailActivity"
|
2021-10-31 20:19:25 +01:00
|
|
|
}
|
|
|
|
}
|