Skip to content

Commit

Permalink
v19 (#174)
Browse files Browse the repository at this point in the history
* Replace AsyncTask (#172)

* Replace AsyncTasks in FoodHistoryActivity and TweakHistoryActivity

* Add AsyncTask replacement following https://medium.com/swlh/asynctask-is-deprecated-now-what-f30c31362761

* Refactor BackupTask

* Refactor RestoreTask

* Add result types for BackupTask and RestoreTask

* Refactor GenerateDataTask and CalculateStreaksTask

* Remove unnecessary `throws Exception`

* Rename boolean result variable to `success`

* Refactor CalculateStreakTask

* Refactor CalculateTweakStreakTask

* Refactor LoadServingsHistoryTask

* Refactor LoadTweakServingsHistoryTask

* Refactor LoadWeightsHistoryTask

* Remove TaskWithContext

* Change GenerateDataTask to return true instead of null

* Update compileSdkVersion and targetSdkVersion to 30

* Update libraries

* Replace jcenter with mavenCentral

* Upgrade gradle

* Delete commented out function

* Update README with past contributors

* Add more contributors

* Update `versionCode` and `versionName`

* Remove broken link to turnip videos
  • Loading branch information
slavick authored May 20, 2021
1 parent 123a187 commit 3f54adf
Show file tree
Hide file tree
Showing 27 changed files with 672 additions and 696 deletions.
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,36 @@ Contributors

* [John Slavick][slavick] (Maintainer)
* [Christi Richards][christirichards]
* [Armfoot][Armfoot]
* [Lauren Hacker][laurenhacker]

Special thanks to the volunteer efforts of the creators of the previous app: Chan Kruse, Allan Portera, and Sangeeta Kumar
* [MovGP0][MovGP0]
* [alibitek][alibitek]
* [angelagvarela][angelagvarela]
* [caugner][caugner]
* [jordansieve][jordansieve]
* [josephkelemen97][josephkelemen97]
* [lucab85][lucab85]
* [marc-medley][marc-medley]
* [myang310][myang310]
* [notmypassword][notmypassword]
* [qyearsley][qyearsley]

[nutritionfacts.org]: http://nutritionfacts.org "NutritionFacts.org - The Latest in Nutrition Research"
[contribute]: https://github.com/nutritionfactsorg/daily-dozen-android/blob/master/CONTRIBUTING.md "Contribute to the Daily Dozen Android App"
[donate]: https://nutritionfacts.org/donate "Donate to NutritionFacts.org"
[book]: http://nutritionfacts.org/book "How Not to Die"
[slavick]: https://github.com/slavick "John Slavick on GitHub"
[christirichards]: https://github.com/christirichards "Christi Richards on GitHub"
[laurenhacker]: https://github.com/lahacker "Lauren Hacker on GitHub"
[laurenhacker]: https://github.com/lahacker "Lauren Hacker on GitHub"
[Armfoot]: https://github.com/Armfoot "Armfoot on GitHub"
[MovGP0]: https://github.com/MovGP0 "MovGP0 on GitHub"
[notmypassword]: https://github.com/notmypassword "notmypassword on GitHub"
[alibitek]: https://github.com/alibitek "alibitek on GitHub"
[caugner]: https://github.com/caugner "caugner on GitHub"
[myang310]: https://github.com/myang310 "myang310 on GitHub"
[lucab85]: https://github.com/lucab85 "lucab85 on GitHub"
[qyearsley]: https://github.com/qyearsley "qyearsley on GitHub"
[jordansieve]: https://github.com/jordansieve "jordansieve on GitHub"
[angelagvarela]: https://github.com/angelagvarela "angelagvarela on GitHub"
[josephkelemen97]: https://github.com/josephkelemen97 "josephkelemen97 on GitHub"
[marc-medley]: https://github.com/marc-medley "marc-medley on GitHub"
12 changes: 6 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ configurations {
}

android {
compileSdkVersion 29
compileSdkVersion 30
buildToolsVersion '29.0.3'

viewBinding.enabled = true
Expand All @@ -37,10 +37,10 @@ android {
defaultConfig {
applicationId "org.nutritionfacts.dailydozen"
minSdkVersion 16
targetSdkVersion 29
targetSdkVersion 30
vectorDrawables.useSupportLibrary = true
versionCode 56
versionName "18"
versionCode 57
versionName "19"
}
buildTypes {
debug {
Expand All @@ -66,8 +66,8 @@ android {
}

dependencies {
implementation 'com.google.firebase:firebase-analytics:18.0.2'
implementation 'com.google.firebase:firebase-crashlytics:17.4.1'
implementation 'com.google.firebase:firebase-analytics:19.0.0'
implementation 'com.google.firebase:firebase-crashlytics:18.0.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.3.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.nutritionfacts.dailydozen.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
Expand All @@ -12,12 +13,18 @@
import org.nutritionfacts.dailydozen.R;
import org.nutritionfacts.dailydozen.databinding.ActivityDebugBinding;
import org.nutritionfacts.dailydozen.task.GenerateDataTask;
import org.nutritionfacts.dailydozen.task.ProgressListener;
import org.nutritionfacts.dailydozen.task.TaskRunner;
import org.nutritionfacts.dailydozen.task.params.GenerateDataTaskParams;
import org.nutritionfacts.dailydozen.util.NotificationUtil;

public class DebugActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
import timber.log.Timber;

public class DebugActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, ProgressListener {
private ActivityDebugBinding binding;

private ProgressDialog progressDialog;

private int historyToGenerate;

@Override
Expand Down Expand Up @@ -72,7 +79,7 @@ private void generateData(final boolean generateRandomData) {
.setPositiveButton(R.string.yes, (dialog, which) -> {
final GenerateDataTaskParams taskParams = new GenerateDataTaskParams(historyToGenerate, generateRandomData);

new GenerateDataTask(DebugActivity.this).execute(taskParams);
new TaskRunner().executeAsync(new GenerateDataTask(this, taskParams));

dialog.dismiss();
})
Expand Down Expand Up @@ -113,4 +120,32 @@ public void onNothingSelected(AdapterView<?> parent) {
public void onShowNotificationClicked() {
binding.debugShowNotification.setOnClickListener(v -> NotificationUtil.showUpdateReminderNotification(DebugActivity.this, null));
}

@Override
public void showProgressBar(int titleId) {
progressDialog = new ProgressDialog(this);
progressDialog.setIndeterminate(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle(titleId);
progressDialog.show();
}

@Override
public void updateProgressBar(int current, int total) {
progressDialog.setProgress(current);
progressDialog.setMax(total);
}

@Override
public void hideProgressBar() {
try {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
} catch (Exception e) {
Timber.e("hideProgressBar: Exception while trying to dismiss progress dialog");
} finally {
progressDialog = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.nutritionfacts.dailydozen.activity;

import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;

import androidx.core.content.ContextCompat;
Expand All @@ -25,13 +26,15 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import hirondelle.date4j.DateTime;

public class FoodHistoryActivity extends InfoActivity {
private ActivityHistoryBinding binding;

private Set<String> loadedMonths = new HashSet<>();
private final Set<String> loadedMonths = new HashSet<>();
private List<DateTime> fullServingsDates;
private List<DateTime> partialServingsDates;

Expand Down Expand Up @@ -76,58 +79,50 @@ private void initCalendar(final long foodId, final int recommendedServings) {
}

private void displayEntriesForVisibleMonths(final Calendar cal, final long foodId) {
new AsyncTask<Void, Void, Void>() {
ColorDrawable bgLessThanRecServings;
ColorDrawable bgRecServings;

@Override
protected Void doInBackground(Void... params) {
bgLessThanRecServings = new ColorDrawable(
ContextCompat.getColor(FoodHistoryActivity.this, R.color.legend_less_than_recommended_servings));

bgRecServings = new ColorDrawable(
ContextCompat.getColor(FoodHistoryActivity.this, R.color.legend_recommended_servings));

// We start 2 months in the past because this prevents "flickering" of dates when the user swipes to
// the previous month. For instance, starting in February and swiping to January, the dates from
// December that are shown in the January calendar will have their backgrounds noticeably flicker on.
DateUtil.subtractTwoMonths(cal);

int i = 0;
do {
final String monthStr = DateUtil.toStringYYYYMM(cal);

if (!loadedMonths.contains(monthStr)) {
final Map<Day, Boolean> servings = DDServings.getServingsOfFoodInYearAndMonth(foodId,
DateUtil.getYear(cal), DateUtil.getMonthOneBased(cal));

loadedMonths.add(monthStr);

for (Map.Entry<Day, Boolean> serving : servings.entrySet()) {
if (serving.getValue()) {
fullServingsDates.add(serving.getKey().getDateTime());
} else {
partialServingsDates.add(serving.getKey().getDateTime());
}
}
}
ExecutorService executor = Executors.newSingleThreadExecutor();
Handler handler = new Handler(Looper.getMainLooper());

executor.execute(() -> {
ColorDrawable bgLessThanRecServings = new ColorDrawable(
ContextCompat.getColor(FoodHistoryActivity.this, R.color.legend_less_than_recommended_servings));

ColorDrawable bgRecServings = new ColorDrawable(
ContextCompat.getColor(FoodHistoryActivity.this, R.color.legend_recommended_servings));

// We start 2 months in the past because this prevents "flickering" of dates when the user swipes to
// the previous month. For instance, starting in February and swiping to January, the dates from
// December that are shown in the January calendar will have their backgrounds noticeably flicker on.
DateUtil.subtractTwoMonths(cal);

DateUtil.addOneMonth(cal);
i++;
} while (i < 3);
int i = 0;
do {
final String monthStr = DateUtil.toStringYYYYMM(cal);

return null;
}
if (!loadedMonths.contains(monthStr)) {
final Map<Day, Boolean> servings = DDServings.getServingsOfFoodInYearAndMonth(foodId,
DateUtil.getYear(cal), DateUtil.getMonthOneBased(cal));

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
loadedMonths.add(monthStr);

for (Map.Entry<Day, Boolean> serving : servings.entrySet()) {
if (serving.getValue()) {
fullServingsDates.add(serving.getKey().getDateTime());
} else {
partialServingsDates.add(serving.getKey().getDateTime());
}
}
}

DateUtil.addOneMonth(cal);
i++;
} while (i < 3);

handler.post(() -> {
ArrayList<DayViewDecorator> decorators = new ArrayList<>();
decorators.add(new CalendarHistoryDecorator(fullServingsDates, bgRecServings));
decorators.add(new CalendarHistoryDecorator(partialServingsDates, bgLessThanRecServings));
binding.calendarView.addDecorators(decorators);
}
}.execute();
});
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.nutritionfacts.dailydozen.activity;

import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
Expand Down Expand Up @@ -35,7 +36,9 @@
import org.nutritionfacts.dailydozen.model.Day;
import org.nutritionfacts.dailydozen.task.BackupTask;
import org.nutritionfacts.dailydozen.task.CalculateStreaksTask;
import org.nutritionfacts.dailydozen.task.ProgressListener;
import org.nutritionfacts.dailydozen.task.RestoreTask;
import org.nutritionfacts.dailydozen.task.TaskRunner;
import org.nutritionfacts.dailydozen.util.DateUtil;
import org.nutritionfacts.dailydozen.util.NotificationUtil;

Expand All @@ -45,11 +48,13 @@
import hirondelle.date4j.DateTime;
import timber.log.Timber;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements ProgressListener {
private static final String ALREADY_HANDLED_RESTORE_INTENT = "already_handled_restore_intent";

private ActivityMainBinding binding;

private ProgressDialog progressDialog;

private MenuItem menuToggleModes;

private Handler dayChangeHandler;
Expand Down Expand Up @@ -98,7 +103,7 @@ private void calculateStreaksAfterDatabaseUpgradeToV2() {
.setCancelable(false)
.setTitle(R.string.dialog_streaks_title)
.setMessage(R.string.dialog_streaks_message)
.setPositiveButton(R.string.OK, (dialog, which) -> new CalculateStreaksTask(MainActivity.this).execute())
.setPositiveButton(R.string.OK, (dialog, which) -> new TaskRunner().executeAsync(new CalculateStreaksTask(this)))
.create().show();
}
}
Expand Down Expand Up @@ -286,7 +291,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
private void backup() {
if (!DDServings.isEmpty()) {
if (PermissionController.canWriteExternalStorage(this)) {
new BackupTask(this).execute(getBackupFile());
new TaskRunner().executeAsync(new BackupTask(this, getBackupFile()));
} else {
PermissionController.askForWriteExternalStorage(this);
}
Expand Down Expand Up @@ -324,7 +329,7 @@ private void checkIfOpenedForRestore(final Intent intent) {
}

private void restore(final Uri restoreFileUri) {
new RestoreTask(this).execute(restoreFileUri);
new TaskRunner().executeAsync(new RestoreTask(this, restoreFileUri, getContentResolver()));
}

public File getBackupFile() {
Expand Down Expand Up @@ -410,4 +415,32 @@ private void setDatePagerDate(final DateTime dateTime) {
binding.datePager.setCurrentItem(Day.getNumDaysSinceEpoch(dateTime));
}
}

@Override
public void showProgressBar(int titleId) {
progressDialog = new ProgressDialog(this);
progressDialog.setIndeterminate(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle(titleId);
progressDialog.show();
}

@Override
public void updateProgressBar(int current, int total) {
progressDialog.setProgress(current);
progressDialog.setMax(total);
}

@Override
public void hideProgressBar() {
try {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
} catch (Exception e) {
Timber.e("hideProgressBar: Exception while trying to dismiss progress dialog");
} finally {
progressDialog = null;
}
}
}
Loading

0 comments on commit 3f54adf

Please sign in to comment.