Last active
December 21, 2017 12:29
-
-
Save PrashamTrivedi/bcfd349350debf86d38d2aa346a8b38a to your computer and use it in GitHub Desktop.
Files to reproduce databinding error in 3.1.0-alpha06
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
<?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"> | |
<data> | |
<variable | |
name="login" | |
type="com.test.android.login.viewmodels.LoginViewmodel" /> | |
</data> | |
<android.support.constraint.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="?colorPrimary"> | |
<android.support.v7.widget.Toolbar | |
android:id="@+id/toolbar" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="8dp" | |
android:layout_marginEnd="8dp" | |
android:layout_marginStart="8dp" | |
android:layout_marginTop="8dp" | |
android:background="?attr/colorPrimary" | |
android:minHeight="?attr/actionBarSize" | |
android:theme="?attr/actionBarTheme" | |
app:layout_constraintBottom_toTopOf="@+id/cardView" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintVertical_chainStyle="packed"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:gravity="bottom" | |
android:orientation="horizontal" | |
android:paddingEnd="5dp"> | |
<ImageView | |
android:id="@+id/img_college_logo" | |
android:layout_width="85dp" | |
android:layout_height="85dp" | |
android:layout_gravity="bottom|center_vertical" | |
android:src="@mipmap/ic_launcher" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_vertical" | |
android:layout_marginStart="12dp" | |
android:text="Welcome!" | |
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" /> | |
</LinearLayout> | |
</android.support.v7.widget.Toolbar> | |
<android.support.v7.widget.CardView | |
android:id="@+id/cardView" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="8dp" | |
android:layout_marginEnd="8dp" | |
android:layout_marginStart="8dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/toolbar"> | |
<android.support.constraint.ConstraintLayout | |
android:id="@+id/formParent" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:animateLayoutChanges="true"> | |
<android.support.design.widget.TextInputLayout | |
android:id="@+id/txtInputEmail" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="8dp" | |
app:error="@{login.userNameError}" | |
app:errorEnabled="true" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent"> | |
<android.support.design.widget.TextInputEditText | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Email" | |
android:inputType="textEmailAddress" | |
android:text="@={login.userName}" /> | |
<requestFocus /> | |
</android.support.design.widget.TextInputLayout> | |
<android.support.design.widget.TextInputLayout | |
android:id="@+id/txtInputPassword" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:error="@{login.passwordError}" | |
app:errorEnabled="true" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/txtInputEmail" | |
app:passwordToggleEnabled="true"> | |
<android.support.design.widget.TextInputEditText | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Password" | |
android:inputType="textPassword" | |
android:maxLines="1" | |
android:text="@={login.password}" /> | |
</android.support.design.widget.TextInputLayout> | |
<Button | |
android:id="@+id/btnSignIn" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginEnd="0dp" | |
android:layout_marginStart="0dp" | |
android:text="Sign In" | |
android:textColor="@android:color/white" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/txtInputPassword" /> | |
<ProgressBar | |
android:id="@+id/progressBar" | |
style="?android:attr/progressBarStyle" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginEnd="0dp" | |
android:layout_marginStart="0dp" | |
android:visibility="gone" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/txtInputPassword" /> | |
</android.support.constraint.ConstraintLayout> | |
</android.support.v7.widget.CardView> | |
</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
class LoginActivity : AppCompatActivity() { | |
@Inject lateinit var loginPresenter: LoginPresenter | |
lateinit var binding: ActivityLoginBinding | |
val loginViewModel: LoginViewmodel by lazy { | |
LoginViewmodel() | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
AndroidInjection.inject(this) | |
super.onCreate(savedInstanceState) | |
binding = DataBindingUtil.setContentView(this, R.layout.activity_login) | |
binding.login = loginViewModel | |
btnSignIn.setOnClickListener { | |
if (loginViewModel.isValid) { | |
loginPresenter.doLogin(loginViewModel.loginModel) | |
} | |
} | |
observe(loginPresenter.loginResponse) { | |
when (it) { | |
is Loading -> showLoading() | |
is SuccessData<LoginResponse> -> handleData(it.data) | |
is WebserviceError -> handleError(it.throwable) | |
} | |
} | |
} | |
private fun handleError(throwable: Throwable) { | |
hideLoading() | |
} | |
private fun handleData(data: LoginResponse?) { | |
hideLoading() | |
d("Login resp: $data") | |
} | |
private fun showLoading() { | |
btnSignIn.setVisible(false) | |
progressBar.setVisible(true) | |
} | |
private fun hideLoading() { | |
btnSignIn.setVisible(true) | |
progressBar.setVisible(false) | |
} | |
} |
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
class LoginViewmodel : BaseObservable() { | |
var userName: String = "" | |
@Bindable get | |
var password: String = "" | |
@Bindable get | |
var userNameError: String = "" | |
@Bindable get | |
var passwordError: String = "" | |
@Bindable get | |
val isValid: Boolean | |
get() { | |
userNameError = "" | |
notifyPropertyChanged(BR.userNameError) | |
passwordError = "" | |
notifyPropertyChanged(BR.passwordError) | |
return when { | |
userName.isEmptyString() -> { | |
userNameError = "Please enter username" | |
notifyPropertyChanged(BR.userNameError) | |
false | |
} | |
password.isEmptyString() -> { | |
passwordError = "Please enter password" | |
notifyPropertyChanged(BR.passwordError) | |
false | |
} | |
else -> true | |
} | |
} | |
val loginModel: LoginRequest | |
get() = LoginRequest(userName = userName, password = password) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment