Convert Task and TaskRunner

pull/716/head
Quentin Hibon 5 years ago
parent 8131d37d8e
commit 7d361b2203

@ -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()

@ -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;

@ -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)
} }

@ -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 {
import androidx.annotation.*; fun cancel() {}
val isCanceled: Boolean
public interface Task get() = false
{
default void cancel() {} fun doInBackground()
fun onAttached(runner: TaskRunner) {}
default boolean isCanceled() fun onPostExecute() {}
{ fun onPreExecute() {}
return false; fun onProgressUpdate(value: Int) {}
}
void doInBackground();
default void onAttached(@NonNull TaskRunner runner) {}
default void onPostExecute() {}
default void onPreExecute() {}
default void onProgressUpdate(int value) {}
} }

@ -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 {
public interface TaskRunner fun addListener(listener: Listener)
{ fun removeListener(listener: Listener)
void addListener(Listener listener); fun execute(task: Task)
fun publishProgress(task: Task, progress: Int)
void removeListener(Listener listener); val activeTaskCount: Int
void execute(Task task); interface Listener {
fun onTaskStarted(task: Task)
void publishProgress(Task task, int progress); fun onTaskFinished(task: Task)
int getActiveTaskCount();
interface Listener
{
void onTaskStarted(Task task);
void onTaskFinished(Task task);
} }
} }

@ -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

Loading…
Cancel
Save