Renetik Android - Event & Property
https://github.com/renetik/renetik-android-event
Documentation
Framework to enjoy, improve and speed up your application development while writing readable code. Used as library in many projects and improving it while developing new projects. I am open for Hire or investment in my mobile app music production & perfromance project Renetik Instruments www.renetik.com.
Installation
allprojects {
repositories {
// For master-SNAPSHOT
maven { url 'https://github.com/renetik/maven-snapshot/raw/master/repository' }
// For release builds
maven { url 'https://github.com/renetik/maven/raw/master/repository' }
}
}
dependencies {
implementation 'com.renetik.library:renetik-android-event:$renetik-android-version'
}
Examples
/**
* Simple event use cases
*/
class EventTest {
@Test
fun testListen() {
val event = event()
var count = 0
event.listen { count += 1 }
event.fire()
event.fire()
assertEquals(count, 2)
}
@Test
fun testArgListen() {
val event = event()
var count = 0
event.listen { count += it }
event.fire(2)
event.fire(3)
assertEquals(count, 5)
}
@Test
fun testListenOnce() {
val event = event()
var count = 0
event.listenOnce { count += 1 }
event.fire()
event.fire()
assertEquals(count, 1)
}
@Test
fun testArgListenOnce() {
val event = event()
var count = 0
event.listenOnce { count += 1 }
event.fire()
event.fire()
assertEquals(count, 1)
}
@Test
fun testEventCancel() {
val event = event()
var count = 0
event.listen { registration, _ ->
count += 1
if (count == 2) registration.cancel()
}
event.fire()
event.fire()
event.fire()
assertEquals(count, 2)
}
@Test
fun testStringEventCancel() {
val event = event()
var value: String? = null
event.listen { registration, newValue ->
if (newValue == "second") registration.cancel()
value = newValue
}
event.fire("first")
assertEquals("first", value)
event.fire("second")
assertEquals("second", value)
event.fire("third")
assertEquals("second", value)
}
@Test
fun testEventPause() {
val event = event()
var count = 0
val registration = event.listen { count += 1 }
registration.pause { event.fire() }
assertEquals(count, 0)
event.fire()
assertEquals(count, 1)
}
}
/**
* Simple event property use cases
*/
class EventPropertyTest {
@Test
fun testOnChange() {
val property = property("initial")
var count = 0
property.onChange { count += 1 }
property.value = "second"
property.value = "third"
assertEquals(count, 2)
assertEquals("third", property.value)
}
@Test
fun testOnApply() {
var count = 0
val property = property("initial") { count += 1 }.apply()
property.value = "second"
property.value = "third"
assertEquals(count, 3)
assertEquals("third", property.value)
}
@Test
fun testArgListen() {
var count = 0
val property = property(0) { count += 1 }
property.value += 2
property.value += 3
assertEquals(5, property.value)
assertEquals(2, count)
}
@Test
fun testEquals() {
var count = 0
val property = property("") { count += 1 }
property.value = "second"
property.value = "second"
assertEquals(count, 1)
assertEquals("second", property.value)
}
@Test
fun testOnChangeOnce() {
var count = 0
val property = property("")
property.onChangeOnce { count += 1 }
property.value = "second"
property.value = "third"
assertEquals(count, 1)
assertEquals("third", property.value)
}
@Test
fun testEventCancel() {
var count = 0
val property = property(0)
property.onChange { registration, value ->
count += value
if (count > 2) registration.cancel()
}
property.value = 1
property.value = 2
property.value = 3
assertEquals(count, 3)
}
@Test
fun testEventPause() {
var count = 0
val property = property(0)
val registration = property.onChange { count += it }
registration.pause { property.value = 1 }
assertEquals(count, 0)
property.value = 2
assertEquals(count, 2)
}
}
/**
* Event unregister after owner nulled
*/
class EventOwnerEventTest {
@Test
fun testUnregisteredAfterNilled() {
val owner = CSEventOwnerHasDestroyBase()
val event = event()
var count = 0
owner.register(event.listen { count += 1 })
event.fire()
event.fire()
assertEquals(count, 2)
owner.destroy()
event.fire()
assertEquals(count, 2)
}
}
/**
* Event property unregister after owner nulled
*/
class EventOwnerPropertyTest {
class SomeClass(argument: SomeClass? = null) : CSEventOwnerHasDestroyBase(argument) {
val string = property("initial value")
init {
register(argument?.string?.onChange { string.value = it })
}
}
@Test
fun testUnregisteredAfterNilled() {
val instance1 = SomeClass()
val instance2 = SomeClass(instance1)
val instance3 = SomeClass(instance2)
assertEquals(instance3.string.value, "initial value")
instance1.string.value = "first value"
assertEquals(instance3.string.value, "first value")
instance2.destroy()
instance1.string.value = "second value"
assertEquals(instance3.string.value, "first value")
}
}