Server list persistence added #28

Merged
Jeremy Kescher merged 1 commit from jeremy/0129/persistence-shared-preferences into master 2020-01-29 13:27:06 +00:00
4 changed files with 72 additions and 28 deletions

View file

@ -37,7 +37,6 @@ android {
} }
defaultConfig { defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "at.htldonaustadt.server_pinger" applicationId "at.htldonaustadt.server_pinger"
minSdkVersion 16 minSdkVersion 16
targetSdkVersion 28 targetSdkVersion 28

View file

@ -16,4 +16,31 @@ class Server {
this.displayName, this.displayName,
this.uri, this.uri,
); );
static Server fromPrefs(PrefsServer prefsServer) {
return Server(prefsServer.displayName, prefsServer.uri);
}
}
class PrefsServer {
String displayName;
String uri;
PrefsServer(
this.displayName,
this.uri,
);
static PrefsServer fromServer(Server server) {
return PrefsServer(server.displayName, server.uri);
}
Map<String, dynamic> toJson() => {
'displayName': displayName,
'uri': uri,
};
PrefsServer.fromJson(Map<String, dynamic> json)
: displayName = json['displayName'],
uri = json['uri'];
} }

View file

@ -1,10 +1,12 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:math'; import 'dart:math';
import 'package:basic_utils/basic_utils.dart'; import 'package:basic_utils/basic_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:server_pinger/server.dart'; import 'package:server_pinger/server.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'server.dart'; import 'server.dart';
@ -18,26 +20,12 @@ class ServerList extends StatefulWidget {
} }
class _ServerListState extends State<ServerList> { class _ServerListState extends State<ServerList> {
_ServerListState() {
_initServersFromPreferences();
}
// Currently expanded server list entry, -1 = no field is expanded // Currently expanded server list entry, -1 = no field is expanded
int selectedServer = -1; int selectedServer = -1;
List<Server> servers = [ List<Server> servers;
new Server(
"example.com",
"http://example.com",
),
new Server(
"youtube.com",
"https://youtube.com",
),
new Server(
"My VPS",
"http://kescher.at",
),
new Server(
"Raspberry Pi",
"https://pi.kescher.at/isup",
),
];
Timer timer; Timer timer;
Random rnd = new Random(); Random rnd = new Random();
@ -46,9 +34,6 @@ class _ServerListState extends State<ServerList> {
void initState() { void initState() {
super.initState(); super.initState();
// TODO: load shared preferences
_updateAllServers();
timer = new Timer.periodic( timer = new Timer.periodic(
new Duration(seconds: 30), (_) => _updateAllServers()); new Duration(seconds: 30), (_) => _updateAllServers());
} }
@ -138,18 +123,18 @@ class _ServerListState extends State<ServerList> {
} }
void _addServer(String customName, String uri) { void _addServer(String customName, String uri) {
// TODO: add to shared preferences
Server server = Server(customName, uri); Server server = Server(customName, uri);
servers.add(server); servers.add(server);
_saveServersToPreferences();
_updateServerStatus(server); _updateServerStatus(server);
Navigator.of(context).pop(); Navigator.of(context).pop();
} }
void _removeServer(int index) { void _removeServer(int index) {
// TODO: remove from shared preferences
setState(() { setState(() {
servers.removeAt(index); servers.removeAt(index);
}); });
_saveServersToPreferences();
} }
void _modifyServerForm(Server server) { void _modifyServerForm(Server server) {
@ -218,7 +203,7 @@ class _ServerListState extends State<ServerList> {
} }
List<Widget> _createChildren() { List<Widget> _createChildren() {
List<Widget> list = new List<Widget>.generate(servers.length, (int index) { List<Widget> entries = new List<Widget>.generate(servers.length, (int index) {
Widget info = Row(children: [ Widget info = Row(children: [
Expanded( Expanded(
child: Align( child: Align(
@ -341,7 +326,7 @@ class _ServerListState extends State<ServerList> {
child: info), child: info),
); );
}); });
list.add( entries.add(
GestureDetector( GestureDetector(
onTap: _addServerForm, onTap: _addServerForm,
child: Container( child: Container(
@ -360,7 +345,7 @@ class _ServerListState extends State<ServerList> {
), ),
), ),
); );
return list; return entries;
} }
static Future<ServerStatus> _checkStatus(String uri) async { static Future<ServerStatus> _checkStatus(String uri) async {
@ -469,4 +454,37 @@ class _ServerListState extends State<ServerList> {
if (uri.length < 100) return uri; if (uri.length < 100) return uri;
return uri.substring(0, 100) + "..."; return uri.substring(0, 100) + "...";
} }
void _initServersFromPreferences() {
servers = [];
SharedPreferences.getInstance().then((prefs) {
List<String> serverStrings = prefs.getStringList('servers');
if (serverStrings == null) {
prefs.setStringList('servers', []).then((_) {
serverStrings = prefs.getStringList('servers');
});
}
serverStrings.forEach((str) {
Server server = Server.fromPrefs(
PrefsServer.fromJson(
json.decode(str),
),
);
_updateServerStatus(server);
servers.add(server);
});
});
}
void _saveServersToPreferences() {
SharedPreferences.getInstance().then((prefs) {
prefs
.setStringList(
'servers',
new List<String>.generate(servers.length, (int i) {
return json.encode(PrefsServer.fromServer(servers[i]).toJson());
}))
.then((set) {});
});
}
} }

View file

@ -14,7 +14,7 @@ description: A new Flutter application.
version: 1.0.0+1 version: 1.0.0+1
environment: environment:
sdk: ">=2.1.0 <3.0.0" sdk: ">=2.4.0 <3.0.0"
dependencies: dependencies:
flutter: flutter: