Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
İbrahim Yilmaz committed Apr 14, 2019
0 parents commit dc906aa
Show file tree
Hide file tree
Showing 16 changed files with 764 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
6 changes: 6 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## [Unreleased]
- PublishLiveData and ReplayLiveData implemented
- Initial commit

21 changes: 21 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [2019] [IBRAHIM YILMAZ [email protected]]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
211 changes: 211 additions & 0 deletions arch_data.iml

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 28



defaultConfig {
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
}
}

dependencies {
def lifecycle_version = "2.0.0"
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
testImplementation "org.mockito:mockito-core:$versionMockito"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:$versionMockitoKotlin"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
}
repositories {
mavenCentral()
}
21 changes: 21 additions & 0 deletions proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.ibrahimyilmaz.archrelay

import android.content.Context

import org.junit.Test
import org.junit.runner.RunWith

import androidx.test.InstrumentationRegistry
import androidx.test.runner.AndroidJUnit4

import org.junit.Assert.assertEquals

/**
* Instrumented test, which will execute on an Android device.
*
* @see [Testing documentation](http://d.android.com/tools/testing)
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getTargetContext()

assertEquals("me.ibrahimyilmaz.archrelay.test", appContext.packageName)
}
}
1 change: 1 addition & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="me.ibrahimyilmaz.arch_data" />
13 changes: 13 additions & 0 deletions src/main/java/me/ibrahimyilmaz/arch_data/LiveDataExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.ibrahimyilmaz.arch_data

import androidx.lifecycle.MutableLiveData


inline fun <T> mutableLiveDataOf(): MutableLiveData<T> = MutableLiveData()

inline fun <T> publishLiveDataOf(): PublishLiveData<T> = PublishLiveData()

inline fun <T> replayLiveDataOf(): ReplayLiveData<T> = ReplayLiveData()



46 changes: 46 additions & 0 deletions src/main/java/me/ibrahimyilmaz/arch_data/MemoryLessLiveData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.ibrahimyilmaz.arch_data

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer

internal class MemoryLessLiveData<T> : MutableLiveData<T>() {

private val singleDataEvent = mutableLiveDataOf<SingleLiveDataEvent<T>>()

private val observerMap = mutableMapOf<Observer<in T>, SingleLiveEventObserver<T>>()

private val observers: MutableCollection<Observer<in T>> get() = observerMap.keys

override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
val singleEventObserver = singleEventObserverOf(observer)
observerMap[observer] = singleEventObserver
singleDataEvent.observe(owner, singleEventObserver)
}

override fun observeForever(observer: Observer<in T>) {
val singleEventObserver = singleEventObserverOf(observer)
observerMap[observer] = singleEventObserver
singleDataEvent.observeForever(singleEventObserver)
}

override fun removeObserver(observer: Observer<in T>) {
observerMap.remove(observer)?.let {
singleDataEvent.removeObserver(it)
}
}

override fun removeObservers(owner: LifecycleOwner) {
observerMap.clear()
singleDataEvent.removeObservers(owner)
}

override fun postValue(value: T) =
singleDataEvent.postValue(SingleLiveDataEvent(observers, value))

override fun setValue(value: T) {
singleDataEvent.value = SingleLiveDataEvent(observers, value)
}

}

38 changes: 38 additions & 0 deletions src/main/java/me/ibrahimyilmaz/arch_data/PublishLiveData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.ibrahimyilmaz.arch_data

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer

class PublishLiveData<T> : MutableLiveData<T>() {

private val liveData = MemoryLessLiveData<T>()

override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
liveData.observe(owner, observer)
}

override fun observeForever(observer: Observer<in T>) {
liveData.observeForever(observer)
}

override fun removeObserver(observer: Observer<in T>) {
liveData.removeObserver(observer)
}

override fun removeObservers(owner: LifecycleOwner) {
liveData.removeObservers(owner)
}

override fun postValue(value: T) {
liveData.postValue(value)
}

override fun setValue(value: T) {
liveData.setValue(value)
}

override fun getValue(): T? {
return liveData.value
}
}
50 changes: 50 additions & 0 deletions src/main/java/me/ibrahimyilmaz/arch_data/ReplayLiveData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.ibrahimyilmaz.arch_data

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer

class ReplayLiveData<T> : MutableLiveData<T>() {

private val liveData = MemoryLessLiveData<T>()

private val values = mutableListOf<T>()

private val mutableDataEvent = mutableLiveDataOf<Observer<in T>>()

private val eventObserver: (Observer<in T>) -> Unit = { observer ->
values.forEach { observer.onChanged(it) }
}

init {
mutableDataEvent.observeForever(eventObserver)
}

override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
liveData.observe(owner, observer)
mutableDataEvent.postValue(observer)
}

override fun observeForever(observer: Observer<in T>) {
liveData.observeForever(observer)
mutableDataEvent.postValue(observer)
}

override fun postValue(value: T) {
values.add(value)
liveData.postValue(value)
}

override fun setValue(value: T) {
values.add(value)
liveData.setValue(value)
}

override fun getValue(): T? {
return liveData.value
}

protected fun finalize() {
mutableDataEvent.removeObserver(eventObserver)
}
}
15 changes: 15 additions & 0 deletions src/main/java/me/ibrahimyilmaz/arch_data/SingleEventObserver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.ibrahimyilmaz.arch_data

import androidx.lifecycle.Observer


internal inline fun <T> singleEventObserverOf(observer: Observer<in T>): SingleLiveEventObserver<T> = SingleLiveEventObserver(observer)

internal class SingleLiveEventObserver<T>(private val observer: Observer<in T>) : Observer<SingleLiveDataEvent<T>> {
override fun onChanged(t: SingleLiveDataEvent<T>?) {
t?.getContentIfNotHandled(observer)?.let {
observer.onChanged(it)
}
}
}

17 changes: 17 additions & 0 deletions src/main/java/me/ibrahimyilmaz/arch_data/SingleLiveDataEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.ibrahimyilmaz.arch_data

import androidx.lifecycle.Observer


internal class SingleLiveDataEvent<T>(observers: MutableCollection<Observer<in T>>, private val content: T) {

private val receiverObservers = ArrayList(observers)

fun getContentIfNotHandled(observer: Observer<in T>): T? =
receiverObservers.firstOrNull { it == observer }?.let {
receiverObservers.remove(it)
content
}
}


Loading

0 comments on commit dc906aa

Please sign in to comment.