2021-10-28 16:04:14 +13:00
|
|
|
package io.heckel.ntfy.ui
|
2021-10-26 02:01:10 +13:00
|
|
|
|
2021-10-26 13:25:54 +13:00
|
|
|
import android.app.NotificationChannel
|
|
|
|
import android.app.NotificationManager
|
|
|
|
import android.content.Context
|
2021-10-26 02:01:10 +13:00
|
|
|
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
|
2021-10-26 02:01:10 +13:00
|
|
|
import android.os.Bundle
|
2021-10-30 14:13:58 +13:00
|
|
|
import android.util.Log
|
2021-10-28 16:04:14 +13:00
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuItem
|
2021-10-26 02:01:10 +13:00
|
|
|
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
|
2021-10-26 02:01:10 +13:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-10-30 14:13:58 +13:00
|
|
|
import com.google.android.gms.tasks.OnCompleteListener
|
2021-10-28 16:04:14 +13:00
|
|
|
import io.heckel.ntfy.R
|
2021-10-26 13:25:54 +13:00
|
|
|
import kotlin.random.Random
|
2021-10-30 14:13:58 +13:00
|
|
|
import com.google.firebase.messaging.FirebaseMessaging
|
|
|
|
import io.heckel.ntfy.app.Application
|
|
|
|
import io.heckel.ntfy.data.*
|
2021-10-26 02:01:10 +13:00
|
|
|
|
2021-10-29 04:45:34 +13:00
|
|
|
class MainActivity : AppCompatActivity(), AddFragment.AddSubscriptionListener {
|
|
|
|
private val subscriptionsViewModel by viewModels<SubscriptionsViewModel> {
|
2021-10-30 14:13:58 +13:00
|
|
|
SubscriptionsViewModelFactory((application as Application).repository)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun doStuff() {
|
|
|
|
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
|
|
|
|
if (!task.isSuccessful) {
|
|
|
|
Log.w(TAG, "Fetching FCM registration token failed", task.exception)
|
|
|
|
return@OnCompleteListener
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get new FCM registration token
|
|
|
|
val token = task.result
|
|
|
|
|
|
|
|
// Log and toast
|
|
|
|
Log.d(TAG, "message token: $token")
|
|
|
|
})
|
2021-10-26 02:01:10 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2021-10-29 01:28:22 +13:00
|
|
|
setContentView(R.layout.main_activity)
|
2021-10-26 02:01:10 +13:00
|
|
|
|
2021-10-29 04:45:34 +13:00
|
|
|
// Action bar
|
|
|
|
title = getString(R.string.main_action_bar_title)
|
|
|
|
|
2021-10-27 08:55:59 +13:00
|
|
|
// Floating action button ("+")
|
|
|
|
val fab: View = findViewById(R.id.fab)
|
|
|
|
fab.setOnClickListener {
|
2021-10-30 14:13:58 +13:00
|
|
|
onSubscribeButtonClick()
|
2021-10-27 08:55:59 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update main list based on topicsViewModel (& its datasource/livedata)
|
2021-10-29 04:45:34 +13:00
|
|
|
val noSubscriptionsText: View = findViewById(R.id.main_no_subscriptions_text)
|
2021-10-30 14:13:58 +13:00
|
|
|
val adapter = SubscriptionsAdapter { subscription -> onUnsubscribe(subscription) }
|
2021-10-29 04:45:34 +13:00
|
|
|
val mainList: RecyclerView = findViewById(R.id.main_subscriptions_list)
|
|
|
|
mainList.adapter = adapter
|
2021-10-26 02:01:10 +13:00
|
|
|
|
2021-10-29 04:45:34 +13:00
|
|
|
subscriptionsViewModel.list().observe(this) {
|
2021-10-26 02:01:10 +13:00
|
|
|
it?.let {
|
2021-10-28 09:15:59 +13:00
|
|
|
adapter.submitList(it as MutableList<Subscription>)
|
2021-10-29 04:45:34 +13:00
|
|
|
if (it.isEmpty()) {
|
|
|
|
mainList.visibility = View.GONE
|
|
|
|
noSubscriptionsText.visibility = View.VISIBLE
|
|
|
|
} else {
|
|
|
|
mainList.visibility = View.VISIBLE
|
|
|
|
noSubscriptionsText.visibility = View.GONE
|
|
|
|
}
|
2021-10-26 02:01:10 +13:00
|
|
|
}
|
2021-10-26 06:45:56 +13:00
|
|
|
}
|
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 {
|
2021-10-29 04:45:34 +13:00
|
|
|
menuInflater.inflate(R.menu.main_action_bar_menu, menu)
|
2021-10-28 16:04:14 +13:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
return when (item.itemId) {
|
|
|
|
R.id.menu_action_source -> {
|
2021-10-29 01:28:22 +13:00
|
|
|
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.main_menu_source_url))))
|
2021-10-28 16:04:14 +13:00
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.menu_action_website -> {
|
2021-10-30 14:13:58 +13:00
|
|
|
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.app_base_url))))
|
2021-10-28 16:04:14 +13:00
|
|
|
true
|
|
|
|
}
|
|
|
|
else -> super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 14:13:58 +13:00
|
|
|
private fun onSubscribeButtonClick() {
|
2021-10-28 15:25:02 +13:00
|
|
|
val newFragment = AddFragment(this)
|
|
|
|
newFragment.show(supportFragmentManager, "AddFragment")
|
2021-10-26 02:01:10 +13:00
|
|
|
}
|
|
|
|
|
2021-10-30 14:13:58 +13:00
|
|
|
override fun onSubscribe(topic: String, baseUrl: String) {
|
2021-10-31 01:57:20 +13:00
|
|
|
val subscription = Subscription(id = Random.nextLong(), baseUrl = baseUrl, topic = topic, messages = 0)
|
2021-10-29 04:45:34 +13:00
|
|
|
subscriptionsViewModel.add(subscription)
|
2021-10-30 14:13:58 +13:00
|
|
|
FirebaseMessaging.getInstance().subscribeToTopic(topic) // FIXME ignores baseUrl
|
2021-10-26 02:01:10 +13:00
|
|
|
}
|
2021-10-26 13:25:54 +13:00
|
|
|
|
2021-10-30 14:13:58 +13:00
|
|
|
private fun onUnsubscribe(subscription: Subscription) {
|
|
|
|
subscriptionsViewModel.remove(subscription)
|
|
|
|
FirebaseMessaging.getInstance().unsubscribeFromTopic(subscription.topic)
|
2021-10-27 05:23:41 +13:00
|
|
|
}
|
2021-10-27 06:46:49 +13:00
|
|
|
|
2021-10-30 14:13:58 +13:00
|
|
|
companion object {
|
|
|
|
const val TAG = "NtfyMainActivity"
|
2021-10-27 06:46:49 +13:00
|
|
|
}
|
2021-10-26 02:01:10 +13:00
|
|
|
}
|