2021-10-25 15:01:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-10-26 20:35:51 +02:00
|
|
|
package io.heckel.ntfy
|
2021-10-25 15:01:10 +02:00
|
|
|
|
|
|
|
import android.app.Activity
|
2021-10-26 02:25:54 +02:00
|
|
|
import android.app.NotificationChannel
|
|
|
|
import android.app.NotificationManager
|
|
|
|
import android.content.Context
|
2021-10-25 15:01:10 +02:00
|
|
|
import android.content.Intent
|
2021-10-26 02:25:54 +02:00
|
|
|
import android.os.Build
|
2021-10-25 15:01:10 +02:00
|
|
|
import android.os.Bundle
|
|
|
|
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
|
2021-10-25 15:01:10 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-10-25 20:24:44 +02:00
|
|
|
import io.heckel.ntfy.add.AddTopicActivity
|
2021-10-25 15:01:10 +02:00
|
|
|
import io.heckel.ntfy.data.Topic
|
2021-10-26 20:40:52 +02:00
|
|
|
import io.heckel.ntfy.detail.DetailActivity
|
2021-10-26 21:55:59 +02:00
|
|
|
import io.heckel.ntfy.list.*
|
2021-10-26 02:25:54 +02:00
|
|
|
import kotlin.random.Random
|
2021-10-25 15:01:10 +02:00
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
const val TOPIC_ID = "topic id"
|
2021-10-26 20:40:52 +02:00
|
|
|
const val TOPIC_URL = "url"
|
2021-10-25 15:01:10 +02:00
|
|
|
|
2021-10-26 20:35:51 +02:00
|
|
|
class MainActivity : AppCompatActivity() {
|
2021-10-25 15:01:10 +02:00
|
|
|
private val newTopicActivityRequestCode = 1
|
2021-10-26 21:55:59 +02:00
|
|
|
private val topicsViewModel by viewModels<TopicsViewModel> {
|
2021-10-27 02:34:09 +02:00
|
|
|
TopicsViewModelFactory()
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_main)
|
|
|
|
|
2021-10-26 21:55:59 +02:00
|
|
|
// 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 -> topicOnClick(topic) }
|
2021-10-25 15:01:10 +02:00
|
|
|
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
|
2021-10-25 19:45:56 +02:00
|
|
|
recyclerView.adapter = adapter
|
2021-10-25 15:01:10 +02:00
|
|
|
|
2021-10-26 21:55:59 +02:00
|
|
|
topicsViewModel.list().observe(this) {
|
2021-10-25 15:01:10 +02:00
|
|
|
it?.let {
|
2021-10-25 19:45:56 +02:00
|
|
|
adapter.submitList(it as MutableList<Topic>)
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
2021-10-25 19:45:56 +02:00
|
|
|
}
|
2021-10-25 15:01:10 +02:00
|
|
|
|
2021-10-26 21:55:59 +02:00
|
|
|
// Set up notification channel
|
2021-10-26 02:25:54 +02:00
|
|
|
createNotificationChannel()
|
2021-10-26 21:55:59 +02:00
|
|
|
topicsViewModel.setNotificationListener { n -> displayNotification(n) }
|
2021-10-26 03:14:09 +02:00
|
|
|
}
|
2021-10-26 02:25:54 +02:00
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
/* Opens TopicDetailActivity when RecyclerView item is clicked. */
|
2021-10-26 21:55:59 +02:00
|
|
|
private fun topicOnClick(topic: Topic) {
|
2021-10-26 20:40:52 +02:00
|
|
|
val intent = Intent(this, DetailActivity()::class.java)
|
2021-10-25 19:45:56 +02:00
|
|
|
intent.putExtra(TOPIC_ID, topic.id)
|
2021-10-25 15:01:10 +02:00
|
|
|
startActivity(intent)
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
/* Adds topic to topicList when FAB is clicked. */
|
2021-10-25 15:01:10 +02:00
|
|
|
private fun fabOnClick() {
|
|
|
|
val intent = Intent(this, AddTopicActivity::class.java)
|
|
|
|
startActivityForResult(intent, newTopicActivityRequestCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, intentData: Intent?) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, intentData)
|
|
|
|
|
|
|
|
if (requestCode == newTopicActivityRequestCode && resultCode == Activity.RESULT_OK) {
|
|
|
|
intentData?.let { data ->
|
2021-10-26 18:23:41 +02:00
|
|
|
val topicId = Random.nextLong()
|
2021-10-26 03:14:09 +02:00
|
|
|
val topicUrl = data.getStringExtra(TOPIC_URL) ?: return
|
2021-10-26 18:23:41 +02:00
|
|
|
val topic = Topic(topicId, topicUrl)
|
|
|
|
|
2021-10-26 21:55:59 +02:00
|
|
|
topicsViewModel.add(topic)
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 02:25:54 +02:00
|
|
|
|
2021-10-26 21:55:59 +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-26 21:55:59 +02:00
|
|
|
.setContentTitle(n.topic)
|
|
|
|
.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)
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|