implement database import/export to json file

This commit is contained in:
Trent Palmer 2019-07-25 09:26:13 -07:00
parent ad5b0ee98e
commit a07e283841
6 changed files with 549 additions and 3 deletions

View File

@ -53,7 +53,7 @@ InkWell aboutApp(BuildContext context) {
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(
'Version: 0.1.1\n',
'Version: 0.1.2\n',
style: TextStyle(
color: candyApple,
),

View File

@ -1,5 +1,6 @@
// https://github.com/tekartik/sqflite/blob/master/sqflite/doc/migration_example.md
import 'dart:io';
import 'dart:convert';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
@ -253,4 +254,10 @@ class DatabaseHelper {
return await db
.delete(table, where: '$columnMapLocation = ?', whereArgs: [ml]);
}
Future<String> queryDBExport() async{
Database db = await instance.database;
List<Map> result = await db.rawQuery('SELECT $columnMapLocation,$columnNotes,$columnIsAutoTimeOffset,$columnTimeOffSet FROM $table');
return(jsonEncode(result));
}
}

View File

@ -391,3 +391,41 @@ Future<bool> setPreferenceUseWeather(bool useWeather) async {
bool committed = await prefs.setBool("useWeather", useWeather);
return (committed);
}
Future<String> getPreferenceDBExportFileName() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String dbExportFileName = prefs.getString("dbExportFileName") ?? "libre_gps_parser.json";
return (dbExportFileName.length > 0) ? dbExportFileName : "libre_gps_parser.json";
}
Future<bool> setPreferenceDBExportFileName(String dbExportFileName) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool committed = await prefs.setString("dbExportFileName", dbExportFileName);
return (committed);
}
Future<bool> importDataBase(String importString) {
final dbHelper = DatabaseHelper.instance;
final int _timeStamp = newTimeStamp();
List<dynamic> _importJson = jsonDecode(importString);
int _importJsonLength = _importJson.length;
for (int i = 0; i < _importJsonLength; i++) {
dbHelper.queryRowExists(_importJson[i]['mapLocation']).then((int rowExists) {
if (rowExists == 0) {
Map<String, dynamic> row = {
DatabaseHelper.columnMapLocation: _importJson[i]['mapLocation'],
};
if (_importJson[i]['notes'] != null) {
row['notes'] = _importJson[i]['notes'];
}
if (_importJson[i]['isAutoTimeOffset'] == 0) {
row['timeOffSet'] = _importJson[i]['timeOffSet'];
row['timeOffSetTime'] = _timeStamp;
row['isAutoTimeOffset'] = _importJson[i]['isAutoTimeOffset'];
}
dbHelper.insert(row);
}
});
}
return Future.value(true);
}

View File

@ -1,5 +1,10 @@
import 'package:permission_handler/permission_handler.dart';
import 'package:path_provider/path_provider.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'global_helper_functions.dart';
import 'database_helper.dart';
import 'dart:io';
class Settings extends StatefulWidget {
@override
@ -7,9 +12,12 @@ class Settings extends StatefulWidget {
}
class _SettingsState extends State<Settings> {
// String absoluteDBExportFileName = '';
String _dbExportFileNameString = '';
String elevationServer = "none";
String openWeatherMapKey = "none??";
String shortOWMAK = "none";
final _dbExportFileNameController = TextEditingController();
final _elevationServerController = TextEditingController();
final _oWMKController = TextEditingController();
bool _useElevation = false;
@ -24,6 +32,394 @@ class _SettingsState extends State<Settings> {
@override
Widget build(BuildContext context) {
Future<void> _showDBImportDialog() async{
Map<PermissionGroup, PermissionStatus> _storagePermissions;
PermissionStatus _storagePermission = await PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
if (_storagePermission == PermissionStatus.denied) {
_storagePermissions = await PermissionHandler().requestPermissions([PermissionGroup.storage]);
}
if ((_storagePermission == PermissionStatus.granted) || (_storagePermissions.toString() == PermissionStatus.granted.toString())) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: ivory,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
title: Text(
'You can import a\njson backup of\nyour database',
textAlign: TextAlign.center,
style: TextStyle(
color: candyApple,
),
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
margin: EdgeInsets.only(
top: 40,
bottom: 10,
),
child: ButtonTheme(
height: 55,
padding: EdgeInsets.only(
top: 5,
right: 12,
bottom: 12,
left: 10
),
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
color: peacockBlue,
child: Text(
'Select File',
style: TextStyle(
height: textHeight,
color: Colors.white,
fontSize: 24,
),
),
onPressed: () async{
String _filePath;
_filePath = await FilePicker.getFilePath(type: FileType.ANY);
Navigator.of(context).pop();
try {
String _importString = await File(_filePath).readAsString();
bool _imported = await importDataBase(_importString);
if (_imported) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: ivory,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
title: Text(
'Importing From\n\'$_filePath\'\n Succeeded!',
textAlign: TextAlign.center,
style: TextStyle(
color: candyApple,
),
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
margin: EdgeInsets.only(
top: 40,
bottom: 10,
),
child: ButtonTheme(
height: 55,
padding: EdgeInsets.only(
top: 5,
right: 12,
bottom: 12,
left: 10
),
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
color: peacockBlue,
child: Text(
'OK',
style: TextStyle(
height: textHeight,
color: Colors.white,
fontSize: 24,
),
),
onPressed: () async{
Navigator.of(context).pop();
}
),
),
),
],
),
);
}
);
}
} catch(e) {
print(e);
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: ivory,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
title: Text(
'Oops, something went wrong',
textAlign: TextAlign.center,
style: TextStyle(
color: candyApple,
),
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
margin: EdgeInsets.only(
top: 40,
bottom: 10,
),
child: ButtonTheme(
height: 55,
padding: EdgeInsets.only(
top: 5,
right: 12,
bottom: 12,
left: 10
),
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
color: peacockBlue,
child: Text(
'OK',
style: TextStyle(
height: textHeight,
color: Colors.white,
fontSize: 24,
),
),
onPressed: () async{
Navigator.of(context).pop();
}
),
),
),
],
),
);
}
);
}
}
),
),
),
],
),
);
}
);
}
}
Future<void> _showDBExportDialog() async{
Map<PermissionGroup, PermissionStatus> _storagePermissions;
PermissionStatus _storagePermission = await PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
if (_storagePermission == PermissionStatus.denied) {
_storagePermissions = await PermissionHandler().requestPermissions([PermissionGroup.storage]);
}
if ((_storagePermission == PermissionStatus.granted) || (_storagePermissions.toString() == PermissionStatus.granted.toString())) {
Directory sdcard = await getExternalStorageDirectory();
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: ivory,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
title: Text(
'Export File Name?',
textAlign: TextAlign.center,
style: TextStyle(
color: candyApple,
),
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
'DataBase will be\nexported to json file.',
style: TextStyle(
fontSize: 16,
),
),
TextField(
controller: _dbExportFileNameController,
keyboardType: TextInputType.multiline,
maxLines: null,
decoration: InputDecoration(
hintText: this._dbExportFileNameString,
),
onChanged: (text) {
this._dbExportFileNameString = (text.length > 0) ? text : "libre_gps_parser.json";
},
),
Container(
margin: EdgeInsets.only(
top: 40,
bottom: 10,
),
child: ButtonTheme(
height: 55,
padding: EdgeInsets.only(
top: 5,
right: 12,
bottom: 12,
left: 10
),
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
color: peacockBlue,
child: Text(
'Export',
style: TextStyle(
height: textHeight,
color: Colors.white,
fontSize: 24,
),
),
onPressed: () async{
_updatedbExportFileName();
final dbHelper = DatabaseHelper.instance;
String _dbExportString = await dbHelper.queryDBExport();
String _absoluteDBExportFileName = '${sdcard.path}/${this._dbExportFileNameString}';
File _file = File(_absoluteDBExportFileName);
File _result = await _file.writeAsString(_dbExportString);
Navigator.of(context).pop();
if (_result == null) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: ivory,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
title: Text(
'Writing To\n\'$_absoluteDBExportFileName\'\n(sdcard) Failed!',
textAlign: TextAlign.center,
style: TextStyle(
color: candyApple,
),
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
margin: EdgeInsets.only(
top: 40,
bottom: 10,
),
child: ButtonTheme(
height: 55,
padding: EdgeInsets.only(
top: 5,
right: 12,
bottom: 12,
left: 10
),
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
color: peacockBlue,
child: Text(
'OK',
style: TextStyle(
height: textHeight,
color: Colors.white,
fontSize: 24,
),
),
onPressed: () async{
Navigator.of(context).pop();
}
),
),
),
],
),
);
}
);
} else {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: ivory,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
title: Text(
'Writing To\n\'$_absoluteDBExportFileName\'\n(sdcard) Succeeded!',
textAlign: TextAlign.center,
style: TextStyle(
color: candyApple,
),
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
margin: EdgeInsets.only(
top: 40,
bottom: 10,
),
child: ButtonTheme(
height: 55,
padding: EdgeInsets.only(
top: 5,
right: 12,
bottom: 12,
left: 10
),
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
color: peacockBlue,
child: Text(
'OK',
style: TextStyle(
height: textHeight,
color: Colors.white,
fontSize: 24,
),
),
onPressed: () async{
Navigator.of(context).pop();
}
),
),
),
],
),
);
}
);
}
}
),
),
),
],
),
);
}
);
}
}
return WillPopScope(
onWillPop: _popBack,
child: Scaffold(
@ -183,6 +579,100 @@ class _SettingsState extends State<Settings> {
],
),
) : Container()),
Container(
padding: myBoxPadding,
decoration: myBoxDecoration(ivory),
child: Row(
children: [
Expanded(
flex: 2,
child: IconButton(
alignment: Alignment.center,
icon: Icon(
Icons.arrow_back,
color: candyApple,
),
iconSize: 60.0,
onPressed: () {
_showDBExportDialog();
},
),
),
Expanded(
flex: 6,
child: Text(
'Export DataBase',
textAlign: TextAlign.center,
style: TextStyle(
height: textHeight,
fontSize: 20,
),
),
),
Expanded(
flex: 2,
child: IconButton(
alignment: Alignment.center,
icon: Icon(
Icons.arrow_forward,
color: candyApple,
),
iconSize: 60.0,
onPressed: () {
_showDBExportDialog();
},
),
),
],
),
),
Container(
padding: myBoxPadding,
decoration: myBoxDecoration(ivory),
child: Row(
children: [
Expanded(
flex: 2,
child: IconButton(
alignment: Alignment.center,
icon: Icon(
Icons.arrow_forward,
color: candyApple,
),
iconSize: 60.0,
onPressed: () {
_showDBImportDialog();
},
),
),
Expanded(
flex: 6,
child: Text(
'Import DataBase',
textAlign: TextAlign.center,
style: TextStyle(
height: textHeight,
fontSize: 20,
),
),
),
Expanded(
flex: 2,
child: IconButton(
alignment: Alignment.center,
icon: Icon(
Icons.arrow_back,
color: candyApple,
),
iconSize: 60.0,
onPressed: () {
_showDBImportDialog();
},
),
),
],
),
),
],
),
),
@ -192,6 +682,7 @@ class _SettingsState extends State<Settings> {
Future<void> updateState() async {
String _evServer = await getPreferenceElevationServer();
String _oWMK = await getPreferenceOpenWeatherMapApiKey();
String _dbEFNS = await getPreferenceDBExportFileName();
bool _useElev = await getPreferenceUseElevation();
bool _useWTHR = await getPreferenceUseWeather();
setState(() {
@ -200,6 +691,16 @@ class _SettingsState extends State<Settings> {
this._useElevation = _useElev;
this._useWeather = _useWTHR;
this.shortOWMAK = _oWMK.substring(0, 5);
this._dbExportFileNameString = _dbEFNS;
});
}
void _updatedbExportFileName() {
String _dbEFNS = this._dbExportFileNameString;
setPreferenceDBExportFileName(_dbEFNS).then((bool committed) {
setState(() {
this._dbExportFileNameString = _dbEFNS;
});
});
}

View File

@ -901,7 +901,7 @@ class _WeatherState extends State<Weather> {
return Container(
width: 100,
child: CachedNetworkImage(
imageUrl: 'https://openweathermap.org/img/w/${widget.weatherConditionsIcon}.png',
imageUrl: 'http://openweathermap.org/img/w/${widget.weatherConditionsIcon}.png',
),
);
}

View File

@ -81,7 +81,7 @@ class _WeatherForeCastState extends State<WeatherForeCast> {
return Container(
width: 100,
child: CachedNetworkImage(
imageUrl: 'https://openweathermap.org/img/w/${foreCast['weather'][0]['icon']}.png',
imageUrl: 'http://openweathermap.org/img/w/${foreCast['weather'][0]['icon']}.png',
),
);
}