Skip to content

Commit

Permalink
data handling progress
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCZ2051 committed Nov 26, 2022
1 parent 6d91fea commit c171c87
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
42 changes: 31 additions & 11 deletions lib/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class _SettingsState extends State<Settings> {
Expanded(
flex: 10,
child: Text(
clothing.name!,
clothing.name,
style: const TextStyle(fontSize: 25),
),
),
Expand Down Expand Up @@ -495,18 +495,23 @@ class _SettingsState extends State<Settings> {
TextField(
//TODO: Add red border when invalid (need to check if parsing to Clothing object is possible)
onChanged: (value) {
try {
importData = jsonDecode(value);
} catch (e) {
var object = areDataValid(value);
if (object != null) {
importData = object;
} else {
importData = null;
}
setState(() {});
},
style: TextStyle(
style: const TextStyle(
fontFamily: 'IBM Plex Mono'),
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
border: OutlineInputBorder(),
errorText: importData == null
? 'Neplatná data'
: null,
),
),
],
Expand All @@ -516,21 +521,19 @@ class _SettingsState extends State<Settings> {
child: const Text("Zrušit"),
onPressed: () {
importMode = 0;
validData = false;
Navigator.pop(context);
},
),
ElevatedButton(
onPressed: () {
if (importMode == 0 &&
importData != null) {
vars.clothes = [];
for (Map clothing in importData!) {
vars.clothes.add(
Clothing.fromJson(clothing));
}
saveData();
setstate();
} else if (importMode == 1) {
setState(() {});
} else if (importMode == 1 &&
importData != null) {
for (Map clothing in importData!) {
vars.clothes.add(
Clothing.fromJson(clothing));
Expand All @@ -540,6 +543,7 @@ class _SettingsState extends State<Settings> {
}

importMode = 0;
validData = false;
Navigator.pop(context);
},
child: const Text("Importovat"),
Expand Down Expand Up @@ -594,6 +598,7 @@ class _SettingsState extends State<Settings> {
fontFamily: 'IBM Plex Mono',
),
),
//TODO: idk but importing data without time doesnt set it to null idk
],
),
),
Expand Down Expand Up @@ -677,3 +682,18 @@ saveData() {
prefs.setString("theme", vars.theme);
});
}

areDataValid(String data) {
print("checking: $data");
try {
List clothes = [];
for (Map clothing in jsonDecode(data)) {
clothes.add(Clothing.fromJson(clothing));
}
print('valid');
return clothes;
} catch (e) {
print('invalid');
return null;
}
}
4 changes: 2 additions & 2 deletions lib/vars.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class Clothing {
required this.count,
required this.lastChangedDateTime,
});
String? name;
int? count;
late String name;
late int count;
DateTime? lastChangedDateTime;

Map toJson() => {
Expand Down

0 comments on commit c171c87

Please sign in to comment.