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

126 lines
4.7 KiB
Kotlin
Raw Normal View History

2021-10-28 16:04:14 +13:00
package io.heckel.ntfy.ui
2021-10-26 13:25:54 +13:00
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
2021-10-28 16:04:14 +13:00
import android.net.Uri
2021-10-26 13:25:54 +13:00
import android.os.Build
import android.os.Bundle
2021-10-28 16:04:14 +13:00
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.viewModels
2021-10-26 07:24:44 +13:00
import androidx.appcompat.app.AppCompatActivity
2021-10-26 13:25:54 +13:00
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.recyclerview.widget.RecyclerView
2021-10-28 16:04:14 +13:00
import io.heckel.ntfy.R
2021-10-28 15:25:02 +13: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 13:25:54 +13:00
import kotlin.random.Random
const val SUBSCRIPTION_ID = "topic_id"
2021-10-28 15:25:02 +13:00
class MainActivity : AppCompatActivity(), AddFragment.Listener {
private val subscriptionViewModel by viewModels<SubscriptionsViewModel> {
2021-10-28 09:15:59 +13:00
SubscriptionsViewModelFactory()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Floating action button ("+")
val fab: View = findViewById(R.id.fab)
fab.setOnClickListener {
fabOnClick()
}
// Update main list based on topicsViewModel (& its datasource/livedata)
val adapter = TopicsAdapter { topic -> subscriptionOnClick(topic) }
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
2021-10-26 06:45:56 +13:00
recyclerView.adapter = adapter
subscriptionViewModel.list().observe(this) {
it?.let {
2021-10-28 09:15:59 +13:00
adapter.submitList(it as MutableList<Subscription>)
}
2021-10-26 06:45:56 +13:00
}
// Set up notification channel
2021-10-26 13:25:54 +13:00
createNotificationChannel()
subscriptionViewModel.setListener { n -> displayNotification(n) }
2021-10-26 14:14:09 +13:00
}
2021-10-26 13:25:54 +13:00
2021-10-28 16:04:14 +13:00
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.menu_action_source -> {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.menu_source_url))))
true
}
R.id.menu_action_website -> {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.menu_website_url))))
true
}
else -> super.onOptionsItemSelected(item)
}
}
/* Opens detail view when list item is clicked. */
private fun subscriptionOnClick(subscription: Subscription) {
2021-10-27 07:40:52 +13:00
val intent = Intent(this, DetailActivity()::class.java)
intent.putExtra(SUBSCRIPTION_ID, subscription.id)
startActivity(intent)
}
2021-10-26 06:45:56 +13:00
/* Adds topic to topicList when FAB is clicked. */
private fun fabOnClick() {
2021-10-28 15:25:02 +13:00
val newFragment = AddFragment(this)
newFragment.show(supportFragmentManager, "AddFragment")
}
2021-10-28 15:25:02 +13:00
override fun onAddClicked(topic: String, baseUrl: String) {
val subscription = Subscription(Random.nextLong(), topic, baseUrl, Status.CONNECTING, 0)
subscriptionViewModel.add(subscription)
}
2021-10-26 13:25:54 +13:00
private fun displayNotification(n: Notification) {
2021-10-27 05:23:41 +13:00
val channelId = getString(R.string.notification_channel_id)
val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ntfy)
2021-10-28 09:15:59 +13:00
.setContentTitle(topicShortUrl(n.subscription))
.setContentText(n.message)
2021-10-27 05:23:41 +13:00
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
with(NotificationManagerCompat.from(this)) {
notify(Random.nextInt(), notification)
}
}
2021-10-27 06:46:49 +13: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)
}
}
}