-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e48b71e
commit 58c8169
Showing
12 changed files
with
339 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.