Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added AutoCleanedValue.kt + dependency updates #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
Expand All @@ -24,17 +25,22 @@ android {
buildFeatures {
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"


implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1-alpha01'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1-alpha01'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.4.0-alpha05'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0-alpha05'
implementation project(':utils')
implementation 'androidx.multidex:multidex:2.0.1'
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.4.20'
ext.kotlin_version = '1.4.31'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.2'
classpath 'com.android.tools.build:gradle:4.1.3'
classpath 'com.google.gms:google-services:4.3.5'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.github.dcendents:android-maven-gradle-plugin:2.1"
Expand Down
24 changes: 13 additions & 11 deletions utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ apply plugin: 'kotlin-android'
apply plugin: 'com.github.dcendents.android-maven'

// JitPack
group="bornfight.com"
version="0.98"
group = "bornfight.com"
version = "0.98"


android {
compileSdkVersion 30



defaultConfig {
minSdkVersion 16
targetSdkVersion 30
Expand All @@ -38,38 +37,41 @@ dependencies {
// Various dependencies ========================================

implementation 'org.ocpsoft.prettytime:prettytime:4.0.1.Final'
implementation 'com.github.bumptech.glide:glide:4.10.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'

// ==============================================================

// AndroidX Extensions
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.browser:browser:1.0.0'
implementation 'androidx.browser:browser:1.3.0'

// Google
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.material:material:1.3.0'

// RxJava2
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.11'
implementation 'com.artemzin.rxjava:proguard-rules:1.3.3.0'

//Retrofit & Networking
implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:retrofit:2.6.2'

// Firebase
implementation 'com.google.firebase:firebase-core:17.2.1'
implementation 'com.google.firebase:firebase-core:18.0.2'

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

//Lifecycle
implementation "androidx.lifecycle:lifecycle-common-java8:2.3.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"

// Tests
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-beta01', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testImplementation 'junit:junit:4.12'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.13'
}


Expand Down
56 changes: 56 additions & 0 deletions utils/src/main/java/com/bornfight/utils/AutoCleanedValue.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.bornfight.utils

import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty


class AutoCleanedValue<T : Any>(fragment: Fragment, private val initializer: (() -> T)?) :
ReadWriteProperty<Fragment, T> {

private var _value: T? = null

init {
fragment.lifecycle.addObserver(object : DefaultLifecycleObserver {
val viewLifecycleOwnerObserver = Observer<LifecycleOwner?> { viewLifecycleOwner ->

viewLifecycleOwner?.lifecycle?.addObserver(object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
_value = null
}
})
}

override fun onCreate(owner: LifecycleOwner) {
fragment.viewLifecycleOwnerLiveData.observeForever(viewLifecycleOwnerObserver)
}

override fun onDestroy(owner: LifecycleOwner) {
fragment.viewLifecycleOwnerLiveData.removeObserver(viewLifecycleOwnerObserver)
}
})
}

override fun getValue(thisRef: Fragment, property: KProperty<*>): T {
val value = _value

if (value != null) {
return value
}

if (thisRef.viewLifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.INITIALIZED)) {
return initializer?.invoke().also { _value = it }
?: throw IllegalStateException("The value has not yet been set or no default initializer provided")
} else {
throw IllegalStateException("Fragment might have been destroyed or not initialized yet")
}
}

override fun setValue(thisRef: Fragment, property: KProperty<*>, value: T) {
_value = value
}
}
11 changes: 11 additions & 0 deletions utils/src/main/java/com/bornfight/utils/ViewBindingUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.bornfight.utils

import androidx.fragment.app.Fragment

/**
* Cleans the field reference once the view is destroyed.
* There is also an option of setting initial value.
*/
fun <T : Any> Fragment.autoCleaned(initializer: (() -> T)? = null): AutoCleanedValue<T> {
return AutoCleanedValue(this, initializer)
}