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

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

View File

@@ -16,27 +16,16 @@
* You should have received a copy of the GNU General Public License along
* 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.*;
public interface Task
{
default void cancel() {}
default boolean isCanceled()
{
return false;
}
void doInBackground();
default void onAttached(@NonNull TaskRunner runner) {}
default void onPostExecute() {}
default void onPreExecute() {}
default void onProgressUpdate(int value) {}
fun doInBackground()
fun onAttached(runner: TaskRunner) {}
fun onPostExecute() {}
fun onPreExecute() {}
fun onProgressUpdate(value: Int) {}
}

View File

@@ -16,25 +16,17 @@
* You should have received a copy of the GNU General Public License along
* 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
{
void addListener(Listener listener);
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);
interface Listener {
fun onTaskStarted(task: Task)
fun onTaskFinished(task: Task)
}
}

View File

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