Refactor Tasks for better testability

This commit is contained in:
2016-03-30 08:28:06 -04:00
parent 67b88c5012
commit 2ba7246e5f
18 changed files with 217 additions and 54 deletions

View 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();
}
}

View File

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

View File

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

View File

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

View File

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