From dfab55d70e5afccecd3f21fdb96c960d76bda792 Mon Sep 17 00:00:00 2001 From: Jeremy Kescher Date: Wed, 29 Jan 2020 14:26:10 +0100 Subject: [PATCH] Server persistence added --- android/app/build.gradle | 1 - lib/server.dart | 27 ++++++++++++++++ lib/serverlist.dart | 70 +++++++++++++++++++++++++--------------- pubspec.yaml | 2 +- 4 files changed, 72 insertions(+), 28 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 6b0543a..696afba 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -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 diff --git a/lib/server.dart b/lib/server.dart index 05cba87..ee0db44 100644 --- a/lib/server.dart +++ b/lib/server.dart @@ -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 toJson() => { + 'displayName': displayName, + 'uri': uri, + }; + + PrefsServer.fromJson(Map json) + : displayName = json['displayName'], + uri = json['uri']; } diff --git a/lib/serverlist.dart b/lib/serverlist.dart index 407e2f5..ece22e8 100644 --- a/lib/serverlist.dart +++ b/lib/serverlist.dart @@ -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 { + _ServerListState() { + _initServersFromPreferences(); + } // Currently expanded server list entry, -1 = no field is expanded int selectedServer = -1; - List 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 servers; Timer timer; Random rnd = new Random(); @@ -46,9 +34,6 @@ class _ServerListState extends State { 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 { } 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 { } List _createChildren() { - List list = new List.generate(servers.length, (int index) { + List entries = new List.generate(servers.length, (int index) { Widget info = Row(children: [ Expanded( child: Align( @@ -341,7 +326,7 @@ class _ServerListState extends State { child: info), ); }); - list.add( + entries.add( GestureDetector( onTap: _addServerForm, child: Container( @@ -360,7 +345,7 @@ class _ServerListState extends State { ), ), ); - return list; + return entries; } static Future _checkStatus(String uri) async { @@ -469,4 +454,37 @@ class _ServerListState extends State { if (uri.length < 100) return uri; return uri.substring(0, 100) + "..."; } + + void _initServersFromPreferences() { + servers = []; + SharedPreferences.getInstance().then((prefs) { + List 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.generate(servers.length, (int i) { + return json.encode(PrefsServer.fromServer(servers[i]).toJson()); + })) + .then((set) {}); + }); + } } diff --git a/pubspec.yaml b/pubspec.yaml index 9853b57..2afb138 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: -- 2.47.0