I am trying to check that if user is logged on or not. If yes then show a specific view group otherwise show different view group. To check whether user is logged on or not I am fetching the user from shared preference (at the time of login user is saved in shared preference). Let me show my code.
SplashViewModel
public class SplashViewModel extends ViewModel {
public final String TAG = "SplashViewModel";
private final String GREETING = "Hi ";
public ObservableBoolean isLoggedIn = new ObservableBoolean();
public ObservableField<String> userName = new ObservableField<>("");
private Preference<User> preference;
SplashViewModel(Preference preference) {
this.preference = preference;
isLoggedIn.set(false);
}
public void getCurrentUser() {
preference.get(Constants.CURRENT_USER, User.class)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(user -> {
isLoggedIn.set(true);
userName.set(GREETING + user.name);
}, throwable -> isLoggedIn.set(false));
}
}
activity_splash
<?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"
tools:context="com.sevenbits.android.mvvmsample.view.SplashActivity">
<data>
<variable name="splashViewModel"
type="com.sevenbits.android.mvvmsample.viewmodel.SplashViewModel"/>
</data>
<android.support.constraint.ConstraintLayout
android:id="@+id/parent1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_bg">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/loggedin_layout"
android:visibility="@{splashViewModel.isLoggedIn ? View.VISIBLE : View.GONE,default=gone}">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="@{splashViewModel.userName}"
android:textColor="@color/white"
android:gravity="center"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0.40"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="32sp"
android:text="Welcome Back"
android:textColor="@color/white"
android:gravity="center"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0.50"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Start a New Game >>"
android:textColor="@color/white"
android:gravity="center"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0.70"/>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/loggedout_layout"
android:visibility="@{splashViewModel.isLoggedIn ? View.GONE : View.VISIBLE}"
>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_button"
android:gravity="center"
android:text="Login"
android:textColor="@color/white"
android:textSize="18sp"
android:id="@+id/login_button"
android:paddingStart="40dp"
android:paddingEnd="40dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.33"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_button"
android:gravity="center"
android:text="Sign Up"
android:textColor="@color/white"
android:textSize="18sp"
android:id="@+id/sign_up_button"
android:paddingStart="40dp"
android:paddingEnd="40dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_button"
android:gravity="center"
android:text="View Scores"
android:textColor="@color/white"
android:textSize="18sp"
android:id="@+id/view_scores_button"
android:paddingStart="40dp"
android:paddingEnd="40dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
app:layout_constraintVertical_bias="0.66"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:textColor="@color/white"
android:textAllCaps="false"
android:textSize="18sp"
android:id="@+id/guest_button"
android:layout_marginBottom="20dp"
android:text="Play As a Guest User"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</layout>
SplashActivity
public class SplashActivity extends AppCompatActivity {
SplashViewModel splashViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initDataBinding();
setButtonClickListeners();
}
@Override
protected void onStart() {
super.onStart();
splashViewModel.getCurrentUser();
}
private void initDataBinding() {
ActivitySplashBinding activitySplashBinding = DataBindingUtil.setContentView(this, R.layout.activity_splash);
SplashViewModelFactory splashViewModelFactory = Injection.provideSplashViewModelFactory(this);
splashViewModel = ViewModelProviders.of(this, splashViewModelFactory).get(SplashViewModel.class);
activitySplashBinding.setSplashViewModel(splashViewModel);
// splashViewModel.getCurrentUser();
}
private void setButtonClickListeners() {
findViewById(R.id.guest_button).setOnClickListener(v -> {
startActivity(new Intent(SplashActivity.this, GameActivity.class));
finish();
});
findViewById(R.id.sign_up_button).setOnClickListener(v -> startActivity(
new Intent(SplashActivity.this, RegisterActivity.class)));
findViewById(R.id.login_button).setOnClickListener(v -> startActivity(
new Intent(SplashActivity.this, LoginActivity.class)));
findViewById(R.id.view_scores_button).setOnClickListener(v -> startActivity(
new Intent(SplashActivity.this, ScoresActivity.class)));
}
}
But when I try to run, it gives me the following error.
error: cannot find symbol class ActivitySplashBinding
Can anyone help me with it?