Skip to content

Commit

Permalink
docs and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-dev committed Oct 20, 2017
1 parent e48b71e commit 58c8169
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 53 deletions.
151 changes: 139 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,151 @@
# bindingtools
Convenient helpers for Android Kotlin development
Lightweight helper library for Android Kotlin development
- Shared Preferences delegates
- Bundle args delegates
- Resources delegates
- View<->Data Binding

### Setup
Step 1. Add the JitPack repository to your build file
## Quick Setup
Step 1. Add the JitPack repository to your build file
```
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
...
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```

Step 2. Add the dependency
```
dependencies {
compile 'com.github.deviant-studio:bindingtools:{latest_version}'
}
dependencies {
compile 'com.github.deviant-studio:bindingtools:{latest_version}'
}
```

## Documentation

### Data Binding
Let's say you have the Activity:
```kotlin
class MainActivity : AppCompatActivity() {

private lateinit var textLabel: TextView
private val viewModel = MainViewModel()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

textLabel = findViewById(R.id.text)
bindViews()
}

private fun bindViews() {
// ...
}
}
```
and the ViewModel:
```kotlin
class MainViewModel {
var text: String = ""

fun sayHello() {
text = "Hello, World!"
}
}
```
It would be nice if we can bind `text` field to the view. Let's modify ViewModel:
```kotlin
class MainViewModel : Bindable {
var text: String by binding("")

fun sayHello() {
text = "Hello, World!"
}
}
```
and Activity:
```kotlin
private fun bindViews() = with(viewModel) {
bind(::text, textLabel::setText, textLabel::getText)
}
```
Thats it! Now we can set TextView's text like:
```kotlin
viewModel.sayHello()
```
Don't forget to unbind viewModel to avoid leaks:
```kotlin
viewModel.unbindAll()
```
Also library allows you to simplify `TextView`/`EditText` bindings to this:
```kotlin
with(viewModel) {
bind(::text, textLabel)
}
```

### Shared Preferences
It's so annoying to deal with SharedPreferences directly:
```java
final String ageKey = "age";
final String userNameKey = "userName";
final String adminKey = "admin";
SharedPreferences prefs = getSharedPreferences("main_prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(ageKey, 12);
editor.putString(userNameKey, "Luke");
editor.putBoolean(adminKey,true);
editor.apply();
```
Fortunately now we have `Kotlin` and the `bindingtools`!
First, declare the `PreferencesAware` class
```kotlin
class Prefs(ctx: Context) : PreferencesAware {

override val forcePersistDefaults = true
override val sharedPreferences: SharedPreferences = ctx.getSharedPreferences("main_prefs", Context.MODE_PRIVATE)

var age by pref(0)
var userName by pref("")
var isAdmin by pref(false)

}
```
Now you can use preferences like this:
```kotlin
val prefs = Prefs(this)
prefs.age = 12
prefs.userName = "Ani Lorak"
prefs.isAdmin = true

println("the name is ${prefs.userName}")
```

### Bundle arguments tricks
The args bundle have never been such simple before. Let's declare another one activity:
```kotlin
class SecondActivity : AppCompatActivity() {

val userName: String by arg("")
val age: String by arg("")
val country: String? by arg()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
println("$userName $age $country")
}
}

```

### Resources
Same rules can be used when using resources:
```kotlin
private val appName: String by res(R.string.app_name)
...
println(appName)
```
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "ds.bindingtools"
applicationId "ds.bindingtools.demo"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
Expand All @@ -22,10 +22,10 @@ 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'
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'
}
21 changes: 17 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ds.bindingtools">
package="ds.bindingtools.demo">

<application android:allowBackup="true"
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"/>
</manifest>
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".SecondActivity"/>

</application>

</manifest>
51 changes: 51 additions & 0 deletions app/src/main/java/ds/bindingtools/demo/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ds.bindingtools.demo

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView
import ds.bindingtools.bind
import ds.bindingtools.startActivity

class MainActivity : AppCompatActivity() {

private lateinit var textLabel: TextView
private lateinit var prefs: Prefs
private val viewModel = MainViewModel()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textLabel = findViewById(R.id.text)
prefs = Prefs(this)

bindViews()

viewModel.sayHello()

fillPrefs()

findViewById<Button>(R.id.button).setOnClickListener { navigateNext() }

}

private fun bindViews() = with(viewModel) {
bind(::text, textLabel::setText, textLabel::getText)
}

private fun fillPrefs() {
prefs.age = 12
prefs.userName = "Ani Lorak"
prefs.isAdmin = true

println("the name is ${prefs.userName}")
}

private fun navigateNext() {
startActivity<SecondActivity> {
SecondActivity::userName to "Ivo Bobul"
SecondActivity::age to 99
SecondActivity::code to 65536
}
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/ds/bindingtools/demo/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ds.bindingtools.demo

import ds.bindingtools.Bindable
import ds.bindingtools.binding

class MainViewModel : Bindable {
var text: String by binding("")

fun sayHello() {
text = "Hello, World!"
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/ds/bindingtools/demo/Prefs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2016. Deviant Studio
*/

package ds.bindingtools.demo

import android.content.Context
import android.content.SharedPreferences
import ds.bindingtools.PreferencesAware
import ds.bindingtools.pref

class Prefs(ctx: Context) : PreferencesAware {

override val forcePersistDefaults = true
override val sharedPreferences: SharedPreferences = ctx.getSharedPreferences("main_prefs", Context.MODE_PRIVATE)

var age: Int by pref(0)
var userName: String by pref("")
var isAdmin by pref(true)

}
24 changes: 24 additions & 0 deletions app/src/main/java/ds/bindingtools/demo/SecondActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ds.bindingtools.demo

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

class SecondActivity : AppCompatActivity() {

val userName: String by arg("")
val age: Int by arg(0)
val country: String? by arg()
val code: Int? by arg()

private val appName by res<String>(R.string.app_name)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)

println(appName)
println("$userName $age $country $code")
}
}
23 changes: 23 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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.MainActivity"
android:orientation="vertical">

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="navigate"/>

</LinearLayout>
10 changes: 10 additions & 0 deletions app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.2.0-beta-31'
ext.kotlin_version = '1.2.0-beta-88'
repositories {
google()
jcenter()
Expand Down
3 changes: 0 additions & 3 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ android {
compileSdkVersion 26
buildToolsVersion "26.0.2"


defaultConfig {
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}

buildTypes {
Expand Down
Loading

0 comments on commit 58c8169

Please sign in to comment.