/* * Copyright (C) 2017 MINDORKS NEXTGEN PRIVATE LIMITED * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://mindorks.com/license/apache-v2 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License */ package com.mindorks.framework.mvvm.ui.main; import android.arch.lifecycle.MutableLiveData; import android.databinding.ObservableArrayList; import android.databinding.ObservableField; import com.mindorks.framework.mvvm.data.DataManager; import com.mindorks.framework.mvvm.data.model.api.LogoutResponse; import com.mindorks.framework.mvvm.data.model.others.QuestionCardData; import com.mindorks.framework.mvvm.ui.base.BaseViewModel; import com.mindorks.framework.mvvm.utils.rx.SchedulerProvider; import java.util.List; import io.reactivex.functions.Consumer; /** * Created by amitshekhar on 07/07/17. */ public class MainViewModel extends BaseViewModel { public static final int NO_ACTION = -1, ACTION_ADD_ALL = 0, ACTION_DELETE_SINGLE = 1; private final ObservableField appVersion = new ObservableField<>(); private final ObservableField userName = new ObservableField<>(); private final ObservableField userEmail = new ObservableField<>(); private final ObservableField userProfilePicUrl = new ObservableField<>(); private final ObservableArrayList questionDataList = new ObservableArrayList<>(); private final MutableLiveData> questionCardData; private int action = NO_ACTION; public MainViewModel(DataManager dataManager, SchedulerProvider schedulerProvider) { super(dataManager, schedulerProvider); questionCardData = new MutableLiveData<>(); loadQuestionCards(); } public void updateAppVersion(String version) { appVersion.set(version); } public void onNavMenuCreated() { final String currentUserName = getDataManager().getCurrentUserName(); if (currentUserName != null && !currentUserName.isEmpty()) { userName.set(currentUserName); } final String currentUserEmail = getDataManager().getCurrentUserEmail(); if (currentUserEmail != null && !currentUserEmail.isEmpty()) { userEmail.set(currentUserEmail); } final String profilePicUrl = getDataManager().getCurrentUserProfilePicUrl(); if (profilePicUrl != null && !profilePicUrl.isEmpty()) { userProfilePicUrl.set(profilePicUrl); } } public void loadQuestionCards() { getCompositeDisposable().add(getDataManager() .getQuestionCardData() .subscribeOn(getSchedulerProvider().io()) .observeOn(getSchedulerProvider().ui()) .subscribe(new Consumer>() { @Override public void accept(List questionList) throws Exception { if (questionList != null) { action = ACTION_ADD_ALL; questionCardData.setValue(questionList); } } })); } public void logout() { setIsLoading(true); getCompositeDisposable().add(getDataManager().doLogoutApiCall() .subscribeOn(getSchedulerProvider().io()) .observeOn(getSchedulerProvider().ui()) .subscribe(new Consumer() { @Override public void accept(LogoutResponse response) throws Exception { getDataManager().setUserAsLoggedOut(); setIsLoading(false); getNavigator().openLoginActivity(); } }, new Consumer() { @Override public void accept(Throwable throwable) throws Exception { setIsLoading(false); getNavigator().handleError(throwable); } })); } public void removeQuestionCard() { action = ACTION_DELETE_SINGLE; questionDataList.remove(0); questionCardData.getValue().remove(0); } public void setQuestionDataList(List questionCardDatas) { action = ACTION_ADD_ALL; questionDataList.clear(); questionDataList.addAll(questionCardDatas); } public MutableLiveData> getQuestionCardData() { return questionCardData; } public ObservableField getAppVersion() { return appVersion; } public ObservableField getUserName() { return userName; } public ObservableField getUserEmail() { return userEmail; } public ObservableField getUserProfilePicUrl() { return userProfilePicUrl; } public ObservableArrayList getQuestionDataList() { return questionDataList; } public int getAction() { return action; } }