Stupid live data

This commit is contained in:
Philipp Heckel 2021-10-27 16:15:59 -04:00
parent 391b04366b
commit 43b3aec311
10 changed files with 250 additions and 215 deletions

View file

@ -14,10 +14,7 @@ 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.Status
import io.heckel.ntfy.data.Topic
import io.heckel.ntfy.data.topicShortUrl
import io.heckel.ntfy.data.topicUrl
import io.heckel.ntfy.data.*
import io.heckel.ntfy.detail.DetailActivity
import kotlin.random.Random
@ -27,8 +24,8 @@ const val TOPIC_BASE_URL = "base_url"
class MainActivity : AppCompatActivity() {
private val newTopicActivityRequestCode = 1
private val topicsViewModel by viewModels<TopicsViewModel> {
TopicsViewModelFactory()
private val topicsViewModel by viewModels<SubscriptionViewModel> {
SubscriptionsViewModelFactory()
}
override fun onCreate(savedInstanceState: Bundle?) {
@ -48,17 +45,22 @@ class MainActivity : AppCompatActivity() {
topicsViewModel.list().observe(this) {
it?.let {
adapter.submitList(it as MutableList<Topic>)
println("new data arrived: $it")
adapter.submitList(it as MutableList<Subscription>)
}
}
// Set up notification channel
createNotificationChannel()
topicsViewModel.setNotificationListener { n -> displayNotification(n) }
topicsViewModel.setListener(object : NotificationListener {
override fun onNotification(subscriptionId: Long, notification: Notification) {
displayNotification(notification)
}
})
}
/* Opens TopicDetailActivity when RecyclerView item is clicked. */
private fun topicOnClick(topic: Topic) {
private fun topicOnClick(topic: Subscription) {
val intent = Intent(this, DetailActivity()::class.java)
intent.putExtra(TOPIC_ID, topic.id)
startActivity(intent)
@ -77,7 +79,7 @@ class MainActivity : AppCompatActivity() {
intentData?.let { data ->
val name = data.getStringExtra(TOPIC_NAME) ?: return
val baseUrl = data.getStringExtra(TOPIC_BASE_URL) ?: return
val topic = Topic(Random.nextLong(), name, baseUrl, Status.CONNECTING, 0)
val topic = Subscription(Random.nextLong(), name, baseUrl, Status.CONNECTING, 0)
topicsViewModel.add(topic)
}
@ -88,7 +90,7 @@ class MainActivity : AppCompatActivity() {
val channelId = getString(R.string.notification_channel_id)
val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ntfy)
.setContentTitle(topicShortUrl(n.topic))
.setContentTitle(topicShortUrl(n.subscription))
.setContentText(n.message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()

View file

@ -0,0 +1,67 @@
package io.heckel.ntfy
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import io.heckel.ntfy.data.*
import kotlin.collections.List
class SubscriptionViewModel(private val repository: Repository, private val connectionManager: ConnectionManager) : ViewModel() {
fun add(topic: Subscription) {
repository.add(topic)
connectionManager.start(topic, viewModelScope)
}
fun get(id: Long) : Subscription? {
return repository.get(id)
}
fun list(): LiveData<List<Subscription>> {
return repository.list()
}
fun remove(topic: Subscription) {
repository.remove(topic)
connectionManager.stop(topic)
}
fun setListener(listener: NotificationListener) {
connectionManager.setListener(object : ConnectionListener {
override fun onStatusChanged(subcriptionId: Long, status: Status) {
println("onStatusChanged($subcriptionId, $status)")
val topic = repository.get(subcriptionId)
if (topic != null) {
println("-> old topic: $topic")
repository.update(topic.copy(status = status))
}
}
override fun onNotification(subscriptionId: Long, notification: Notification) {
println("onNotification($subscriptionId, $notification)")
val topic = repository.get(subscriptionId)
if (topic != null) {
println("-> old topic: $topic")
repository.update(topic.copy(messages = topic.messages + 1))
}
listener.onNotification(subscriptionId, notification) // Forward downstream
}
})
}
}
class SubscriptionsViewModelFactory : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>) =
with(modelClass){
when {
isAssignableFrom(SubscriptionViewModel::class.java) -> {
val repository = Repository.getInstance()
val connectionManager = ConnectionManager.getInstance()
SubscriptionViewModel(repository, connectionManager) as T
}
else -> throw IllegalArgumentException("Unknown viewModel class $modelClass")
}
}
}

View file

@ -9,16 +9,16 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import io.heckel.ntfy.data.Status
import io.heckel.ntfy.data.Topic
import io.heckel.ntfy.data.Subscription
import io.heckel.ntfy.data.topicUrl
class TopicsAdapter(private val onClick: (Topic) -> Unit) :
ListAdapter<Topic, TopicsAdapter.TopicViewHolder>(TopicDiffCallback) {
class TopicsAdapter(private val onClick: (Subscription) -> Unit) :
ListAdapter<Subscription, TopicsAdapter.TopicViewHolder>(TopicDiffCallback) {
/* ViewHolder for Topic, takes in the inflated view and the onClick behavior. */
class TopicViewHolder(itemView: View, val onClick: (Topic) -> Unit) :
class TopicViewHolder(itemView: View, val onClick: (Subscription) -> Unit) :
RecyclerView.ViewHolder(itemView) {
private var topic: Topic? = null
private var topic: Subscription? = null
private val context: Context = itemView.context
private val nameView: TextView = itemView.findViewById(R.id.topic_text)
private val statusView: TextView = itemView.findViewById(R.id.topic_status)
@ -31,18 +31,19 @@ class TopicsAdapter(private val onClick: (Topic) -> Unit) :
}
}
fun bind(topic: Topic) {
this.topic = topic
val statusText = when (topic.status) {
fun bind(subscription: Subscription) {
println("bind sub: $subscription")
this.topic = subscription
val statusText = when (subscription.status) {
Status.CONNECTING -> context.getString(R.string.status_connecting)
else -> context.getString(R.string.status_subscribed)
else -> context.getString(R.string.status_connected)
}
val statusMessage = if (topic.messages == 1) {
context.getString(R.string.status_text_one, statusText, topic.messages)
val statusMessage = if (subscription.messages == 1) {
context.getString(R.string.status_text_one, statusText, subscription.messages)
} else {
context.getString(R.string.status_text_not_one, statusText, topic.messages)
context.getString(R.string.status_text_not_one, statusText, subscription.messages)
}
nameView.text = topicUrl(topic)
nameView.text = topicUrl(subscription)
statusView.text = statusMessage
}
}
@ -61,12 +62,14 @@ class TopicsAdapter(private val onClick: (Topic) -> Unit) :
}
}
object TopicDiffCallback : DiffUtil.ItemCallback<Topic>() {
override fun areItemsTheSame(oldItem: Topic, newItem: Topic): Boolean {
object TopicDiffCallback : DiffUtil.ItemCallback<Subscription>() {
override fun areItemsTheSame(oldItem: Subscription, newItem: Subscription): Boolean {
println("areItemsTheSame: $oldItem.id ==? $newItem.id")
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Topic, newItem: Topic): Boolean {
override fun areContentsTheSame(oldItem: Subscription, newItem: Subscription): Boolean {
println("areContentsTheSame: $oldItem ==? $newItem")
return oldItem == newItem
}
}

View file

@ -1,45 +0,0 @@
package io.heckel.ntfy
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import io.heckel.ntfy.data.Repository
import io.heckel.ntfy.data.Topic
import kotlin.collections.List
data class Notification(val topic: Topic, val message: String)
typealias NotificationListener = (notification: Notification) -> Unit
class TopicsViewModel(private val repository: Repository) : ViewModel() {
fun add(topic: Topic) {
repository.add(topic, viewModelScope)
}
fun get(id: Long) : Topic? {
return repository.get(id)
}
fun list(): LiveData<List<Topic>> {
return repository.list()
}
fun remove(topic: Topic) {
repository.remove(topic)
}
fun setNotificationListener(listener: NotificationListener) {
repository.setNotificationListener(listener)
}
}
class TopicsViewModelFactory() : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>) =
with(modelClass){
when {
isAssignableFrom(TopicsViewModel::class.java) -> TopicsViewModel(Repository.getInstance()) as T
else -> throw IllegalArgumentException("Unknown viewModel class $modelClass")
}
}
}

View file

@ -0,0 +1,84 @@
package io.heckel.ntfy.data
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.google.gson.JsonSyntaxException
import kotlinx.coroutines.*
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
const val READ_TIMEOUT = 60_000 // Keep alive every 30s assumed
class ConnectionManager {
private val jobs = mutableMapOf<Long, Job>()
private val gson = GsonBuilder().create()
private var listener: ConnectionListener? = null;
fun start(subscription: Subscription, scope: CoroutineScope) {
jobs[subscription.id] = launchConnection(subscription, scope)
}
fun stop(subscription: Subscription) {
jobs.remove(subscription.id)?.cancel() // Cancel coroutine and remove
}
fun setListener(listener: ConnectionListener) {
this.listener = listener
}
private fun launchConnection(subscription: Subscription, scope: CoroutineScope): Job {
return scope.launch(Dispatchers.IO) {
while (isActive) {
openConnection(this, subscription)
delay(5000) // TODO exponential back-off
}
}
}
private fun openConnection(scope: CoroutineScope, subscription: Subscription) {
val url = "${subscription.baseUrl}/${subscription.topic}/json"
println("Connecting to $url ...")
val conn = (URL(url).openConnection() as HttpURLConnection).also {
it.doInput = true
it.readTimeout = READ_TIMEOUT
}
try {
listener?.onStatusChanged(subscription.id, Status.CONNECTED)
val input = conn.inputStream.bufferedReader()
while (scope.isActive) {
val line = input.readLine() ?: break // Break if EOF is reached, i.e. readLine is null
if (!scope.isActive) {
break // Break if scope is not active anymore; readLine blocks for a while, so we want to be sure
}
try {
val json = gson.fromJson(line, JsonObject::class.java) ?: break // Break on unexpected line
if (!json.isJsonNull && !json.has("event") && json.has("message")) {
val message = json.get("message").asString
listener?.onNotification(subscription.id, Notification(subscription, message))
}
} catch (e: JsonSyntaxException) {
break // Break on unexpected line
}
}
} catch (e: IOException) {
println("Connection error: " + e.message)
} finally {
conn.disconnect()
}
listener?.onStatusChanged(subscription.id, Status.CONNECTING)
println("Connection terminated: $url")
}
companion object {
private var instance: ConnectionManager? = null
fun getInstance(): ConnectionManager {
return synchronized(ConnectionManager::class) {
val newInstance = instance ?: ConnectionManager()
instance = newInstance
newInstance
}
}
}
}

View file

@ -0,0 +1,29 @@
package io.heckel.ntfy.data
enum class Status {
CONNECTED, CONNECTING
}
data class Subscription(
val id: Long, // Internal ID, only used in Repository and activities
val topic: String,
val baseUrl: String,
val status: Status,
val messages: Int
)
data class Notification(
val subscription: Subscription,
val message: String
)
interface NotificationListener {
fun onNotification(subscriptionId: Long, notification: Notification)
}
interface ConnectionListener : NotificationListener {
fun onStatusChanged(subcriptionId: Long, status: Status)
}
fun topicUrl(s: Subscription) = "${s.baseUrl}/${s.topic}"
fun topicShortUrl(s: Subscription) = topicUrl(s).replace("http://", "").replace("https://", "")

View file

@ -2,133 +2,43 @@ package io.heckel.ntfy.data
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.google.gson.JsonSyntaxException
import io.heckel.ntfy.Notification
import io.heckel.ntfy.NotificationListener
import kotlinx.coroutines.*
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
const val READ_TIMEOUT = 60_000 // Keep alive every 30s assumed
class Repository {
private val topics: MutableLiveData<List<Topic>> = MutableLiveData(mutableListOf())
private val jobs = mutableMapOf<Long, Job>()
private val gson = GsonBuilder().create()
private var notificationListener: NotificationListener? = null;
private val subscriptions = mutableListOf<Subscription>()
private val subscriptionsLiveData: MutableLiveData<List<Subscription>> = MutableLiveData(subscriptions)
fun add(topic: Topic, scope: CoroutineScope) {
val currentList = topics.value
if (currentList == null) {
topics.postValue(listOf(topic))
} else {
val updatedList = currentList.toMutableList()
updatedList.add(0, topic)
topics.postValue(updatedList)
fun add(subscription: Subscription) {
synchronized(subscriptions) {
subscriptions.add(subscription)
subscriptionsLiveData.postValue(ArrayList(subscriptions)) // Copy!
}
jobs[topic.id] = subscribeTopic(topic, scope)
}
fun update(topic: Topic) {
val currentList = topics.value
if (currentList == null) {
topics.postValue(listOf(topic))
} else {
val index = currentList.indexOfFirst { it.id == topic.id } // Find index by Topic ID
if (index == -1) {
return // TODO race?
} else {
val updatedList = currentList.toMutableList()
updatedList[index] = topic
println("PHIL updated list:")
println(updatedList)
topics.postValue(updatedList)
fun update(subscription: Subscription) {
synchronized(subscriptions) {
val index = subscriptions.indexOfFirst { it.id == subscription.id } // Find index by Topic ID
if (index == -1) return
subscriptions[index] = subscription
subscriptionsLiveData.postValue(ArrayList(subscriptions)) // Copy!
}
}
fun remove(subscription: Subscription) {
synchronized(subscriptions) {
if (subscriptions.remove(subscription)) {
subscriptionsLiveData.postValue(ArrayList(subscriptions)) // Copy!
}
}
}
fun remove(topic: Topic) {
val currentList = topics.value
if (currentList != null) {
val updatedList = currentList.toMutableList()
updatedList.remove(topic)
topics.postValue(updatedList)
}
jobs.remove(topic.id)?.cancel() // Cancel coroutine and remove
}
fun get(id: Long): Topic? {
topics.value?.let { topics ->
return topics.firstOrNull{ it.id == id}
}
return null
}
fun list(): LiveData<List<Topic>> {
return topics
}
fun setNotificationListener(listener: NotificationListener) {
notificationListener = listener
}
private fun subscribeTopic(topic: Topic, scope: CoroutineScope): Job {
return scope.launch(Dispatchers.IO) {
while (isActive) {
openConnection(this, topic)
delay(5000) // TODO exponential back-off
}
fun get(id: Long): Subscription? {
synchronized(subscriptions) {
return subscriptions.firstOrNull { it.id == id } // Find index by Topic ID
}
}
private fun openConnection(scope: CoroutineScope, topic: Topic) {
val url = "${topic.baseUrl}/${topic.name}/json"
println("Connecting to $url ...")
val conn = (URL(url).openConnection() as HttpURLConnection).also {
it.doInput = true
it.readTimeout = READ_TIMEOUT
}
// TODO ugly
val currentTopic = get(topic.id)
if (currentTopic != null) {
update(currentTopic.copy(status = Status.SUBSCRIBED))
}
try {
val input = conn.inputStream.bufferedReader()
while (scope.isActive) {
val line = input.readLine() ?: break // Break if EOF is reached, i.e. readLine is null
if (!scope.isActive) {
break // Break if scope is not active anymore; readLine blocks for a while, so we want to be sure
}
try {
val json = gson.fromJson(line, JsonObject::class.java) ?: break // Break on unexpected line
if (!json.isJsonNull && json.has("message")) {
val message = json.get("message").asString
notificationListener?.let { it(Notification(topic, message)) }
// TODO ugly
val currentTopic = get(topic.id)
if (currentTopic != null) {
update(currentTopic.copy(messages = currentTopic.messages+1))
}
}
} catch (e: JsonSyntaxException) {
break // Break on unexpected line
}
}
} catch (e: IOException) {
println("Connection error: " + e.message)
} finally {
conn.disconnect()
}
val currentTopic2 = get(topic.id)
if (currentTopic2 != null) {
update(currentTopic2.copy(status = Status.CONNECTING))
}
println("Connection terminated: $url")
fun list(): LiveData<List<Subscription>> {
return subscriptionsLiveData
}
companion object {

View file

@ -1,16 +0,0 @@
package io.heckel.ntfy.data
enum class Status {
SUBSCRIBED, CONNECTING
}
data class Topic(
val id: Long, // Internal ID, only used in Repository and activities
val name: String,
val baseUrl: String,
val status: Status,
val messages: Int
)
fun topicUrl(t: Topic) = "${t.baseUrl}/${t.name}"
fun topicShortUrl(t: Topic) = topicUrl(t).replace("http://", "").replace("https://", "")

View file

@ -23,19 +23,20 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import io.heckel.ntfy.R
import io.heckel.ntfy.TOPIC_ID
import io.heckel.ntfy.TopicsViewModel
import io.heckel.ntfy.TopicsViewModelFactory
import io.heckel.ntfy.SubscriptionViewModel
import io.heckel.ntfy.SubscriptionsViewModelFactory
import io.heckel.ntfy.data.topicShortUrl
class DetailActivity : AppCompatActivity() {
private val topicsViewModel by viewModels<TopicsViewModel> {
TopicsViewModelFactory()
private val subscriptionsViewModel by viewModels<SubscriptionViewModel> {
SubscriptionsViewModelFactory()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.topic_detail_activity)
var topicId: Long? = null
var subscriptionId: Long? = null
/* Connect variables to UI elements. */
val topicText: TextView = findViewById(R.id.topic_detail_url)
@ -43,20 +44,20 @@ class DetailActivity : AppCompatActivity() {
val bundle: Bundle? = intent.extras
if (bundle != null) {
topicId = bundle.getLong(TOPIC_ID)
subscriptionId = bundle.getLong(TOPIC_ID)
}
// TODO This should probably fail hard if topicId is null
/* If currentTopicId is not null, get corresponding topic and set name, image and
description */
topicId?.let {
val topic = topicsViewModel.get(it)
topicText.text = "${topic?.baseUrl}/${topic?.name}"
subscriptionId?.let {
val subscription = subscriptionsViewModel.get(it)
topicText.text = subscription?.let { s -> topicShortUrl(s) }
removeButton.setOnClickListener {
if (topic != null) {
topicsViewModel.remove(topic)
if (subscription != null) {
subscriptionsViewModel.remove(subscription)
}
finish()
}

View file

@ -2,12 +2,12 @@
<string name="app_name">Ntfy</string>
<string name="add_topic">Add Topic</string>
<string name="topic_string">Topics</string>
<string name="topic_name_edit_text">Topic Name</string>
<string name="topic_base_url_edit_text">Service URL</string>
<string name="topic_base_url_default_value">https://ntfy.sh</string>
<string name="subscribe_button_text">Subscribe</string>
<string name="status_subscribed">Subscribed</string>
<string name="status_connected">Connected</string>
<string name="status_connecting">Connecting</string>
<string name="status_text_one">%1$s, %2$d notification</string>
<string name="status_text_not_one">%1$s, %2$d notifications</string>