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 {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "at.htldonaustadt.server_pinger"
minSdkVersion 16
targetSdkVersion 28

View file

@ -16,4 +16,31 @@ class Server {
this.displayName,
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:convert';
import 'dart:io';
import 'dart:math';
import 'package:basic_utils/basic_utils.dart';
import 'package:flutter/material.dart';
import 'package:server_pinger/server.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'server.dart';
@ -18,26 +20,12 @@ class ServerList extends StatefulWidget {
}
class _ServerListState extends State<ServerList> {
_ServerListState() {
_initServersFromPreferences();
}
// Currently expanded server list entry, -1 = no field is expanded
int selectedServer = -1;
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",
),
];
List<Server> servers;
Timer timer;
Random rnd = new Random();
@ -46,9 +34,6 @@ class _ServerListState extends State<ServerList> {
void initState() {
super.initState();
// TODO: load shared preferences
_updateAllServers();
timer = new Timer.periodic(
new Duration(seconds: 30), (_) => _updateAllServers());
}
@ -138,18 +123,18 @@ class _ServerListState extends State<ServerList> {
}
void _addServer(String customName, String uri) {
// TODO: add to shared preferences
Server server = Server(customName, uri);
servers.add(server);
_saveServersToPreferences();
_updateServerStatus(server);
Navigator.of(context).pop();
}
void _removeServer(int index) {
// TODO: remove from shared preferences
setState(() {
servers.removeAt(index);
});
_saveServersToPreferences();
}
void _modifyServerForm(Server server) {
@ -218,7 +203,7 @@ class _ServerListState extends State<ServerList> {
}
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: [
Expanded(
child: Align(
@ -341,7 +326,7 @@ class _ServerListState extends State<ServerList> {
child: info),
);
});
list.add(
entries.add(
GestureDetector(
onTap: _addServerForm,
child: Container(
@ -360,7 +345,7 @@ class _ServerListState extends State<ServerList> {
),
),
);
return list;
return entries;
}
static Future<ServerStatus> _checkStatus(String uri) async {
@ -469,4 +454,37 @@ class _ServerListState extends State<ServerList> {
if (uri.length < 100) return uri;
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
environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.4.0 <3.0.0"
dependencies:
flutter: