Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to import from openScale backup #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions app/lib/core/measurement.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

import 'package:trale/core/traleNotifier.dart';
Expand Down Expand Up @@ -81,18 +82,39 @@ class Measurement {
=> '${date.toIso8601String()} ${weight.toStringAsFixed(10)}';

/// copy with applying change
static Measurement fromString({required String exportString}) {
final List<String> strings = exportString.split(' ');

if (strings.length != 2) {
print('error with parsing measurement!');
static Measurement fromString({required String exportString, bool native=true}) {
if(native){
// trale format
final List<String> strings = exportString.split(' ');

if(strings.length != 2){
print('error with parsing measurement (trale format)!');
}

return Measurement(
weight: double.parse(strings[1]),
date: DateTime.parse(strings[0]),
isMeasured: true,
);
} else {
// openscale format
final List<String> strings = exportString.split(',');

if(strings.length != 19){
print('error with parsing measurement (openscale format)!');
}

final String dateString = strings[8].substring(1, strings[8].length-2);
final DateFormat format = DateFormat('yyyy-MM-dd HH:mm');
final DateTime date = format.parse(dateString);


return Measurement(
weight: double.parse(strings[18]),
date: DateTime.parse(date.toIso8601String()),
isMeasured: true,
);
}

return Measurement(
weight: double.parse(strings[1]),
date: DateTime.parse(strings[0]),
isMeasured: true,
);
}

/// compare method to use default sort method on list
Expand Down
2 changes: 1 addition & 1 deletion app/lib/l10n/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"home": "Start",
"import": "Importiere Backup",
"importDialog": "Importiere alle Messungen in die aktuelle Messreihe. Dies kann nicht rückgängig gemacht werden!",
"importSubtitle": "Importiere Messungen aus einer Backup .txt Datei.",
"importSubtitle": "Importiere Messungen aus einer Backup .txt Datei oder einer openScale .csv Datei.",
"importingAbort": "Import abgebrochen.",
"interpolation": "Interpolation",
"intro1": "drücke",
Expand Down
4 changes: 2 additions & 2 deletions app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@
"@import": {
"description": "Import backup."
},
"importSubtitle": "Import measurements from txt backup file.",
"importSubtitle": "Import measurements from txt backup file or openScale csv file.",
"@importSubtitle": {
"description": "Import measurements from txt backup file."
"description": "Import measurements from txt backup file or openScale csv file."
},
"amoled": "AMOLED",
"@amoled": {
Expand Down
7 changes: 4 additions & 3 deletions app/lib/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class ImportListTile extends StatelessWidget {
if (accepted) {
final FilePickerResult? pickerResult = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: <String>['txt'],
allowedExtensions: <String>['txt','csv'],
);
if (
pickerResult != null &&
Expand All @@ -236,9 +236,10 @@ class ImportListTile extends StatelessWidget {
int measurementCounts = 0;
for (final String line in file.readAsLinesSync()) {
// parse comments
if (!line.startsWith('#')) {
if (!line.startsWith('#') && !line.startsWith('"')) {
final Measurement m = Measurement.fromString(
exportString: line
exportString: line,
native: pickerResult.files.single.extension == 'txt',
);
final bool wasInserted = db.insertMeasurement(m);
if (wasInserted) {
Expand Down