Singleton repository class

This commit is contained in:
Philipp Heckel 2021-10-26 20:34:09 -04:00
parent b25ce1f06a
commit 638c8f093a
5 changed files with 134 additions and 167 deletions

View file

@ -41,7 +41,7 @@ const val TOPIC_URL = "url"
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private val newTopicActivityRequestCode = 1 private val newTopicActivityRequestCode = 1
private val topicsViewModel by viewModels<TopicsViewModel> { private val topicsViewModel by viewModels<TopicsViewModel> {
TopicsViewModelFactory(this) TopicsViewModelFactory()
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {

View file

@ -1,116 +1,46 @@
/* package io.heckel.ntfy
* 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.
*/
package io.heckel.ntfy.list import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import android.content.Context import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.* import androidx.lifecycle.viewModelScope
import com.google.gson.GsonBuilder import io.heckel.ntfy.data.TopicsRepository
import com.google.gson.JsonObject
import com.google.gson.JsonSyntaxException
import io.heckel.ntfy.data.DataSource
import io.heckel.ntfy.data.Topic import io.heckel.ntfy.data.Topic
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow import kotlin.collections.List
import kotlinx.coroutines.flow.flow
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
data class Notification(val topic: String, val message: String) data class Notification(val topic: String, val message: String)
typealias NotificationListener = (notification: Notification) -> Unit typealias NotificationListener = (notification: Notification) -> Unit
class TopicsViewModel(val datasource: DataSource) : ViewModel() { class TopicsViewModel(private val repository: TopicsRepository) : ViewModel() {
private val gson = GsonBuilder().create()
private val jobs = mutableMapOf<Long, Job>()
private var notificationListener: NotificationListener? = null;
fun add(topic: Topic) { fun add(topic: Topic) {
println("Adding topic $topic $this") repository.add(topic, viewModelScope)
datasource.add(topic)
jobs[topic.id] = subscribeTopic(topic.url)
} }
fun get(id: Long) : Topic? { fun get(id: Long) : Topic? {
return datasource.get(id) return repository.get(id)
} }
fun list(): LiveData<List<Topic>> { fun list(): LiveData<List<Topic>> {
return datasource.list() return repository.list()
} }
fun remove(topic: Topic) { fun remove(topic: Topic) {
println("Removing topic $topic $this") repository.remove(topic)
jobs[topic.id]?.cancel()
println("${jobs[topic.id]}")
jobs.remove(topic.id)?.cancel() // Cancel and remove
println("${jobs[topic.id]}")
datasource.remove(topic)
} }
fun setNotificationListener(listener: NotificationListener) { fun setNotificationListener(listener: NotificationListener) {
notificationListener = listener repository.setNotificationListener(listener)
}
private fun subscribeTopic(url: String): Job {
return viewModelScope.launch(Dispatchers.IO) {
while (isActive) {
openURL(this, url)
delay(5000) // TODO exponential back-off
}
}
}
private fun openURL(scope: CoroutineScope, url: String) {
println("Connecting to $url ...")
val conn = (URL(url).openConnection() as HttpURLConnection).also {
it.doInput = true
}
try {
val input = conn.inputStream.bufferedReader()
while (scope.isActive) {
val line = input.readLine() ?: break // Exit if null
try {
val json = gson.fromJson(line, JsonObject::class.java) ?: break // Exit if null
if (!json.isJsonNull && json.has("message")) {
val message = json.get("message").asString
notificationListener?.let { it(Notification(url, message)) }
}
} catch (e: JsonSyntaxException) {
// Ignore invalid JSON
}
}
} catch (e: IOException) {
println("PHIL: " + e.message)
} finally {
conn.disconnect()
}
println("Connection terminated: $url")
} }
} }
class TopicsViewModelFactory(private val context: Context) : ViewModelProvider.Factory { class TopicsViewModelFactory() : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T { @Suppress("UNCHECKED_CAST")
if (modelClass.isAssignableFrom(TopicsViewModel::class.java)) { override fun <T : ViewModel?> create(modelClass: Class<T>) =
@Suppress("UNCHECKED_CAST") with(modelClass){
return TopicsViewModel( when {
datasource = DataSource.getDataSource(context.resources) isAssignableFrom(TopicsViewModel::class.java) -> TopicsViewModel(TopicsRepository.getInstance()) as T
) as T else -> throw IllegalArgumentException("Unknown viewModel class $modelClass")
}
} }
throw IllegalArgumentException("Unknown ViewModel class")
}
} }

View file

@ -1,72 +0,0 @@
/*
* 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.
*/
package io.heckel.ntfy.data
import android.content.res.Resources
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
/* Handles operations on topicsLiveData and holds details about it. */
class DataSource(resources: Resources) {
private val topicsLiveData: MutableLiveData<List<Topic>> = MutableLiveData(mutableListOf())
/* Adds topic to liveData and posts value. */
fun add(topic: Topic) {
val currentList = topicsLiveData.value
if (currentList == null) {
topicsLiveData.postValue(listOf(topic))
} else {
val updatedList = currentList.toMutableList()
updatedList.add(0, topic)
topicsLiveData.postValue(updatedList)
}
}
/* Removes topic from liveData and posts value. */
fun remove(topic: Topic) {
val currentList = topicsLiveData.value
if (currentList != null) {
val updatedList = currentList.toMutableList()
updatedList.remove(topic)
topicsLiveData.postValue(updatedList)
}
}
/* Returns topic given an ID. */
fun get(id: Long): Topic? {
topicsLiveData.value?.let { topics ->
return topics.firstOrNull{ it.id == id}
}
return null
}
fun list(): LiveData<List<Topic>> {
return topicsLiveData
}
companion object {
private var instance: DataSource? = null
fun getDataSource(resources: Resources): DataSource {
return synchronized(DataSource::class) {
val newInstance = instance ?: DataSource(resources)
instance = newInstance
newInstance
}
}
}
}

View file

@ -0,0 +1,109 @@
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
/* Handles operations on topicsLiveData and holds details about it. */
class TopicsRepository {
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) {
openURL(this, topic.url, topic.url) // TODO
delay(5000) // TODO exponential back-off
}
}
}
private fun openURL(scope: CoroutineScope, topic: String, url: String) {
println("Connecting to $url ...")
val conn = (URL(url).openConnection() as HttpURLConnection).also {
it.doInput = true
}
try {
val input = conn.inputStream.bufferedReader()
while (scope.isActive) {
val line = input.readLine() ?: break // Exit if null
try {
val json = gson.fromJson(line, JsonObject::class.java) ?: break // Exit if null
if (!json.isJsonNull && json.has("message")) {
val message = json.get("message").asString
notificationListener?.let { it(Notification(url, message)) }
}
} catch (e: JsonSyntaxException) {
// Ignore invalid JSON
}
}
} catch (e: IOException) {
println("PHIL: " + e.message)
} finally {
conn.disconnect()
}
println("Connection terminated: $url")
}
companion object {
private var instance: TopicsRepository? = null
fun getInstance(): TopicsRepository {
return synchronized(TopicsRepository::class) {
val newInstance = instance ?: TopicsRepository()
instance = newInstance
newInstance
}
}
}
}

View file

@ -23,12 +23,12 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import io.heckel.ntfy.R import io.heckel.ntfy.R
import io.heckel.ntfy.TOPIC_ID import io.heckel.ntfy.TOPIC_ID
import io.heckel.ntfy.list.TopicsViewModel import io.heckel.ntfy.TopicsViewModel
import io.heckel.ntfy.list.TopicsViewModelFactory import io.heckel.ntfy.TopicsViewModelFactory
class DetailActivity : AppCompatActivity() { class DetailActivity : AppCompatActivity() {
private val topicsViewModel by viewModels<TopicsViewModel> { private val topicsViewModel by viewModels<TopicsViewModel> {
TopicsViewModelFactory(this) TopicsViewModelFactory()
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {