Commit 59e74014 authored by David's avatar David

Converted to a proper class, for better management of stuff

parent 5441fb3c
......@@ -28,12 +28,13 @@ import java.util.concurrent.TimeUnit
import kotlin.random.Random
//Generic the shit out of this!
data class TickerEntry<T : Node>(
open class TickerEntry<out T : Node>(
val title: String,
val content: T,
val reschedule: Boolean = false,
val updateObservable: ((content: T) -> Completable)? = null
)
val reschedule: Boolean = false
) {
open fun updateObservable(): Completable? = null
}
//TODO: need to add a size buffer, some dynamic things get weird
/**
......@@ -76,63 +77,53 @@ class TickerView : View() {
}
override fun onDock() {
//Lets create a timer view for the current time, and one for the CPU temp that get rescheduled when they're done
val currentTime = TickerEntry("Current Time", createText("88:88:88"), reschedule = true) { text ->
val secondClock = Clock.tickSeconds(ZoneId.systemDefault())
val formatter = DateTimeFormatter.ofPattern("HH:mm:ss")
Observable.interval(1, TimeUnit.SECONDS)
.map {
LocalDateTime.ofInstant(secondClock.instant(), ZoneId.systemDefault())
}
.doOnNextFx {
text.text = formatter.format(it)
}
.ignoreElements()
}
val DEGREE = "\u00b0"
val currentTemp = TickerEntry("CPU Temp", createText("88${DEGREE}C"), reschedule = true) { text ->
val temperatureFile = Paths.get("/sys/class/thermal/thermal_zone0/temp").toFile()
Observable.interval(1, TimeUnit.SECONDS)
.observeOn(Schedulers.io())
.map {
if (temperatureFile.exists()) {
temperatureFile.readText().trim().toInt()
} else {
//TODO: add some randomization to this, because coloring!
Random.nextInt(45000, 88000)
val currentTime = object : TickerEntry<Text>(title = "Time", content = createText("88:88:88"), reschedule = true) {
private val secondClock = Clock.tickSeconds(ZoneId.systemDefault())
private val formatter = DateTimeFormatter.ofPattern("HH:mm:ss")
override fun updateObservable(): Completable {
return Observable.interval(1, TimeUnit.SECONDS)
.map {
LocalDateTime.ofInstant(secondClock.instant(), ZoneId.systemDefault())
}
}
.map {
it.toDouble().div(1000)
}
.doOnNextFx {
text.text = "%.2f%sC".format(it, DEGREE)
}
.ignoreElements()
.doOnNextFx {
content.text = formatter.format(it)
}
.ignoreElements()
}
}
val cpuTemp = object : TickerEntry<Text>(title = "CPU Temp", content = createText("88${DEGREE}C"), reschedule = true) {
val temperatureFile = Paths.get("/sys/class/thermal/thermal_zone0/temp").toFile()
//Do the updating logic out of band on an observable that can be cancelled when it's gone
val time2 = TickerEntry("CurrentTimeObserved", createText("2011-12-03T10:15:30.000"), updateObservable = { text ->
Observable.interval(1, TimeUnit.SECONDS)
.doOnNextFx {
text.text = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
}
.doOnNext {
logger.debug("Updating time! -- $it")
}
.ignoreElements()
})
override fun updateObservable(): Completable? {
return Observable.interval(1, TimeUnit.SECONDS)
.observeOn(Schedulers.io())
.map {
if (temperatureFile.exists()) {
temperatureFile.readText().trim().toInt()
} else {
//TODO: add some randomization to this, because coloring!
Random.nextInt(45000, 88000)
}
}
.map {
it.toDouble().div(1000)
}
.doOnNextFx {
content.text = "%.2f%sC".format(it, DEGREE)
}
.ignoreElements()
}
}
val entries2 = listOf(
TickerEntry<Node>("time", createText(ZonedDateTime.now().format(DateTimeFormatter.ISO_ZONED_DATE_TIME)))
)
val entries = listOf(
currentTime as TickerEntry<Node>,
currentTime,
TickerEntry<Node>("blorp", createText("Third!")),
TickerEntry<Node>("blerp", createText("this is the second")),
//time as TickerEntry<Node>,
......@@ -140,7 +131,7 @@ class TickerView : View() {
//TickerEntry<Node>("more", createText("Some more words")),
TickerEntry<Node>("another", createText("This is happening")),
TickerEntry<Node>("time", createText(ZonedDateTime.now().format(DateTimeFormatter.ISO_ZONED_DATE_TIME))),
currentTemp as TickerEntry<Node>
cpuTemp
)
Observable.fromIterable(entries)
......@@ -174,6 +165,7 @@ class MarqueeView : View() {
private val OFFSET = 5.0 //Amount of space between entries!
data class ActiveTick(val entry: TickerEntry<Node>, var cleared: Boolean = false)
private val activeTicks = ConcurrentLinkedQueue<ActiveTick>() //This might not need to be threadsafe, only one thing is adding/removing it
private val queuedTicks = ConcurrentLinkedQueue<TickerEntry<Node>>() //This one does, multiple threads!
......@@ -216,10 +208,10 @@ class MarqueeView : View() {
fun lastOneCleared(): Boolean {
//Determine if the last entry in the activeQueue has cleared
val last = activeTicks.last()
if(!last.cleared){
if (!last.cleared) {
//Only do the math one time, well up to many times, but it might be cleared, and then clean it out
val entry = last.entry
if(entry.content.layoutBounds.width + entry.content.layoutX + OFFSET <= root.width) {
if (entry.content.layoutBounds.width + entry.content.layoutX + OFFSET <= root.width) {
last.cleared = true
}
}
......@@ -273,11 +265,12 @@ class MarqueeView : View() {
} else {
content.layoutX = content.layoutX - 1 //TODO: I should probably do animation in larger chunks perhaps, so that it can use less CPU?
//If there's an observable that we haven't started, fire it up!
if (entry.updateObservable != null && !subscriptions.containsKey(entry)) {
val updateObservable = entry.updateObservable()
if (updateObservable != null && !subscriptions.containsKey(entry)) {
//Start up the observable that updates the UI
logger.debug("Starting observable for ${entry.title}")
val disposable = entry.updateObservable.invoke(entry.content).subscribe()
subscriptions.put(entry, disposable)
val disposable = updateObservable.subscribe()
subscriptions[entry] = disposable
//Need a thing to track these with
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment