mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-09 18:48:51 -06:00
Refactor Tasks for better testability
This commit is contained in:
61
app/src/main/java/org/isoron/uhabits/tasks/BaseTask.java
Normal file
61
app/src/main/java/org/isoron/uhabits/tasks/BaseTask.java
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* This file is part of Loop Habit Tracker.
|
||||
*
|
||||
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* 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.tasks;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class BaseTask extends AsyncTask<Void, Void, Void>
|
||||
{
|
||||
private static CountDownLatch latch;
|
||||
private static int activeTaskCount;
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute()
|
||||
{
|
||||
activeTaskCount++;
|
||||
latch = new CountDownLatch(activeTaskCount);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void aVoid)
|
||||
{
|
||||
activeTaskCount--;
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
public static void waitForTasks(long timeout, TimeUnit unit)
|
||||
throws TimeoutException, InterruptedException
|
||||
{
|
||||
if(activeTaskCount == 0) return;
|
||||
|
||||
boolean successful = latch.await(timeout, unit);
|
||||
if(!successful) throw new TimeoutException();
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class ExportCSVTask extends AsyncTask<Void, Void, Void>
|
||||
public class ExportCSVTask extends BaseTask
|
||||
{
|
||||
public interface Listener
|
||||
{
|
||||
@@ -58,6 +58,8 @@ public class ExportCSVTask extends AsyncTask<Void, Void, Void>
|
||||
@Override
|
||||
protected void onPreExecute()
|
||||
{
|
||||
super.onPreExecute();
|
||||
|
||||
if(progressBar != null)
|
||||
{
|
||||
progressBar.setIndeterminate(true);
|
||||
@@ -73,6 +75,8 @@ public class ExportCSVTask extends AsyncTask<Void, Void, Void>
|
||||
|
||||
if(progressBar != null)
|
||||
progressBar.setVisibility(View.GONE);
|
||||
|
||||
super.onPostExecute(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ExportDBTask extends AsyncTask<Void, Void, Void>
|
||||
public class ExportDBTask extends BaseTask
|
||||
{
|
||||
public interface Listener
|
||||
{
|
||||
@@ -53,6 +53,8 @@ public class ExportDBTask extends AsyncTask<Void, Void, Void>
|
||||
@Override
|
||||
protected void onPreExecute()
|
||||
{
|
||||
super.onPreExecute();
|
||||
|
||||
if(progressBar != null)
|
||||
{
|
||||
progressBar.setIndeterminate(true);
|
||||
@@ -68,6 +70,8 @@ public class ExportDBTask extends AsyncTask<Void, Void, Void>
|
||||
|
||||
if(progressBar != null)
|
||||
progressBar.setVisibility(View.GONE);
|
||||
|
||||
super.onPostExecute(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -29,7 +29,7 @@ import org.isoron.uhabits.io.GenericImporter;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class ImportDataTask extends AsyncTask<Void, Void, Void>
|
||||
public class ImportDataTask extends BaseTask
|
||||
{
|
||||
public static final int SUCCESS = 1;
|
||||
public static final int NOT_RECOGNIZED = 2;
|
||||
@@ -65,6 +65,8 @@ public class ImportDataTask extends AsyncTask<Void, Void, Void>
|
||||
@Override
|
||||
protected void onPreExecute()
|
||||
{
|
||||
super.onPreExecute();
|
||||
|
||||
if(progressBar != null)
|
||||
{
|
||||
progressBar.setIndeterminate(true);
|
||||
@@ -79,6 +81,8 @@ public class ImportDataTask extends AsyncTask<Void, Void, Void>
|
||||
progressBar.setVisibility(View.GONE);
|
||||
|
||||
if(listener != null) listener.onImportFinished(result);
|
||||
|
||||
super.onPostExecute(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.util.AttributeSet;
|
||||
import android.view.GestureDetector;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewParent;
|
||||
import android.widget.Scroller;
|
||||
|
||||
public abstract class ScrollableDataView extends View implements GestureDetector.OnGestureListener,
|
||||
@@ -89,7 +90,10 @@ public abstract class ScrollableDataView extends View implements GestureDetector
|
||||
return false;
|
||||
|
||||
if(Math.abs(dx) > Math.abs(dy))
|
||||
getParent().requestDisallowInterceptTouchEvent(true);
|
||||
{
|
||||
ViewParent parent = getParent();
|
||||
if(parent != null) parent.requestDisallowInterceptTouchEvent(true);
|
||||
}
|
||||
|
||||
scroller.startScroll(scroller.getCurrX(), scroller.getCurrY(), (int) -dx, (int) dy, 0);
|
||||
scroller.computeScrollOffset();
|
||||
|
||||
Reference in New Issue
Block a user