diff --git a/README.md b/README.md index 8c78434..af41fb5 100644 --- a/README.md +++ b/README.md @@ -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) + ``` \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 333d74a..a339499 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 @@ -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' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1724fe5..51d9e26 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,10 +1,23 @@ + + package="ds.bindingtools.demo"> - - + android:theme="@style/AppTheme"> + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/ds/bindingtools/demo/MainActivity.kt b/app/src/main/java/ds/bindingtools/demo/MainActivity.kt new file mode 100644 index 0000000..f060ae0 --- /dev/null +++ b/app/src/main/java/ds/bindingtools/demo/MainActivity.kt @@ -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