1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-23 16:53:04 +02:00

Run toast message on UI thread (#438)

* run toast message on UI thread, see issue #430
* revert PR #421 due some index out of bounce exception
This commit is contained in:
OliE
2019-03-17 08:48:19 +01:00
committed by GitHub
parent 2edc6bb707
commit 61b7f0b7b9
2 changed files with 22 additions and 10 deletions

View File

@@ -25,6 +25,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabaseCorruptException; import android.database.sqlite.SQLiteDatabaseCorruptException;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.text.format.DateFormat; import android.text.format.DateFormat;
@@ -211,7 +212,7 @@ public class OpenScale {
} }
} catch (Exception e) { } catch (Exception e) {
Timber.e(e); Timber.e(e);
Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show(); runUiToastMsg("Error: " + e.getMessage());
} }
return new ScaleUser(); return new ScaleUser();
@@ -328,15 +329,16 @@ public class OpenScale {
Converters.fromKilogram(scaleMeasurement.getWeight(), unit), unit.toString(), Converters.fromKilogram(scaleMeasurement.getWeight(), unit), unit.toString(),
dateFormat.format(dateTime) + " " + timeFormat.format(dateTime), dateFormat.format(dateTime) + " " + timeFormat.format(dateTime),
scaleUser.getUserName()); scaleUser.getUserName());
Toast.makeText(context, infoText, Toast.LENGTH_LONG).show(); runUiToastMsg(infoText);
} }
alarmHandler.entryChanged(context, scaleMeasurement); alarmHandler.entryChanged(context, scaleMeasurement);
updateScaleData(); updateScaleData();
triggerWidgetUpdate(); triggerWidgetUpdate();
} else { } else {
Timber.d("to be added measurement is thrown away because measurement with the same date and time already exist"); Timber.d("to be added measurement is thrown away because measurement with the same date and time already exist");
if (!silent) { if (!silent) {
Toast.makeText(context, context.getString(R.string.info_new_data_duplicated), Toast.LENGTH_LONG).show(); runUiToastMsg(context.getString(R.string.info_new_data_duplicated));
} }
} }
@@ -496,9 +498,9 @@ public class OpenScale {
measurementDAO.insertAll(csvScaleMeasurementList); measurementDAO.insertAll(csvScaleMeasurementList);
updateScaleData(); updateScaleData();
Toast.makeText(context, context.getString(R.string.info_data_imported) + " " + filename, Toast.LENGTH_SHORT).show(); runUiToastMsg(context.getString(R.string.info_data_imported) + " " + filename);
} catch (IOException | ParseException e) { } catch (IOException | ParseException e) {
Toast.makeText(context, context.getString(R.string.error_importing) + ": " + e.getMessage(), Toast.LENGTH_SHORT).show(); runUiToastMsg(context.getString(R.string.error_importing) + ": " + e.getMessage());
} }
} }
@@ -508,7 +510,7 @@ public class OpenScale {
CsvHelper.exportTo(new OutputStreamWriter(output), scaleMeasurementList); CsvHelper.exportTo(new OutputStreamWriter(output), scaleMeasurementList);
return true; return true;
} catch (IOException e) { } catch (IOException e) {
Toast.makeText(context, context.getResources().getString(R.string.error_exporting) + " " + e.getMessage(), Toast.LENGTH_SHORT).show(); runUiToastMsg(context.getResources().getString(R.string.error_exporting) + " " + e.getMessage());
} }
return false; return false;
@@ -654,4 +656,14 @@ public class OpenScale {
public Cursor getScaleMeasurementListCursor(long userId) { public Cursor getScaleMeasurementListCursor(long userId) {
return measurementDAO.selectAll(userId); return measurementDAO.selectAll(userId);
} }
private void runUiToastMsg(String text) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
});
}
} }

View File

@@ -352,10 +352,10 @@ public class ChartMeasurementView extends LineChart {
} }
private ScaleMeasurement[] averageScaleMeasurementList(List<ScaleMeasurement> measurementList) { private ScaleMeasurement[] averageScaleMeasurementList(List<ScaleMeasurement> measurementList) {
final ScaleMeasurement[] avgMeasurementList = new ScaleMeasurement[ maxXValue - minXValue + 1]; final ScaleMeasurement[] avgMeasurementList = new ScaleMeasurement[ maxXValue + minXValue + 1];
for (ScaleMeasurement measurement : measurementList) { for (ScaleMeasurement measurement : measurementList) {
int binNr = getBinNr(measurement) - minXValue; int binNr = getBinNr(measurement);
if (avgMeasurementList[binNr] == null) { if (avgMeasurementList[binNr] == null) {
avgMeasurementList[binNr] = measurement.clone(); avgMeasurementList[binNr] = measurement.clone();
@@ -369,7 +369,7 @@ public class ChartMeasurementView extends LineChart {
continue; continue;
} }
int binNr = getBinNr(avgMeasurement) - minXValue; int binNr = getBinNr(avgMeasurement);
avgMeasurement.divide(avgMeasurementList[binNr].count()); avgMeasurement.divide(avgMeasurementList[binNr].count());
} }
@@ -377,7 +377,7 @@ public class ChartMeasurementView extends LineChart {
} }
private ScaleMeasurement getPreviousMeasurment(ScaleMeasurement[] masurementList, int binNr) { private ScaleMeasurement getPreviousMeasurment(ScaleMeasurement[] masurementList, int binNr) {
for (int i=binNr-minXValue-1; i >= 0; i--) { for (int i=binNr-1; i >= 0; i--) {
if (masurementList[i] != null) { if (masurementList[i] != null) {
return masurementList[i]; return masurementList[i];
} }