mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Remove progress bar; switch to BaseTask
This commit is contained in:
@@ -115,7 +115,6 @@ public class ListHabitsFragment extends Fragment
|
|||||||
|
|
||||||
loader.setListener(this);
|
loader.setListener(this);
|
||||||
loader.setCheckmarkCount(helper.getButtonCount());
|
loader.setCheckmarkCount(helper.getButtonCount());
|
||||||
loader.setProgressBar(progressBar);
|
|
||||||
|
|
||||||
llHint.setOnClickListener(this);
|
llHint.setOnClickListener(this);
|
||||||
tvStarEmpty.setTypeface(helper.getFontawesome());
|
tvStarEmpty.setTypeface(helper.getFontawesome());
|
||||||
|
|||||||
@@ -19,13 +19,9 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.loaders;
|
package org.isoron.uhabits.loaders;
|
||||||
|
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.os.Handler;
|
|
||||||
import android.view.View;
|
|
||||||
import android.widget.ProgressBar;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.helpers.DateHelper;
|
import org.isoron.uhabits.helpers.DateHelper;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
import org.isoron.uhabits.tasks.BaseTask;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -37,9 +33,8 @@ public class HabitListLoader
|
|||||||
void onLoadFinished();
|
void onLoadFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
private AsyncTask<Void, Integer, Void> currentFetchTask;
|
private BaseTask currentFetchTask;
|
||||||
private int checkmarkCount;
|
private int checkmarkCount;
|
||||||
private ProgressBar progressBar;
|
|
||||||
|
|
||||||
private Listener listener;
|
private Listener listener;
|
||||||
private Long lastLoadTimestamp;
|
private Long lastLoadTimestamp;
|
||||||
@@ -56,11 +51,6 @@ public class HabitListLoader
|
|||||||
this.includeArchived = includeArchived;
|
this.includeArchived = includeArchived;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProgressBar(ProgressBar progressBar)
|
|
||||||
{
|
|
||||||
this.progressBar = progressBar;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCheckmarkCount(int checkmarkCount)
|
public void setCheckmarkCount(int checkmarkCount)
|
||||||
{
|
{
|
||||||
this.checkmarkCount = checkmarkCount;
|
this.checkmarkCount = checkmarkCount;
|
||||||
@@ -98,7 +88,7 @@ public class HabitListLoader
|
|||||||
{
|
{
|
||||||
if (currentFetchTask != null) currentFetchTask.cancel(true);
|
if (currentFetchTask != null) currentFetchTask.cancel(true);
|
||||||
|
|
||||||
currentFetchTask = new AsyncTask<Void, Integer, Void>()
|
currentFetchTask = new BaseTask()
|
||||||
{
|
{
|
||||||
public HashMap<Long, Habit> newHabits;
|
public HashMap<Long, Habit> newHabits;
|
||||||
public HashMap<Long, int[]> newCheckmarks;
|
public HashMap<Long, int[]> newCheckmarks;
|
||||||
@@ -106,7 +96,7 @@ public class HabitListLoader
|
|||||||
public List<Habit> newHabitList;
|
public List<Habit> newHabitList;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params)
|
protected void doInBackground()
|
||||||
{
|
{
|
||||||
newHabits = new HashMap<>();
|
newHabits = new HashMap<>();
|
||||||
newCheckmarks = new HashMap<>();
|
newCheckmarks = new HashMap<>();
|
||||||
@@ -136,12 +126,12 @@ public class HabitListLoader
|
|||||||
|
|
||||||
commit();
|
commit();
|
||||||
|
|
||||||
if(!updateScoresAndCheckmarks) return null;
|
if(!updateScoresAndCheckmarks) return;
|
||||||
|
|
||||||
int current = 0;
|
int current = 0;
|
||||||
for (Habit h : newHabitList)
|
for (Habit h : newHabitList)
|
||||||
{
|
{
|
||||||
if (isCancelled()) return null;
|
if (isCancelled()) return;
|
||||||
|
|
||||||
Long id = h.getId();
|
Long id = h.getId();
|
||||||
newScores.put(id, h.scores.getTodayValue());
|
newScores.put(id, h.scores.getTodayValue());
|
||||||
@@ -149,8 +139,6 @@ public class HabitListLoader
|
|||||||
|
|
||||||
publishProgress(current++, newHabits.size());
|
publishProgress(current++, newHabits.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void commit()
|
private void commit()
|
||||||
@@ -161,26 +149,9 @@ public class HabitListLoader
|
|||||||
habitsList = newHabitList;
|
habitsList = newHabitList;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPreExecute()
|
|
||||||
{
|
|
||||||
if(progressBar != null)
|
|
||||||
{
|
|
||||||
progressBar.setIndeterminate(false);
|
|
||||||
progressBar.setProgress(0);
|
|
||||||
progressBar.setVisibility(View.VISIBLE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onProgressUpdate(Integer... values)
|
protected void onProgressUpdate(Integer... values)
|
||||||
{
|
{
|
||||||
if(progressBar != null)
|
|
||||||
{
|
|
||||||
progressBar.setMax(values[1]);
|
|
||||||
progressBar.setProgress(values[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(listener != null) listener.onLoadFinished();
|
if(listener != null) listener.onLoadFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +160,6 @@ public class HabitListLoader
|
|||||||
{
|
{
|
||||||
if (isCancelled()) return;
|
if (isCancelled()) return;
|
||||||
|
|
||||||
if(progressBar != null) progressBar.setVisibility(View.INVISIBLE);
|
|
||||||
lastLoadTimestamp = DateHelper.getStartOfToday();
|
lastLoadTimestamp = DateHelper.getStartOfToday();
|
||||||
currentFetchTask = null;
|
currentFetchTask = null;
|
||||||
|
|
||||||
@@ -203,50 +173,27 @@ public class HabitListLoader
|
|||||||
|
|
||||||
public void updateHabit(final Long id)
|
public void updateHabit(final Long id)
|
||||||
{
|
{
|
||||||
new AsyncTask<Void, Void, Void>()
|
new BaseTask()
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params)
|
protected void doInBackground()
|
||||||
{
|
{
|
||||||
long dateTo = DateHelper.getStartOfDay(DateHelper.getLocalTime());
|
long dateTo = DateHelper.getStartOfDay(DateHelper.getLocalTime());
|
||||||
long dateFrom = dateTo - (checkmarkCount - 1) * DateHelper.millisecondsInOneDay;
|
long dateFrom = dateTo - (checkmarkCount - 1) * DateHelper.millisecondsInOneDay;
|
||||||
|
|
||||||
Habit h = Habit.get(id);
|
Habit h = Habit.get(id);
|
||||||
|
if(h == null) return;
|
||||||
|
|
||||||
habits.put(id, h);
|
habits.put(id, h);
|
||||||
scores.put(id, h.scores.getTodayValue());
|
scores.put(id, h.scores.getTodayValue());
|
||||||
checkmarks.put(id, h.checkmarks.getValues(dateFrom, dateTo));
|
checkmarks.put(id, h.checkmarks.getValues(dateFrom, dateTo));
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPreExecute()
|
|
||||||
{
|
|
||||||
new Handler().postDelayed(new Runnable()
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
if (getStatus() == Status.RUNNING)
|
|
||||||
{
|
|
||||||
if(progressBar != null)
|
|
||||||
{
|
|
||||||
progressBar.setIndeterminate(true);
|
|
||||||
progressBar.setVisibility(View.VISIBLE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Void aVoid)
|
protected void onPostExecute(Void aVoid)
|
||||||
{
|
{
|
||||||
if(progressBar != null) progressBar.setVisibility(View.GONE);
|
|
||||||
|
|
||||||
if(listener != null)
|
if(listener != null)
|
||||||
listener.onLoadFinished();
|
listener.onLoadFinished();
|
||||||
|
|
||||||
}
|
}
|
||||||
}.execute();
|
}.execute();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user