package io.heckel.ntfy import android.app.Activity import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context import android.content.Intent import android.os.Build import android.os.Bundle import android.view.View import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.recyclerview.widget.RecyclerView import io.heckel.ntfy.add.AddTopicActivity import io.heckel.ntfy.data.* import io.heckel.ntfy.detail.DetailActivity import kotlin.random.Random const val SUBSCRIPTION_ID = "topic_id" const val TOPIC_NAME = "topic_name" const val SERVICE_BASE_URL = "base_url" class MainActivity : AppCompatActivity() { private val newSubscriptionActivityRequestCode = 1 private val subscriptionViewModel by viewModels { 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) recyclerView.adapter = adapter subscriptionViewModel.list().observe(this) { it?.let { println("new data arrived: $it") adapter.submitList(it as MutableList) } } // Set up notification channel createNotificationChannel() subscriptionViewModel.setListener { n -> displayNotification(n) } } /* Opens detail view when list item is clicked. */ private fun subscriptionOnClick(subscription: Subscription) { val intent = Intent(this, DetailActivity()::class.java) intent.putExtra(SUBSCRIPTION_ID, subscription.id) startActivity(intent) } /* Adds topic to topicList when FAB is clicked. */ private fun fabOnClick() { val intent = Intent(this, AddTopicActivity::class.java) startActivityForResult(intent, newSubscriptionActivityRequestCode) } override fun onActivityResult(requestCode: Int, resultCode: Int, intentData: Intent?) { super.onActivityResult(requestCode, resultCode, intentData) if (requestCode == newSubscriptionActivityRequestCode && resultCode == Activity.RESULT_OK) { intentData?.let { data -> val name = data.getStringExtra(TOPIC_NAME) ?: return val baseUrl = data.getStringExtra(SERVICE_BASE_URL) ?: return val subscription = Subscription(Random.nextLong(), name, baseUrl, Status.CONNECTING, 0) subscriptionViewModel.add(subscription) } } } private fun displayNotification(n: Notification) { val channelId = getString(R.string.notification_channel_id) val notification = NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ntfy) .setContentTitle(topicShortUrl(n.subscription)) .setContentText(n.message) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .build() with(NotificationManagerCompat.from(this)) { notify(Random.nextInt(), notification) } } 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) } } }