Use LiveData and ViewModel for ManageSecurityToken fragment

This hopefully also prevents some NPEs from google play crash reports.
This commit is contained in:
Vincent Breitmoser
2018-07-13 13:21:36 +02:00
parent 6ed110f1ee
commit 4ea8c4a8f6
6 changed files with 450 additions and 477 deletions

View File

@@ -3,6 +3,7 @@ package org.sufficientlysecure.keychain.livedata;
import android.content.Context;
import android.net.Uri;
import android.os.SystemClock;
import org.sufficientlysecure.keychain.daos.DatabaseNotifyManager;
import org.sufficientlysecure.keychain.ui.keyview.loader.AsyncTaskLiveData;
@@ -10,6 +11,7 @@ import org.sufficientlysecure.keychain.ui.keyview.loader.AsyncTaskLiveData;
public class GenericLiveData<T> extends AsyncTaskLiveData<T> {
private GenericDataLoader<T> genericDataLoader;
private Long minLoadTime;
public GenericLiveData(Context context, GenericDataLoader<T> genericDataLoader) {
super(context, null);
@@ -26,12 +28,30 @@ public class GenericLiveData<T> extends AsyncTaskLiveData<T> {
this.genericDataLoader = genericDataLoader;
}
public void setMinLoadTime(Long minLoadTime) {
this.minLoadTime = minLoadTime;
}
@Override
protected T asyncLoadData() {
return genericDataLoader.loadData();
long startTime = SystemClock.elapsedRealtime();
T result = genericDataLoader.loadData();
try {
long elapsedTime = SystemClock.elapsedRealtime() - startTime;
if (minLoadTime != null && elapsedTime < minLoadTime) {
Thread.sleep(minLoadTime - elapsedTime);
}
} catch (InterruptedException e) {
// nvm
}
return result;
}
public interface GenericDataLoader<T> {
T loadData();
}
}