mirror of https://github.com/tasks/tasks
Convert CompletableViewModels to Kotlin
parent
919ba42098
commit
dbba2e0e6f
@ -1,12 +0,0 @@
|
||||
package org.tasks.activities;
|
||||
|
||||
import com.google.api.services.tasks.model.TaskList;
|
||||
import com.todoroo.astrid.gtasks.api.GtasksInvoker;
|
||||
import org.tasks.ui.CompletableViewModel;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class CreateListViewModel extends CompletableViewModel<TaskList> {
|
||||
void createList(GtasksInvoker invoker, String account, String name) {
|
||||
run(() -> invoker.forAccount(account).createGtaskList(name));
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package org.tasks.activities
|
||||
|
||||
import com.google.api.services.tasks.model.TaskList
|
||||
import com.todoroo.astrid.gtasks.api.GtasksInvoker
|
||||
import org.tasks.ui.CompletableViewModel
|
||||
|
||||
class CreateListViewModel : CompletableViewModel<TaskList?>() {
|
||||
fun createList(invoker: GtasksInvoker, account: String?, name: String?) {
|
||||
run { invoker.forAccount(account!!).createGtaskList(name) }
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package org.tasks.activities;
|
||||
|
||||
import com.google.api.services.tasks.model.TaskList;
|
||||
import com.todoroo.astrid.gtasks.api.GtasksInvoker;
|
||||
import org.tasks.data.GoogleTaskList;
|
||||
import org.tasks.ui.CompletableViewModel;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class RenameListViewModel extends CompletableViewModel<TaskList> {
|
||||
void renameList(GtasksInvoker invoker, GoogleTaskList list, String name) {
|
||||
run(() -> invoker.forAccount(list.getAccount()).renameGtaskList(list.getRemoteId(), name));
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package org.tasks.activities
|
||||
|
||||
import com.google.api.services.tasks.model.TaskList
|
||||
import com.todoroo.astrid.gtasks.api.GtasksInvoker
|
||||
import org.tasks.data.GoogleTaskList
|
||||
import org.tasks.ui.CompletableViewModel
|
||||
|
||||
class RenameListViewModel : CompletableViewModel<TaskList?>() {
|
||||
fun renameList(invoker: GtasksInvoker, list: GoogleTaskList, name: String?) {
|
||||
run { invoker.forAccount(list.account!!).renameGtaskList(list.remoteId, name) }
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package org.tasks.caldav;
|
||||
|
||||
import org.tasks.ui.CompletableViewModel;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class AddCaldavAccountViewModel extends CompletableViewModel<String> {
|
||||
void addAccount(CaldavClient client, String url, String username, String password) {
|
||||
run(
|
||||
() -> client.setForeground().forUrl(url, username, password).getHomeSet());
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package org.tasks.caldav
|
||||
|
||||
import org.tasks.ui.CompletableViewModel
|
||||
|
||||
class AddCaldavAccountViewModel : CompletableViewModel<String>() {
|
||||
fun addAccount(client: CaldavClient, url: String?, username: String?, password: String?) {
|
||||
run { client.setForeground().forUrl(url, username, password).homeSet }
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package org.tasks.caldav;
|
||||
|
||||
import org.tasks.data.CaldavAccount;
|
||||
import org.tasks.ui.CompletableViewModel;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class CreateCalendarViewModel extends CompletableViewModel<String> {
|
||||
void createCalendar(CaldavClient client, CaldavAccount account, String name, int color) {
|
||||
run(() -> client.forAccount(account).makeCollection(name, color));
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package org.tasks.caldav
|
||||
|
||||
import org.tasks.data.CaldavAccount
|
||||
import org.tasks.ui.CompletableViewModel
|
||||
|
||||
class CreateCalendarViewModel : CompletableViewModel<String?>() {
|
||||
fun createCalendar(client: CaldavClient, account: CaldavAccount?, name: String?, color: Int) {
|
||||
run { client.forAccount(account).makeCollection(name, color) }
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package org.tasks.caldav;
|
||||
|
||||
import org.tasks.ui.CompletableViewModel;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class UpdateCaldavAccountViewModel extends CompletableViewModel<String> {
|
||||
void updateCaldavAccount(
|
||||
CaldavClient client, String url, String username, String password) {
|
||||
run(() -> client.forUrl(url, username, password).getHomeSet());
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package org.tasks.caldav
|
||||
|
||||
import org.tasks.ui.CompletableViewModel
|
||||
|
||||
class UpdateCaldavAccountViewModel : CompletableViewModel<String>() {
|
||||
fun updateCaldavAccount(
|
||||
client: CaldavClient, url: String?, username: String?, password: String?) {
|
||||
run {
|
||||
client.forUrl(url, username, password).homeSet
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package org.tasks.caldav;
|
||||
|
||||
import org.tasks.data.CaldavAccount;
|
||||
import org.tasks.data.CaldavCalendar;
|
||||
import org.tasks.ui.CompletableViewModel;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class UpdateCalendarViewModel extends CompletableViewModel<String> {
|
||||
void updateCalendar(
|
||||
CaldavClient client, CaldavAccount account, CaldavCalendar calendar, String name, int color) {
|
||||
run(() -> client.forCalendar(account, calendar).updateCollection(name, color));
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package org.tasks.caldav
|
||||
|
||||
import org.tasks.data.CaldavAccount
|
||||
import org.tasks.data.CaldavCalendar
|
||||
import org.tasks.ui.CompletableViewModel
|
||||
|
||||
class UpdateCalendarViewModel : CompletableViewModel<String?>() {
|
||||
fun updateCalendar(
|
||||
client: CaldavClient, account: CaldavAccount?, calendar: CaldavCalendar?, name: String?, color: Int) {
|
||||
run { client.forCalendar(account, calendar).updateCollection(name, color) }
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package org.tasks.etesync;
|
||||
|
||||
import androidx.core.util.Pair;
|
||||
import com.etesync.journalmanager.UserInfoManager.UserInfo;
|
||||
import org.tasks.ui.CompletableViewModel;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class AddEteSyncAccountViewModel extends CompletableViewModel<Pair<UserInfo, String>> {
|
||||
void addAccount(EteSyncClient client, String url, String username, String password) {
|
||||
run(
|
||||
() -> {
|
||||
client.setForeground();
|
||||
String token = client.forUrl(url, username, null, null).getToken(password);
|
||||
return Pair.create(client.forUrl(url, username, null, token).getUserInfo(), token);
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package org.tasks.etesync
|
||||
|
||||
import androidx.core.util.Pair
|
||||
import com.etesync.journalmanager.UserInfoManager
|
||||
import org.tasks.ui.CompletableViewModel
|
||||
|
||||
class AddEteSyncAccountViewModel : CompletableViewModel<Pair<UserInfoManager.UserInfo, String>>() {
|
||||
fun addAccount(client: EteSyncClient, url: String?, username: String?, password: String?) {
|
||||
run {
|
||||
client.setForeground()
|
||||
val token = client.forUrl(url, username, null, null).getToken(password)
|
||||
Pair.create(client.forUrl(url, username, null, token!!).userInfo, token)
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package org.tasks.etesync;
|
||||
|
||||
import org.tasks.data.CaldavAccount;
|
||||
import org.tasks.ui.CompletableViewModel;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class CreateCalendarViewModel extends CompletableViewModel<String> {
|
||||
void createCalendar(EteSyncClient client, CaldavAccount account, String name, int color) {
|
||||
run(() -> client.forAccount(account).makeCollection(name, color));
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package org.tasks.etesync
|
||||
|
||||
import org.tasks.data.CaldavAccount
|
||||
import org.tasks.ui.CompletableViewModel
|
||||
|
||||
class CreateCalendarViewModel : CompletableViewModel<String?>() {
|
||||
fun createCalendar(client: EteSyncClient, account: CaldavAccount?, name: String?, color: Int) {
|
||||
run { client.forAccount(account!!).makeCollection(name, color) }
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package org.tasks.etesync;
|
||||
|
||||
import org.tasks.data.CaldavAccount;
|
||||
import org.tasks.ui.CompletableViewModel;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class CreateUserInfoViewModel extends CompletableViewModel<String> {
|
||||
|
||||
void createUserInfo(EteSyncClient client, CaldavAccount caldavAccount, String derivedKey) {
|
||||
run(() -> {
|
||||
client.forAccount(caldavAccount).createUserInfo(derivedKey);
|
||||
return derivedKey;
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package org.tasks.etesync
|
||||
|
||||
import org.tasks.data.CaldavAccount
|
||||
import org.tasks.ui.CompletableViewModel
|
||||
|
||||
class CreateUserInfoViewModel : CompletableViewModel<String?>() {
|
||||
fun createUserInfo(client: EteSyncClient, caldavAccount: CaldavAccount?, derivedKey: String?) {
|
||||
run {
|
||||
client.forAccount(caldavAccount!!).createUserInfo(derivedKey)
|
||||
derivedKey
|
||||
}
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package org.tasks.etesync;
|
||||
|
||||
import org.tasks.data.CaldavAccount;
|
||||
import org.tasks.data.CaldavCalendar;
|
||||
import org.tasks.ui.CompletableViewModel;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class UpdateCalendarViewModel extends CompletableViewModel<String> {
|
||||
void updateCalendar(
|
||||
EteSyncClient client,
|
||||
CaldavAccount account,
|
||||
CaldavCalendar calendar,
|
||||
String name,
|
||||
int color) {
|
||||
run(() -> client.forAccount(account).updateCollection(calendar, name, color));
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package org.tasks.etesync
|
||||
|
||||
import org.tasks.data.CaldavAccount
|
||||
import org.tasks.data.CaldavCalendar
|
||||
import org.tasks.ui.CompletableViewModel
|
||||
|
||||
class UpdateCalendarViewModel : CompletableViewModel<String?>() {
|
||||
fun updateCalendar(
|
||||
client: EteSyncClient,
|
||||
account: CaldavAccount?,
|
||||
calendar: CaldavCalendar?,
|
||||
name: String?,
|
||||
color: Int) {
|
||||
run { client.forAccount(account!!).updateCollection(calendar!!, name, color) }
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package org.tasks.etesync;
|
||||
|
||||
import static org.tasks.Strings.isNullOrEmpty;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.util.Pair;
|
||||
import com.etesync.journalmanager.UserInfoManager.UserInfo;
|
||||
import org.tasks.ui.CompletableViewModel;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class UpdateEteSyncAccountViewModel extends CompletableViewModel<Pair<UserInfo, String>> {
|
||||
void updateAccount(
|
||||
EteSyncClient client,
|
||||
String url,
|
||||
String username,
|
||||
@Nullable String password,
|
||||
@Nullable String token) {
|
||||
run(
|
||||
() -> {
|
||||
client.setForeground();
|
||||
if (isNullOrEmpty(password)) {
|
||||
return Pair.create(client.forUrl(url, username, null, token).getUserInfo(), token);
|
||||
} else {
|
||||
String newToken = client.forUrl(url, username, null, null).getToken(password);
|
||||
return Pair.create(
|
||||
client.forUrl(url, username, null, newToken).getUserInfo(), newToken);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package org.tasks.etesync
|
||||
|
||||
import androidx.core.util.Pair
|
||||
import com.etesync.journalmanager.UserInfoManager
|
||||
import org.tasks.Strings.isNullOrEmpty
|
||||
import org.tasks.ui.CompletableViewModel
|
||||
|
||||
class UpdateEteSyncAccountViewModel : CompletableViewModel<Pair<UserInfoManager.UserInfo, String>>() {
|
||||
fun updateAccount(
|
||||
client: EteSyncClient, url: String?, user: String?, pass: String?, token: String?) {
|
||||
run {
|
||||
client.setForeground()
|
||||
if (isNullOrEmpty(pass)) {
|
||||
Pair.create(client.forUrl(url, user, null, token).userInfo, token)
|
||||
} else {
|
||||
val newToken = client.forUrl(url, user, null, null).getToken(pass)
|
||||
Pair.create(client.forUrl(url, user, null, newToken).userInfo, newToken)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
package org.tasks.ui;
|
||||
|
||||
import static com.todoroo.andlib.utility.AndroidUtilities.assertMainThread;
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public abstract class CompletableViewModel<T> extends ViewModel {
|
||||
private final MutableLiveData<T> data = new MutableLiveData<>();
|
||||
private final MutableLiveData<Throwable> error = new MutableLiveData<>();
|
||||
private final CompositeDisposable disposables = new CompositeDisposable();
|
||||
private boolean inProgress;
|
||||
|
||||
public void observe(
|
||||
LifecycleOwner lifecycleOwner, Observer<T> dataObserver, Observer<Throwable> errorObserver) {
|
||||
data.observe(lifecycleOwner, dataObserver);
|
||||
error.observe(lifecycleOwner, errorObserver);
|
||||
}
|
||||
|
||||
public boolean inProgress() {
|
||||
return inProgress;
|
||||
}
|
||||
|
||||
protected void run(Callable<T> callable) {
|
||||
assertMainThread();
|
||||
|
||||
if (!inProgress) {
|
||||
inProgress = true;
|
||||
disposables.add(
|
||||
Single.fromCallable(callable)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doFinally(
|
||||
() -> {
|
||||
assertMainThread();
|
||||
inProgress = false;
|
||||
})
|
||||
.subscribe(data::setValue, error::setValue));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
disposables.dispose();
|
||||
}
|
||||
|
||||
public void removeObserver(LifecycleOwner owner) {
|
||||
data.removeObservers(owner);
|
||||
error.removeObservers(owner);
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package org.tasks.ui
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.observe
|
||||
import com.todoroo.andlib.utility.AndroidUtilities
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.disposables.CompositeDisposable
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
|
||||
abstract class CompletableViewModel<T> : ViewModel() {
|
||||
private val data = MutableLiveData<T>()
|
||||
private val error = MutableLiveData<Throwable>()
|
||||
private val disposables = CompositeDisposable()
|
||||
private var inProgress = false
|
||||
|
||||
fun observe(
|
||||
lifecycleOwner: LifecycleOwner,
|
||||
dataObserver: (T) -> Unit,
|
||||
errorObserver: (Throwable) -> Unit) {
|
||||
data.observe(lifecycleOwner, dataObserver)
|
||||
error.observe(lifecycleOwner, errorObserver)
|
||||
}
|
||||
|
||||
fun inProgress(): Boolean {
|
||||
return inProgress
|
||||
}
|
||||
|
||||
protected fun run(callable: () -> T) {
|
||||
AndroidUtilities.assertMainThread()
|
||||
if (!inProgress) {
|
||||
inProgress = true
|
||||
disposables.add(
|
||||
Single.fromCallable(callable)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.doFinally {
|
||||
AndroidUtilities.assertMainThread()
|
||||
inProgress = false
|
||||
}
|
||||
.subscribe({ value: T -> data.setValue(value) }) { value: Throwable -> error.setValue(value) })
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
disposables.dispose()
|
||||
}
|
||||
|
||||
fun removeObserver(owner: LifecycleOwner) {
|
||||
data.removeObservers(owner)
|
||||
error.removeObservers(owner)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue