Skip to content

Instantly share code, notes, and snippets.

@nick45chen
Last active August 13, 2019 09:45
Show Gist options
  • Save nick45chen/12044561a95e38fc8b535419531050d0 to your computer and use it in GitHub Desktop.
Save nick45chen/12044561a95e38fc8b535419531050d0 to your computer and use it in GitHub Desktop.
版本更新ViewModel
/**
* 版本檢查 ViewModel
*/
/**
* 版本檢查 ViewModel
*/
public class VersionViewModel extends ViewModel {
private IVersionRepository repository;
private MutableLiveData<Boolean> isLoading = new MutableLiveData<>();
private MutableLiveData<String> alertMessage = new MutableLiveData<>();
private MutableLiveData<String> notUpdateMessage = new MutableLiveData<>();
private MutableLiveData<String> notForceUpdateMessage = new MutableLiveData<>();
private MutableLiveData<String> forceUpdateMessage = new MutableLiveData<>();
public void injectDependency(IVersionRepository repository) {
this.repository = repository;
}
// 是否顯示加載Loading Dialog
public LiveData<Boolean> observeLoadStateData() {
return isLoading;
}
// 錯誤訊息
public LiveData<String> observeAlertMessageData() {
return alertMessage;
}
// 不需更新
public LiveData<String> observeNotUpdateMessageData() {
return notUpdateMessage;
}
// 不強制更新
public LiveData<String> observeNotForceUpdateMessageData() {
return notForceUpdateMessage;
}
// 強制更新
public LiveData<String> observeForceUpdateMessageData() {
return forceUpdateMessage;
}
public void checkVersion() {
repository.getVersionInfo(new IApiCallback<VersionEntity>() {
@Override
public void onStart() {
isLoading.postValue(true);
}
@Override
public void onSuccess(VersionEntity data) {
verifyVersionInfo(data);
}
@Override
public void onFail(Throwable throwable) {
alertMessage.postValue(throwable.getMessage());
}
@Override
public void onCompleted() {
isLoading.postValue(false);
}
});
}
private void verifyVersionInfo(VersionEntity entity) {
if (entity == null) {
return;
}
if (entity.isNeedToUpdate()) {
if (entity.isForceUpdate()) {
forceUpdateMessage.postValue(StayFunApplication.getContext().getString(R.string.message_found_new_version_force));
} else {
final long currentTime = System.currentTimeMillis();
long lastRemindTime = SysUtil.getPrefLong(Config.PREF_LAST_REMIND_UPDATE_TIME_IN_MILLIS, 0, StayFunApplication.getContext());
// 一天提示一次
if ((currentTime - lastRemindTime) <= 86400 * 1000) {
notUpdateMessage.postValue("已經提示過更新了");
} else {
// 儲存提示時間
SysUtil.setPrefLong(Config.PREF_LAST_REMIND_UPDATE_TIME_IN_MILLIS, currentTime, StayFunApplication.getContext());
notForceUpdateMessage.postValue(StayFunApplication.getContext().getString(R.string.message_found_new_version));
}
}
} else {
notUpdateMessage.postValue("不需更新");
}
}
}
@ininmm
Copy link

ininmm commented Aug 8, 2019

private MutableLiveData<Boolean> isLoading = new MutableLiveData<>();

public LiveData getLoadingData() {
    return isLoading;
}

public void checkVersion() {
    repository.getVersionInfo(new IApiCallback<VersionEntity>() {
        @Override
        public void onStart() {
            isLoading.postValue(true);
        }

        @Override
        public void onSuccess(VersionEntity data) {
            verifyVersionInfo(data);
        }

        @Override
        public void onFail(Throwable throwable) {
            alertMessage.postValue(throwable.getMessage());
        }

        @Override
        public void onCompleted() {
            isLoading.postValue(false);
        }
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment