From b321bbe07ff48d571feb4f81c66f58223584dc83 Mon Sep 17 00:00:00 2001
From: pacien
Date: Mon, 20 Aug 2018 17:46:07 +0200
Subject: Generify periodically refreshing live data
---
.../activities/common/SelfRefreshingLiveData.kt | 42 ++++++++++++++++++++++
.../activities/start/NetworkListLiveData.kt | 19 +++-------
.../activities/status/nodes/NodeListLiveData.kt | 25 +++++--------
.../tincapp/activities/viewlog/LogLiveData.kt | 32 ++++++++---------
4 files changed, 70 insertions(+), 48 deletions(-)
create mode 100644 app/src/main/java/org/pacien/tincapp/activities/common/SelfRefreshingLiveData.kt
(limited to 'app')
diff --git a/app/src/main/java/org/pacien/tincapp/activities/common/SelfRefreshingLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/common/SelfRefreshingLiveData.kt
new file mode 100644
index 0000000..cb98736
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/common/SelfRefreshingLiveData.kt
@@ -0,0 +1,42 @@
+/*
+ * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
+ * Copyright (C) 2017-2018 Pacien TRAN-GIRARD
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+package org.pacien.tincapp.activities.common
+
+import android.arch.lifecycle.LiveData
+import java.util.concurrent.Executors
+import java.util.concurrent.ScheduledFuture
+import java.util.concurrent.TimeUnit
+
+/**
+ * @author pacien
+ */
+abstract class SelfRefreshingLiveData(private val refreshInterval: Long, private val timeUnit: TimeUnit) : LiveData() {
+ private val scheduledExecutor = Executors.newSingleThreadScheduledExecutor()
+ private lateinit var scheduledFuture: ScheduledFuture<*>
+
+ override fun onActive() {
+ scheduledFuture = scheduledExecutor.scheduleWithFixedDelay(this::onRefresh, 0, refreshInterval, timeUnit)
+ }
+
+ override fun onInactive() {
+ scheduledFuture.cancel(false)
+ }
+
+ abstract fun onRefresh()
+}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListLiveData.kt
index d0d39b8..aaab0e7 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListLiveData.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/start/NetworkListLiveData.kt
@@ -18,28 +18,17 @@
package org.pacien.tincapp.activities.start
-import android.arch.lifecycle.LiveData
+import org.pacien.tincapp.activities.common.SelfRefreshingLiveData
import org.pacien.tincapp.context.AppPaths
-import java.util.*
-import kotlin.concurrent.timer
+import java.util.concurrent.TimeUnit
/**
* @author pacien
*/
-class NetworkListLiveData : LiveData>() {
- private val updateInterval = 2 * 1000L // in milliseconds
+class NetworkListLiveData : SelfRefreshingLiveData>(1, TimeUnit.SECONDS) {
private val appPaths = AppPaths
- private lateinit var updateTimer: Timer
- override fun onActive() {
- updateTimer = timer(period = updateInterval, action = { updateNetworkList() })
- }
-
- override fun onInactive() {
- updateTimer.apply { cancel() }.apply { purge() }
- }
-
- private fun updateNetworkList() {
+ override fun onRefresh() {
val networkList = appPaths.confDir().list()?.sorted() ?: emptyList()
postValue(networkList)
}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt
index cdbdf0a..70ea54e 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/status/nodes/NodeListLiveData.kt
@@ -18,30 +18,21 @@
package org.pacien.tincapp.activities.status.nodes
-import android.arch.lifecycle.LiveData
+import org.pacien.tincapp.activities.common.SelfRefreshingLiveData
import org.pacien.tincapp.commands.Tinc
-import java.util.*
-import kotlin.concurrent.timer
+import java.util.concurrent.TimeUnit
/**
* @author pacien
*/
-class NodeListLiveData(private val netName: String) : LiveData>() {
- private val updateInterval = 2 * 1000L // in milliseconds
+class NodeListLiveData(private val netName: String) : SelfRefreshingLiveData>(1, TimeUnit.SECONDS) {
private val tincCtl = Tinc
- private lateinit var updateTimer: Timer
- override fun onActive() {
- updateTimer = timer(period = updateInterval, action = { updateNodeList() })
- }
-
- override fun onInactive() {
- updateTimer.apply { cancel() }.apply { purge() }
- }
+ override fun onRefresh() {
+ val nodeList = tincCtl.dumpNodes(netName)
+ .thenApply { list -> list.map { it.substringBefore(' ') } }
+ .get()
- private fun updateNodeList() {
- tincCtl.dumpNodes(netName)
- .thenApply { list -> list.map { it.substringBefore(' ')} }
- .thenAccept(this::postValue)
+ postValue(nodeList)
}
}
diff --git a/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt b/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt
index f410a8c..9767e12 100644
--- a/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt
+++ b/app/src/main/java/org/pacien/tincapp/activities/viewlog/LogLiveData.kt
@@ -18,30 +18,37 @@
package org.pacien.tincapp.activities.viewlog
-import android.arch.lifecycle.LiveData
+import org.pacien.tincapp.activities.common.SelfRefreshingLiveData
import org.pacien.tincapp.commands.Executor
import org.pacien.tincapp.commands.Tinc
import java.util.*
-import kotlin.concurrent.timer
+import java.util.concurrent.TimeUnit
/**
* @author pacien
*/
-class LogLiveData(private val netName: String, private val logLevel: Int, private val logLineSize: Int) : LiveData>() {
- private val updateInterval = 250L // milliseconds
+class LogLiveData(private val netName: String, private val logLevel: Int, private val logLineSize: Int)
+ : SelfRefreshingLiveData>(250, TimeUnit.MILLISECONDS) {
+
private val executor = Executor
private val log = LinkedList()
- private var loggerProcess: Process? = null
- private var logUpdateTimer: Timer? = null
+ private lateinit var loggerProcess: Process
override fun onActive() {
+ super.onActive()
loggerProcess = startNewLogger()
- logUpdateTimer = timer(period = updateInterval, action = { outputLog() })
}
override fun onInactive() {
- loggerProcess?.destroy()
- logUpdateTimer?.apply { cancel() }?.apply { purge() }
+ loggerProcess.destroy()
+ super.onInactive()
+ }
+
+ override fun onRefresh() {
+ synchronized(log) {
+ val logView = ArrayList(log)
+ postValue(logView)
+ }
}
private fun startNewLogger(): Process {
@@ -64,11 +71,4 @@ class LogLiveData(private val netName: String, private val logLevel: Int, privat
log.addLast(line)
}
}
-
- private fun outputLog() {
- synchronized(log) {
- val logView = ArrayList(log)
- postValue(logView)
- }
- }
}
--
cgit v1.2.3