ntfy-android/app/src/main/java/io/heckel/ntfy/data/Repository.kt

115 lines
3.9 KiB
Kotlin
Raw Normal View History

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 14:44:12 +13:00
class Repository {
private val READ_TIMEOUT = 60_000 // Keep alive every 30s assumed
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;
/* Adds topic to liveData and posts value. */
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)
}
/* Removes topic from liveData and posts value. */
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 and remove
}
/* Returns topic given an ID. */
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
}
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 14:44:12 +13:00
notificationListener?.let { it(Notification(topic.name, message)) }
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()
}
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
}
}
}
}