MainViewModel.java 5.4 KB
Newer Older
A
amitshekhariitbhu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 *  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
 */

17
package com.mindorks.framework.mvvm.ui.main;
A
amitshekhariitbhu 已提交
18

V
Vignesh150493 已提交
19
import android.arch.lifecycle.MutableLiveData;
20
import android.databinding.ObservableArrayList;
21 22
import android.databinding.ObservableField;

A
amitshekhariitbhu 已提交
23
import com.mindorks.framework.mvvm.data.DataManager;
24
import com.mindorks.framework.mvvm.data.model.api.LogoutResponse;
25
import com.mindorks.framework.mvvm.data.model.others.QuestionCardData;
26
import com.mindorks.framework.mvvm.ui.base.BaseViewModel;
27 28 29
import com.mindorks.framework.mvvm.utils.rx.SchedulerProvider;

import java.util.List;
A
amitshekhariitbhu 已提交
30

31
import io.reactivex.functions.Consumer;
A
amitshekhariitbhu 已提交
32

33 34 35
/**
 * Created by amitshekhar on 07/07/17.
 */
A
amitshekhariitbhu 已提交
36

A
amitshekhariitbhu 已提交
37
public class MainViewModel extends BaseViewModel<MainNavigator> {
38

39 40
    public static final int NO_ACTION = -1, ACTION_ADD_ALL = 0, ACTION_DELETE_SINGLE = 1;

V
Vignesh150493 已提交
41 42 43 44 45 46 47 48 49 50
    private final ObservableField<String> appVersion = new ObservableField<>();
    private final ObservableField<String> userName = new ObservableField<>();
    private final ObservableField<String> userEmail = new ObservableField<>();
    private final ObservableField<String> userProfilePicUrl = new ObservableField<>();
    private final ObservableArrayList<QuestionCardData> questionDataList = new ObservableArrayList<>();

    private final MutableLiveData<List<QuestionCardData>> questionCardData;

    private int action = NO_ACTION;

A
amitshekhariitbhu 已提交
51
    public MainViewModel(DataManager dataManager,
52 53
                         SchedulerProvider schedulerProvider) {
        super(dataManager, schedulerProvider);
V
Vignesh150493 已提交
54 55
        questionCardData = new MutableLiveData<>();
        loadQuestionCards();
56 57
    }

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    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);
        }
    }

V
Vignesh150493 已提交
80
    public void loadQuestionCards() {
81
        getCompositeDisposable().add(getDataManager()
82
                .getQuestionCardData()
83 84
                .subscribeOn(getSchedulerProvider().io())
                .observeOn(getSchedulerProvider().ui())
85
                .subscribe(new Consumer<List<QuestionCardData>>() {
86
                    @Override
87
                    public void accept(List<QuestionCardData> questionList) throws Exception {
88
                        if (questionList != null) {
V
Vignesh150493 已提交
89 90
                            action = ACTION_ADD_ALL;
                            questionCardData.setValue(questionList);
91 92 93
                        }
                    }
                }));
94 95
    }

V
Vignesh150493 已提交
96

97
    public void logout() {
98
        setIsLoading(true);
99 100 101 102 103 104 105
        getCompositeDisposable().add(getDataManager().doLogoutApiCall()
                .subscribeOn(getSchedulerProvider().io())
                .observeOn(getSchedulerProvider().ui())
                .subscribe(new Consumer<LogoutResponse>() {
                    @Override
                    public void accept(LogoutResponse response) throws Exception {
                        getDataManager().setUserAsLoggedOut();
106
                        setIsLoading(false);
A
amitshekhariitbhu 已提交
107
                        getNavigator().openLoginActivity();
108 109 110 111
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
112
                        setIsLoading(false);
A
amitshekhariitbhu 已提交
113
                        getNavigator().handleError(throwable);
114 115 116 117
                    }
                }));
    }

118
    public void removeQuestionCard() {
V
Vignesh150493 已提交
119
        action = ACTION_DELETE_SINGLE;
120
        questionDataList.remove(0);
V
Vignesh150493 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        questionCardData.getValue().remove(0);
    }

    public void setQuestionDataList(List<QuestionCardData> questionCardDatas) {
        action = ACTION_ADD_ALL;
        questionDataList.clear();
        questionDataList.addAll(questionCardDatas);
    }

    public MutableLiveData<List<QuestionCardData>> getQuestionCardData() {
        return questionCardData;
    }

    public ObservableField<String> getAppVersion() {
        return appVersion;
    }

    public ObservableField<String> getUserName() {
        return userName;
    }

    public ObservableField<String> getUserEmail() {
        return userEmail;
    }

    public ObservableField<String> getUserProfilePicUrl() {
        return userProfilePicUrl;
    }

    public ObservableArrayList<QuestionCardData> getQuestionDataList() {
        return questionDataList;
    }

    public int getAction() {
        return action;
156
    }
A
amitshekhariitbhu 已提交
157
}