2021-10-27 13:34:09 +13:00
|
|
|
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
|
|
|
|
|
2021-10-27 15:41:19 +13:00
|
|
|
const val READ_TIMEOUT = 60_000 // Keep alive every 30s assumed
|
|
|
|
|
2021-10-27 14:44:12 +13:00
|
|
|
class Repository {
|
2021-10-27 13:34:09 +13:00
|
|
|
private val topics: MutableLiveData<List<Topic>> = MutableLiveData(mutableListOf())
|
|
|
|
private val jobs = mutableMapOf<Long, Job>()
|
|
|
|
private val gson = GsonBuilder().create()
|
|
|
|
private var notificationListener: NotificationListener? = null;
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
jobs[topic.id] = subscribeTopic(topic, scope)
|
|
|
|
}
|
|
|
|
|
2021-10-27 15:41:19 +13:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 13:34:09 +13:00
|
|
|
fun remove(topic: Topic) {
|
|
|
|
val currentList = topics.value
|
|
|
|
if (currentList != null) {
|
|
|
|
val updatedList = currentList.toMutableList()
|
|
|
|
updatedList.remove(topic)
|
|
|
|
topics.postValue(updatedList)
|
|
|
|
}
|
2021-10-27 15:41:19 +13:00
|
|
|
jobs.remove(topic.id)?.cancel() // Cancel coroutine and remove
|
2021-10-27 13:34:09 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-10-27 14:44:12 +13:00
|
|
|
openConnection(this, topic)
|
2021-10-27 13:34:09 +13:00
|
|
|
delay(5000) // TODO exponential back-off
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:44:12 +13:00
|
|
|
private fun openConnection(scope: CoroutineScope, topic: Topic) {
|
|
|
|
val url = "${topic.baseUrl}/${topic.name}/json"
|
2021-10-27 13:34:09 +13:00
|
|
|
println("Connecting to $url ...")
|
|
|
|
val conn = (URL(url).openConnection() as HttpURLConnection).also {
|
|
|
|
it.doInput = true
|
2021-10-27 14:44:12 +13:00
|
|
|
it.readTimeout = READ_TIMEOUT
|
2021-10-27 13:34:09 +13:00
|
|
|
}
|
2021-10-27 15:56:03 +13:00
|
|
|
// TODO ugly
|
|
|
|
val currentTopic = get(topic.id)
|
|
|
|
if (currentTopic != null) {
|
|
|
|
update(currentTopic.copy(status = Status.SUBSCRIBED))
|
|
|
|
}
|
2021-10-27 13:34:09 +13:00
|
|
|
try {
|
|
|
|
val input = conn.inputStream.bufferedReader()
|
|
|
|
while (scope.isActive) {
|
2021-10-27 14:44:12 +13:00
|
|
|
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
|
|
|
|
}
|
2021-10-27 13:34:09 +13:00
|
|
|
try {
|
2021-10-27 14:44:12 +13:00
|
|
|
val json = gson.fromJson(line, JsonObject::class.java) ?: break // Break on unexpected line
|
2021-10-27 13:34:09 +13:00
|
|
|
if (!json.isJsonNull && json.has("message")) {
|
|
|
|
val message = json.get("message").asString
|
2021-10-27 15:41:19 +13:00
|
|
|
notificationListener?.let { it(Notification(topic, message)) }
|
|
|
|
|
|
|
|
// TODO ugly
|
|
|
|
val currentTopic = get(topic.id)
|
|
|
|
if (currentTopic != null) {
|
|
|
|
update(currentTopic.copy(messages = currentTopic.messages+1))
|
|
|
|
}
|
2021-10-27 13:34:09 +13:00
|
|
|
}
|
|
|
|
} catch (e: JsonSyntaxException) {
|
2021-10-27 14:44:12 +13:00
|
|
|
break // Break on unexpected line
|
2021-10-27 13:34:09 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e: IOException) {
|
2021-10-27 14:44:12 +13:00
|
|
|
println("Connection error: " + e.message)
|
2021-10-27 13:34:09 +13:00
|
|
|
} finally {
|
|
|
|
conn.disconnect()
|
|
|
|
}
|
2021-10-27 15:56:03 +13:00
|
|
|
val currentTopic2 = get(topic.id)
|
|
|
|
if (currentTopic2 != null) {
|
|
|
|
update(currentTopic2.copy(status = Status.CONNECTING))
|
|
|
|
}
|
2021-10-27 13:34:09 +13:00
|
|
|
println("Connection terminated: $url")
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
2021-10-27 14:44:12 +13:00
|
|
|
private var instance: Repository? = null
|
2021-10-27 13:34:09 +13:00
|
|
|
|
2021-10-27 14:44:12 +13:00
|
|
|
fun getInstance(): Repository {
|
|
|
|
return synchronized(Repository::class) {
|
|
|
|
val newInstance = instance ?: Repository()
|
2021-10-27 13:34:09 +13:00
|
|
|
instance = newInstance
|
|
|
|
newInstance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|