Skip to content

Commit

Permalink
migrate to androidx
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-dev committed Apr 27, 2020
1 parent 7b6e772 commit e38534a
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 44 deletions.
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
compileSdkVersion 28
//buildToolsVersion "26.0.2"
defaultConfig {
applicationId "ds.bindingtools.demo"
minSdkVersion 19
targetSdkVersion 26
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -24,8 +24,8 @@ android {
dependencies {
implementation project(path: ':lib')
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation("androidx.appcompat:appcompat:1.1.0")
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
}
7 changes: 4 additions & 3 deletions app/src/main/java/ds/bindingtools/demo/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package ds.bindingtools.demo

import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import ds.bindingtools.bundle
import ds.bindingtools.startActivity
import ds.bindingtools.withBindable
Expand Down Expand Up @@ -55,10 +56,10 @@ class MainActivity : AppCompatActivity() {

private fun bindViews() = withBindable(viewModel) {
bind(::text, helloLabel::setText, helloLabel::getText)
bind(::buttonText, { it: String -> navigateButton.text = it }, { navigateButton.text.toString() })
bind(::buttonText, { navigateButton.text = it }, { navigateButton.text.toString() })

withBindable(nestedViewModel) {
bind(::secondaryText, { Toast.makeText(this@MainActivity, it, Toast.LENGTH_SHORT).show() })
bind(::secondaryText, { text -> Toast.makeText(this@MainActivity, text, Toast.LENGTH_SHORT).show() })
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/ds/bindingtools/demo/SecondActivity.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ds.bindingtools.demo

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import androidx.appcompat.app.AppCompatActivity
import ds.bindingtools.arg
import ds.bindingtools.res

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ds.bindingtools.demo.SecondActivity">

</android.support.constraint.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
buildscript {
ext.kotlin_version = '1.2.10'
ext.kotlin_version = '1.3.72'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.6.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' // for jitpack
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
21 changes: 13 additions & 8 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ apply plugin: 'com.github.dcendents.android-maven'
group='com.github.deviant-studio'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
compileSdkVersion 28
//buildToolsVersion "26.0.2"

defaultConfig {
minSdkVersion 19
targetSdkVersion 26
targetSdkVersion 28
versionCode 15
versionName "0.15"
versionName "0.16"

}

Expand All @@ -28,11 +28,16 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation("androidx.appcompat:appcompat:1.1.0")
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

def lifecycle_version = "2.2.0"
def arch_version = "2.1.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"

}

// build a jar with source files
Expand Down
32 changes: 17 additions & 15 deletions lib/src/main/java/ds/bindingtools/ArgsBindings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.support.annotation.IdRes
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import java.io.Serializable
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
Expand Down Expand Up @@ -52,15 +52,18 @@ inline fun <reified T : Activity> Activity.startActivityForResult(requestCode: I
}

inline fun <reified T : Fragment> FragmentActivity.replaceFragment(@IdRes layoutId: Int, args: Bundle? = null) {
val fragment = Fragment.instantiate(this, T::class.java.name, args)
val fragment = supportFragmentManager.fragmentFactory.instantiate(classLoader, T::class.java.name)
fragment.arguments = args
//val fragment = Fragment.instantiate(this, T::class.java.name, args)
supportFragmentManager
.beginTransaction()
.replace(layoutId, fragment)
.commitNow()
}

inline fun <reified T : Fragment> FragmentActivity.replaceFragment(@IdRes layoutId: Int, f: BundleBuilder.(T) -> Unit) {
val fragment = Fragment.instantiate(this, T::class.java.name) as T
val fragment = supportFragmentManager.fragmentFactory.instantiate(classLoader, T::class.java.name) as T
//val fragment = Fragment.instantiate(this, T::class.java.name) as T
val builder = BundleBuilder()
f(builder, fragment)
fragment.arguments = builder.build()
Expand All @@ -74,22 +77,21 @@ inline fun <reified T : Fragment> FragmentActivity.replaceFragment(@IdRes layout
@Suppress("unchecked_cast")
class ActivityArgsDelegate<out T : Any?>(private val default: T, private val cls: KClass<*>) : ReadOnlyProperty<Activity, T> {

override fun getValue(thisRef: Activity, property: KProperty<*>): T {
if (thisRef.intent?.extras == null)
return default
override fun getValue(thisRef: Activity, property: KProperty<*>): T = thisRef
.intent
?.extras
?.let { extras -> parseExtras(property.name, extras, cls, default) }
?: default

return parseExtras(property.name, thisRef.intent.extras, cls, default)
}
}

class FragmentArgsDelegate<out T : Any?>(private val default: T, private val cls: KClass<*>) : ReadOnlyProperty<Fragment, T> {

override fun getValue(thisRef: Fragment, property: KProperty<*>): T {
if (thisRef.arguments == null)
return default
override fun getValue(thisRef: Fragment, property: KProperty<*>): T = thisRef
.arguments
?.let { args -> parseExtras(property.name, args, cls, default) }
?: default

return parseExtras(property.name, thisRef.arguments, cls, default)
}
}

@Suppress("unchecked_cast")
Expand Down
8 changes: 4 additions & 4 deletions lib/src/main/java/ds/bindingtools/ResBindings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.support.annotation.AnyRes
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import androidx.annotation.AnyRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty

inline fun <reified T : Any> Context.res(@AnyRes id: Int): ReadOnlyProperty<Activity, T> = ResourcesDelegate(this, id, T::class)
inline fun <reified T : Any> Fragment.res(@AnyRes id: Int): ReadOnlyProperty<Activity, T> = ResourcesDelegate(this.context, id, T::class)
inline fun <reified T : Any> Fragment.res(@AnyRes id: Int): ReadOnlyProperty<Activity, T> = ResourcesDelegate(this.context!!, id, T::class)

class ResourcesDelegate<out T : Any>(private val context: Context, private val id: Int, private val cls: KClass<T>) : ReadOnlyProperty<Any, T> {
private lateinit var type: String
Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/java/ds/bindingtools/ViewBindings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
*/
package ds.bindingtools

import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleOwner
import android.util.Log
import android.widget.CompoundButton
import android.widget.TextView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import java.lang.ref.WeakReference
import java.util.*
import kotlin.properties.ReadWriteProperty
Expand Down

0 comments on commit e38534a

Please sign in to comment.