-
Notifications
You must be signed in to change notification settings - Fork 0
barebones to get live data and data binding working #1
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
Open
ahaly92
wants to merge
7
commits into
master
Choose a base branch
from
ahmed/live-data
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2e810f8
barebones to get live data and data binding working
ahaly92 11bf02c
Update README.md
ahaly92 3c8f87b
fixing PR comments related to formatting and syntax
ahaly92 8b17cb4
revert back to using let
ahaly92 e224ec3
move logic into view model
ahaly92 6e89b08
tying the livedata back to the view for demo sake
ahaly92 bf8cc6d
fix to bind to the result editText
ahaly92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 +1,7 @@ | ||
| # LiveData | ||
| Playground for setting up LiveData | ||
| # LiveDataBinding Playground | ||
|
|
||
| Architecture: MVVM | ||
| Language: Kotlin | ||
| Android Architecture Components: LiveData, Data Binding | ||
|
|
||
| A very bare bone example of using MVVM with a couple of android architectural components |
This file contains hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| package com.example.livedata | ||
|
|
||
| import android.arch.lifecycle.LiveData | ||
| import android.arch.lifecycle.MutableLiveData | ||
| import android.arch.lifecycle.ViewModel | ||
|
|
||
| open class BaseViewModel : ViewModel() { | ||
| protected var <T : Any> LiveData<T>.latestValue: T? | ||
| get() = this.value | ||
| set(value) { | ||
| (this as MutableLiveData<T>).value = value | ||
| } | ||
| } | ||
This file contains hidden or 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,69 @@ | ||
| package com.example.livedata | ||
|
|
||
| import android.arch.lifecycle.LiveData | ||
| import android.arch.lifecycle.Observer | ||
| import android.arch.lifecycle.ViewModelProviders | ||
| import android.os.Bundle | ||
| import android.support.v4.app.Fragment | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.EditText | ||
| import com.example.livedata.LoginErrorType.EmailEmpty | ||
| import com.example.livedata.LoginErrorType.PasswordEmpty | ||
| import com.example.livedata.LoginErrorType.PasswordShort | ||
| import com.example.livedata.databinding.FragmentLoginBinding | ||
| import kotlinx.android.synthetic.main.fragment_login.password | ||
| import kotlinx.android.synthetic.main.fragment_login.resultEmailAddress | ||
| import kotlinx.android.synthetic.main.fragment_login.resultPassword | ||
| import kotlinx.android.synthetic.main.fragment_login.username | ||
|
|
||
| class LoginFragment : Fragment() { | ||
| private lateinit var viewModel: LoginViewModel | ||
| private lateinit var binding: FragmentLoginBinding | ||
|
|
||
| override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
| super.onCreate(savedInstanceState) | ||
| viewModel = ViewModelProviders.of(this).get(LoginViewModel::class.java) | ||
| binding = FragmentLoginBinding.inflate(inflater) | ||
| return binding.apply { | ||
| lifecycleOwner = viewLifecycleOwner | ||
| loginViewModel = viewModel | ||
|
|
||
| }.root | ||
| } | ||
|
|
||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| bindViewModel() | ||
| } | ||
|
|
||
| private fun bindViewModel() { | ||
| username.bindError(viewModel.emailError) | ||
| password.bindError(viewModel.passwordError) | ||
|
|
||
| //TODO: remove this logic and the result views when actually implementing login | ||
| viewModel.user.observe(this, Observer { | ||
| it?.apply { | ||
| resultEmailAddress.text = username | ||
| resultPassword.text = password | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| private fun EditText.bindError(liveDataError: LiveData<LoginErrorType>) { | ||
| liveDataError.observe(viewLifecycleOwner, Observer { | ||
| it?.let { loginErrorType -> this.setErrorOnField(loginErrorType) } | ||
| }) | ||
| } | ||
|
|
||
| private fun EditText.setErrorOnField(errorType: LoginErrorType) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! Sealed class 🔥 |
||
| error = | ||
| when (errorType) { | ||
| is EmailEmpty -> getString(R.string.empty_email_error) | ||
| is PasswordEmpty -> getString(R.string.empty_password_error) | ||
| is PasswordShort -> getString(R.string.password_too_short_error) | ||
| } | ||
| requestFocus() | ||
| } | ||
| } | ||
This file contains hidden or 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,33 @@ | ||
| package com.example.livedata | ||
|
|
||
| import android.arch.lifecycle.LiveData | ||
| import android.arch.lifecycle.MutableLiveData | ||
| import com.example.livedata.LoginErrorType.EmailEmpty | ||
| import com.example.livedata.LoginErrorType.PasswordEmpty | ||
| import com.example.livedata.LoginErrorType.PasswordShort | ||
|
|
||
| const val PASSWORD_LENGTH = 5 | ||
|
|
||
| class LoginViewModel : BaseViewModel() { | ||
| val password = MutableLiveData<String>() | ||
| val username = MutableLiveData<String>() | ||
| val emailError: LiveData<LoginErrorType> = MutableLiveData<LoginErrorType>() | ||
| val passwordError: LiveData<LoginErrorType> = MutableLiveData<LoginErrorType>() | ||
|
|
||
| var user: LiveData<User> = MutableLiveData<User>() | ||
|
|
||
| fun onClick() { | ||
| when { | ||
| username.value.isNullOrEmpty() -> emailError.latestValue = EmailEmpty | ||
| password.value.isNullOrEmpty() -> passwordError.latestValue = PasswordEmpty | ||
| password.value.orEmpty().length < PASSWORD_LENGTH -> passwordError.latestValue = PasswordShort | ||
| else -> user.latestValue = User(username.value.orEmpty(), password.value.orEmpty()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| sealed class LoginErrorType { | ||
| object EmailEmpty : LoginErrorType() | ||
| object PasswordShort : LoginErrorType() | ||
| object PasswordEmpty : LoginErrorType() | ||
| } |
This file contains hidden or 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,12 +1,16 @@ | ||
| package com.example.livedata | ||
|
|
||
| import android.support.v7.app.AppCompatActivity | ||
| import android.os.Bundle | ||
| import android.support.v7.app.AppCompatActivity | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_main) | ||
| val fragment = LoginFragment() | ||
| supportFragmentManager | ||
| .beginTransaction() | ||
| .add(R.id.container, fragment) | ||
| .commit() | ||
| } | ||
| } |
This file contains hidden or 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,6 @@ | ||
| package com.example.livedata | ||
|
|
||
| data class User( | ||
| val username: String, | ||
| val password: String | ||
| ) |
This file contains hidden or 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,21 +1,15 @@ | ||
| <?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=".MainActivity" | ||
| > | ||
|
|
||
| <TextView | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="Hello World!" | ||
| app:layout_constraintBottom_toBottomOf="parent" | ||
| app:layout_constraintLeft_toLeftOf="parent" | ||
| app:layout_constraintRight_toRightOf="parent" | ||
| app:layout_constraintTop_toTopOf="parent" | ||
| <FrameLayout | ||
| android:id="@+id/container" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| /> | ||
|
|
||
| </android.support.constraint.ConstraintLayout> | ||
| </android.support.constraint.ConstraintLayout> |
This file contains hidden or 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,109 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <layout 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" | ||
| > | ||
|
|
||
| <data> | ||
|
|
||
| <variable | ||
| name="loginViewModel" | ||
| type="com.example.livedata.LoginViewModel" | ||
| /> | ||
| </data> | ||
|
|
||
|
|
||
| <android.support.constraint.ConstraintLayout | ||
| android:layout_width="match_parent" | ||
| android:layout_height="match_parent" | ||
| android:padding="@dimen/spacing_md" | ||
| tools:context=".View.MainActivity" | ||
| > | ||
|
|
||
| <EditText | ||
| android:id="@+id/username" | ||
| android:layout_width="0dp" | ||
| android:layout_height="wrap_content" | ||
| android:autofillHints="username" | ||
| android:ems="10" | ||
| android:hint="@string/username_field_placeholder" | ||
| android:text="@={loginViewModel.username}" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toTopOf="parent" | ||
| tools:ignore="UnusedAttribute" | ||
| /> | ||
|
|
||
| <EditText | ||
| android:id="@+id/password" | ||
| android:layout_width="0dp" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginTop="@dimen/spacing_md" | ||
| android:autofillHints="password" | ||
| android:ems="10" | ||
| android:hint="@string/password_field_hint" | ||
| android:inputType="textPassword" | ||
| android:text="@={loginViewModel.password}" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toBottomOf="@+id/username" | ||
| tools:targetApi="o" | ||
| /> | ||
|
|
||
| <Button | ||
| android:id="@+id/loginButton" | ||
| android:layout_width="0dp" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginTop="@dimen/spacing_lg" | ||
| android:onClick="@{() -> loginViewModel.onClick()}" | ||
| android:text="@string/login_button_text" | ||
| android:textSize="@dimen/text_md" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toBottomOf="@+id/password" | ||
| /> | ||
|
|
||
| <TextView | ||
| android:id="@+id/resultsTitle" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginStart="@dimen/spacing_sm" | ||
| android:layout_marginTop="@dimen/spacing_xlg" | ||
| android:layout_marginEnd="@dimen/spacing_sm" | ||
| android:gravity="center" | ||
| android:text="@string/live_data_results_title" | ||
| android:textColor="@android:color/background_dark" | ||
| android:textSize="@dimen/text_md" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toBottomOf="@+id/loginButton" | ||
| /> | ||
|
|
||
| <TextView | ||
| android:id="@+id/resultEmailAddress" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginTop="@dimen/spacing_sm" | ||
| android:text="@string/place_holder_text" | ||
| android:textColor="@android:color/holo_blue_light" | ||
| android:textSize="@dimen/text_md" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toBottomOf="@+id/resultsTitle" | ||
| /> | ||
|
|
||
| <TextView | ||
| android:id="@+id/resultPassword" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:layout_marginTop="@dimen/spacing_sm" | ||
| android:text="@string/place_holder_text" | ||
| android:textColor="@android:color/holo_blue_light" | ||
| android:textSize="@dimen/text_md" | ||
| app:layout_constraintEnd_toEndOf="parent" | ||
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toBottomOf="@+id/resultEmailAddress" | ||
| /> | ||
|
|
||
| </android.support.constraint.ConstraintLayout> | ||
| </layout> |
This file contains hidden or 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,9 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <resources> | ||
| <dimen name="spacing_sm">8dp</dimen> | ||
| <dimen name="spacing_md">16dp</dimen> | ||
| <dimen name="spacing_lg">32dp</dimen> | ||
| <dimen name="spacing_xlg">60dp</dimen> | ||
|
|
||
| <dimen name="text_md">20sp</dimen> | ||
| </resources> |
This file contains hidden or 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,3 +1,11 @@ | ||
| <resources> | ||
| <string name="app_name">LiveData</string> | ||
| <string name="live_data_results_title">Results of LiveDataBinding</string> | ||
| <string name="place_holder_text">– –</string> | ||
| <string name="login_button_text">Login</string> | ||
| <string name="password_field_hint">Password</string> | ||
| <string name="username_field_placeholder">Username</string> | ||
| <string name="empty_email_error">Enter an E-Mail Address</string> | ||
| <string name="empty_password_error">Enter a Password</string> | ||
| <string name="password_too_short_error">Enter at least 6 Digit password</string> | ||
| </resources> |
This file contains hidden or 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,11 +1,7 @@ | ||
| <resources> | ||
|
|
||
| <!-- Base application theme. --> | ||
| <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> | ||
| <!-- Customize your theme here. --> | ||
| <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | ||
| <item name="colorPrimary">@color/colorPrimary</item> | ||
| <item name="colorPrimaryDark">@color/colorPrimaryDark</item> | ||
| <item name="colorAccent">@color/colorAccent</item> | ||
| </style> | ||
|
|
||
| </resources> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new line