Convert Task and TaskRunner

This commit is contained in:
Quentin Hibon
2021-01-21 22:47:40 +01:00
parent 8131d37d8e
commit 7d361b2203
6 changed files with 36 additions and 51 deletions

View File

@@ -40,8 +40,8 @@ class TaskProgressBar(
isIndeterminate = true isIndeterminate = true
} }
override fun onTaskStarted(task: Task?) = update() override fun onTaskStarted(task: Task) = update()
override fun onTaskFinished(task: Task?) = update() override fun onTaskFinished(task: Task) = update()
override fun onAttachedToWindow() { override fun onAttachedToWindow() {
super.onAttachedToWindow() super.onAttachedToWindow()

View File

@@ -23,6 +23,8 @@ import android.os.*;
import org.isoron.uhabits.core.*; import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.tasks.*; import org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.tasks.Task;
import org.jetbrains.annotations.NotNull;
import java.util.*; import java.util.*;
@@ -53,18 +55,19 @@ public class AndroidTaskRunner implements TaskRunner
} }
@Override @Override
public void addListener(Listener listener) public void addListener(@NotNull Listener listener)
{ {
listeners.add(listener); listeners.add(listener);
} }
@Override @Override
public void execute(Task task) public void execute(@NotNull Task task)
{ {
task.onAttached(this); task.onAttached(this);
new CustomAsyncTask(task).execute(); new CustomAsyncTask(task).execute();
} }
@Override @Override
public int getActiveTaskCount() public int getActiveTaskCount()
{ {
@@ -72,7 +75,7 @@ public class AndroidTaskRunner implements TaskRunner
} }
@Override @Override
public void publishProgress(Task task, int progress) public void publishProgress(@NotNull Task task, int progress)
{ {
CustomAsyncTask asyncTask = taskToAsyncTask.get(task); CustomAsyncTask asyncTask = taskToAsyncTask.get(task);
if (asyncTask == null) return; if (asyncTask == null) return;
@@ -80,11 +83,12 @@ public class AndroidTaskRunner implements TaskRunner
} }
@Override @Override
public void removeListener(Listener listener) public void removeListener(@NotNull Listener listener)
{ {
listeners.remove(listener); listeners.remove(listener);
} }
private class CustomAsyncTask extends AsyncTask<Void, Integer, Void> private class CustomAsyncTask extends AsyncTask<Void, Integer, Void>
{ {
private final Task task; private final Task task;

View File

@@ -21,6 +21,9 @@ package org.isoron.uhabits.core.tasks
import java.util.LinkedList import java.util.LinkedList
class SingleThreadTaskRunner : TaskRunner { class SingleThreadTaskRunner : TaskRunner {
override val activeTaskCount: Int
get() = 0
private val listeners: MutableList<TaskRunner.Listener> = LinkedList() private val listeners: MutableList<TaskRunner.Listener> = LinkedList()
override fun addListener(listener: TaskRunner.Listener) { override fun addListener(listener: TaskRunner.Listener) {
listeners.add(listener) listeners.add(listener)
@@ -37,10 +40,6 @@ class SingleThreadTaskRunner : TaskRunner {
for (l in listeners) l.onTaskFinished(task) for (l in listeners) l.onTaskFinished(task)
} }
override fun getActiveTaskCount(): Int {
return 0
}
override fun publishProgress(task: Task, progress: Int) { override fun publishProgress(task: Task, progress: Int) {
task.onProgressUpdate(progress) task.onProgressUpdate(progress)
} }

View File

@@ -16,27 +16,16 @@
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits.core.tasks
package org.isoron.uhabits.core.tasks; fun interface Task {
fun cancel() {}
val isCanceled: Boolean
get() = false
import androidx.annotation.*; fun doInBackground()
fun onAttached(runner: TaskRunner) {}
public interface Task fun onPostExecute() {}
{ fun onPreExecute() {}
default void cancel() {} fun onProgressUpdate(value: Int) {}
default boolean isCanceled()
{
return false;
}
void doInBackground();
default void onAttached(@NonNull TaskRunner runner) {}
default void onPostExecute() {}
default void onPreExecute() {}
default void onProgressUpdate(int value) {}
} }

View File

@@ -16,25 +16,17 @@
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.isoron.uhabits.core.tasks
package org.isoron.uhabits.core.tasks; interface TaskRunner {
fun addListener(listener: Listener)
fun removeListener(listener: Listener)
fun execute(task: Task)
fun publishProgress(task: Task, progress: Int)
val activeTaskCount: Int
public interface TaskRunner interface Listener {
{ fun onTaskStarted(task: Task)
void addListener(Listener listener); fun onTaskFinished(task: Task)
void removeListener(Listener listener);
void execute(Task task);
void publishProgress(Task task, int progress);
int getActiveTaskCount();
interface Listener
{
void onTaskStarted(Task task);
void onTaskFinished(Task task);
} }
} }

View File

@@ -136,8 +136,9 @@ class HabitCardListCache @Inject constructor(
@Synchronized @Synchronized
fun refreshAllHabits() { fun refreshAllHabits() {
if (currentFetchTask != null) currentFetchTask!!.cancel() if (currentFetchTask != null) currentFetchTask!!.cancel()
currentFetchTask = RefreshTask() val task = RefreshTask()
taskRunner.execute(currentFetchTask) currentFetchTask = task
taskRunner.execute(task)
} }
@Synchronized @Synchronized