mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-22 05:38:55 +00:00
debounce task creation
this should prevent the issue that some users had, where tasks were created multiple times
This commit is contained in:
parent
844b909d74
commit
fe24613568
5 changed files with 41 additions and 5 deletions
|
|
@ -14,6 +14,6 @@
|
|||
<item android:id="@+id/action_search"
|
||||
android:icon="@drawable/ic_action_filter_list"
|
||||
android:title="@string/filter"
|
||||
app:showAsAction="always" />
|
||||
app:showAsAction="ifRoom" />
|
||||
|
||||
</menu>
|
||||
|
|
|
|||
|
|
@ -17,4 +17,8 @@ public interface TaskRepository extends BaseRepository {
|
|||
|
||||
Observable<TaskDirectionData> taskChecked(Task task, boolean up);
|
||||
Observable<Task> scoreChecklistItem(String taskId, String itemId);
|
||||
|
||||
Observable<Task> createTask(Task task);
|
||||
|
||||
Observable<Task> updateTask(String id, Task task);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,12 +10,15 @@ import com.magicmicky.habitrpgwrapper.lib.models.tasks.TaskList;
|
|||
import com.magicmicky.habitrpgwrapper.lib.models.tasks.TasksOrder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
|
||||
public class TaskRepositoryImpl extends BaseRepositoryImpl<TaskLocalRepository> implements TaskRepository {
|
||||
|
||||
private long lastTaskAction = 0;
|
||||
|
||||
public TaskRepositoryImpl(TaskLocalRepository localRepository, ApiClient apiClient) {
|
||||
super(localRepository, apiClient);
|
||||
}
|
||||
|
|
@ -33,10 +36,15 @@ public class TaskRepositoryImpl extends BaseRepositoryImpl<TaskLocalRepository>
|
|||
|
||||
@Override
|
||||
public Observable<TaskDirectionData> taskChecked(Task task, boolean up) {
|
||||
long now = new Date().getTime();
|
||||
if (lastTaskAction > now-500) {
|
||||
return Observable.empty();
|
||||
}
|
||||
lastTaskAction = now;
|
||||
return this.apiClient.postTaskDirection(task.getId(), (up ? TaskDirection.up : TaskDirection.down).toString())
|
||||
.doOnNext(res -> {
|
||||
// save local task changes
|
||||
if (task != null && task.type != null && !task.type.equals("reward")) {
|
||||
if (task.type != null && !task.type.equals("reward")) {
|
||||
task.value = task.value + res.getDelta();
|
||||
|
||||
this.localRepository.saveTask(task);
|
||||
|
|
@ -47,4 +55,22 @@ public class TaskRepositoryImpl extends BaseRepositoryImpl<TaskLocalRepository>
|
|||
public Observable<Task> scoreChecklistItem(String taskId, String itemId){
|
||||
return apiClient.scoreChecklistItem(taskId, itemId).doOnNext(this.localRepository::saveTask);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<Task> createTask(Task task) {
|
||||
long now = new Date().getTime();
|
||||
if (lastTaskAction > now-500) {
|
||||
return Observable.empty();
|
||||
}
|
||||
return apiClient.createItem(task);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Observable<Task> updateTask(String id, Task task) {
|
||||
long now = new Date().getTime();
|
||||
if (lastTaskAction > now-500) {
|
||||
return Observable.empty();
|
||||
}
|
||||
return apiClient.updateTask(id, task);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@ import com.magicmicky.habitrpgwrapper.lib.api.ApiClient;
|
|||
|
||||
import android.content.Context;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
|
|
@ -29,6 +31,7 @@ public class RepositoryModule {
|
|||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
TaskRepository providesTaskRepository(TaskLocalRepository localRepository, ApiClient apiClient) {
|
||||
return new TaskRepositoryImpl(localRepository, apiClient);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,7 @@ import com.habitrpg.android.habitica.callbacks.TaskScoringCallback;
|
|||
import com.habitrpg.android.habitica.callbacks.TaskUpdateCallback;
|
||||
import com.habitrpg.android.habitica.callbacks.UnlockCallback;
|
||||
import com.habitrpg.android.habitica.components.AppComponent;
|
||||
import com.habitrpg.android.habitica.data.TaskRepository;
|
||||
import com.habitrpg.android.habitica.databinding.ValueBarBinding;
|
||||
import com.habitrpg.android.habitica.events.ContentReloadedEvent;
|
||||
import com.habitrpg.android.habitica.events.DisplayFragmentEvent;
|
||||
|
|
@ -228,6 +229,9 @@ public class MainActivity extends BaseActivity implements Action1<Throwable>, Ha
|
|||
@Inject
|
||||
NotifyUserUseCase notifyUserUseCase;
|
||||
|
||||
@Inject
|
||||
TaskRepository taskRepository;
|
||||
|
||||
// endregion
|
||||
|
||||
private Drawer drawer;
|
||||
|
|
@ -1451,11 +1455,10 @@ public class MainActivity extends BaseActivity implements Action1<Throwable>, Ha
|
|||
public void onEvent(final TaskSaveEvent event) {
|
||||
Task task = event.task;
|
||||
if (event.created) {
|
||||
this.apiClient.createItem(task)
|
||||
.subscribe(new TaskCreationCallback(), throwable -> {
|
||||
this.taskRepository.createTask(task).subscribe(new TaskCreationCallback(), throwable -> {
|
||||
});
|
||||
} else {
|
||||
this.apiClient.updateTask(task.getId(), task)
|
||||
this.taskRepository.updateTask(task.getId(), task)
|
||||
.subscribe(new TaskUpdateCallback(), throwable -> {
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue