e-ntfy-android/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt

135 lines
5.1 KiB
Kotlin
Raw Normal View History

2021-10-28 05:04:14 +02:00
package io.heckel.ntfy.ui
2021-10-26 02:25:54 +02:00
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
2021-10-28 05:04:14 +02:00
import android.net.Uri
2021-10-26 02:25:54 +02:00
import android.os.Build
import android.os.Bundle
2021-10-28 05:04:14 +02:00
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.viewModels
2021-10-25 20:24:44 +02:00
import androidx.appcompat.app.AppCompatActivity
2021-10-26 02:25:54 +02:00
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.recyclerview.widget.RecyclerView
2021-10-28 05:04:14 +02:00
import io.heckel.ntfy.R
2021-10-28 04:25:02 +02:00
import io.heckel.ntfy.data.Notification
import io.heckel.ntfy.data.Status
import io.heckel.ntfy.data.Subscription
import io.heckel.ntfy.data.topicShortUrl
2021-10-26 02:25:54 +02:00
import kotlin.random.Random
2021-10-28 17:45:34 +02:00
const val SUBSCRIPTION_ID = "topic_id"
2021-10-28 17:45:34 +02:00
class MainActivity : AppCompatActivity(), AddFragment.AddSubscriptionListener {
private val subscriptionsViewModel by viewModels<SubscriptionsViewModel> {
2021-10-27 22:15:59 +02:00
SubscriptionsViewModelFactory()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2021-10-28 14:28:22 +02:00
setContentView(R.layout.main_activity)
2021-10-28 17:45:34 +02:00
// Action bar
title = getString(R.string.main_action_bar_title)
supportActionBar?.setIcon(R.drawable.ntfy) // FIXME this doesn't work
// Floating action button ("+")
val fab: View = findViewById(R.id.fab)
fab.setOnClickListener {
2021-10-28 17:45:34 +02:00
onAddButtonClick()
}
// Update main list based on topicsViewModel (& its datasource/livedata)
2021-10-28 17:45:34 +02:00
val noSubscriptionsText: View = findViewById(R.id.main_no_subscriptions_text)
val adapter = SubscriptionsAdapter(this) { subscription -> onUnsubscribe(subscription) }
val mainList: RecyclerView = findViewById(R.id.main_subscriptions_list)
mainList.adapter = adapter
2021-10-28 17:45:34 +02:00
subscriptionsViewModel.list().observe(this) {
it?.let {
2021-10-27 22:15:59 +02:00
adapter.submitList(it as MutableList<Subscription>)
2021-10-28 17:45:34 +02:00
if (it.isEmpty()) {
mainList.visibility = View.GONE
noSubscriptionsText.visibility = View.VISIBLE
} else {
mainList.visibility = View.VISIBLE
noSubscriptionsText.visibility = View.GONE
}
}
2021-10-25 19:45:56 +02:00
}
// Set up notification channel
2021-10-26 02:25:54 +02:00
createNotificationChannel()
2021-10-28 17:45:34 +02:00
subscriptionsViewModel.setListener { n -> displayNotification(n) }
2021-10-26 03:14:09 +02:00
}
2021-10-26 02:25:54 +02:00
2021-10-28 05:04:14 +02:00
override fun onCreateOptionsMenu(menu: Menu): Boolean {
2021-10-28 17:45:34 +02:00
menuInflater.inflate(R.menu.main_action_bar_menu, menu)
2021-10-28 05:04:14 +02:00
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.menu_action_source -> {
2021-10-28 14:28:22 +02:00
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.main_menu_source_url))))
2021-10-28 05:04:14 +02:00
true
}
R.id.menu_action_website -> {
2021-10-28 14:28:22 +02:00
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.main_menu_website_url))))
2021-10-28 05:04:14 +02:00
true
}
else -> super.onOptionsItemSelected(item)
}
}
2021-10-28 17:45:34 +02:00
private fun onUnsubscribe(subscription: Subscription) {
subscriptionsViewModel.remove(subscription)
}
2021-10-28 17:45:34 +02:00
private fun onAddButtonClick() {
2021-10-28 04:25:02 +02:00
val newFragment = AddFragment(this)
newFragment.show(supportFragmentManager, "AddFragment")
}
2021-10-28 17:45:34 +02:00
override fun onAddSubscription(topic: String, baseUrl: String) {
2021-10-28 04:25:02 +02:00
val subscription = Subscription(Random.nextLong(), topic, baseUrl, Status.CONNECTING, 0)
2021-10-28 17:45:34 +02:00
subscriptionsViewModel.add(subscription)
}
2021-10-26 02:25:54 +02:00
private fun displayNotification(n: Notification) {
2021-10-26 18:23:41 +02:00
val channelId = getString(R.string.notification_channel_id)
val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ntfy)
2021-10-27 22:15:59 +02:00
.setContentTitle(topicShortUrl(n.subscription))
.setContentText(n.message)
2021-10-26 18:23:41 +02:00
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
with(NotificationManagerCompat.from(this)) {
notify(Random.nextInt(), notification)
}
}
2021-10-26 19:46:49 +02:00
private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = getString(R.string.notification_channel_id)
val name = getString(R.string.notification_channel_name)
val descriptionText = getString(R.string.notification_channel_name)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId, name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
}