From 504139f2482dbbd83ea718460ebac8a44ec9d6cd Mon Sep 17 00:00:00 2001 From: estevez Date: Mon, 10 Sep 2018 03:06:35 +0300 Subject: [PATCH] Add settings for Hass.io url and password --- .idea/libraries/Dart_Packages.xml | 8 +++ .idea/libraries/Flutter_Plugins.xml | 4 +- .idea/workspace.xml | 71 +++++++++++++++---- lib/main.dart | 67 ++++++++++++------ lib/settings.dart | 102 ++++++++++++++++++++++++++++ pubspec.lock | 8 +++ pubspec.yaml | 1 + 7 files changed, 225 insertions(+), 36 deletions(-) create mode 100644 lib/settings.dart diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index a45b5ee..85f4a1e 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -247,6 +247,13 @@ + + + + + + @@ -423,6 +430,7 @@ + diff --git a/.idea/libraries/Flutter_Plugins.xml b/.idea/libraries/Flutter_Plugins.xml index b0f6971..0d171fd 100644 --- a/.idea/libraries/Flutter_Plugins.xml +++ b/.idea/libraries/Flutter_Plugins.xml @@ -1,6 +1,8 @@ - + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3284bac..a469ba2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,13 @@ + + + + + @@ -21,8 +26,8 @@ - - + + @@ -30,6 +35,26 @@ + + + + + + + + + + + + + + + + + + + + @@ -57,11 +82,13 @@ - + - + - + - + - + @@ -253,16 +280,16 @@ - + - - + + - - + + @@ -328,10 +355,26 @@ + + + + + + + + + + + + + + + + - - + + diff --git a/lib/main.dart b/lib/main.dart index f50158f..a0753a6 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,9 @@ import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +part 'settings.dart'; void main() => runApp(new MyApp()); @@ -13,13 +16,17 @@ class MyApp extends StatelessWidget { theme: new ThemeData( primarySwatch: Colors.blue, ), - home: new MyHomePage(title: 'Hass main'), + initialRoute: "/", + routes: { + "/": (context) => MainPage(title: 'Hass Client'), + "/settings": (context) => SettingsPage(title: "Settings") + }, ); } } -class MyHomePage extends StatefulWidget { - MyHomePage({Key key, this.title}) : super(key: key); +class MainPage extends StatefulWidget { + MainPage({Key key, this.title}) : super(key: key); // This widget is the home page of your application. It is stateful, meaning // that it has a State object (defined below) that contains fields that affect @@ -33,24 +40,33 @@ class MyHomePage extends StatefulWidget { final String title; @override - _MyHomePageState createState() => new _MyHomePageState(); + _MainPageState createState() => new _MainPageState(); } -class _MyHomePageState extends State { - List entities = []; +class _MainPageState extends State { + List _entities = []; + String _hassioUrl = ""; + String _hassioPassword = ""; - void _getHassStates() async { - String dataURL = "https://homeassistant:8123/api/states"; - http.Response response = await http.get(dataURL, headers: {"X-HA-Access": "password"}); + @override + void initState() { + super.initState(); + _loadSettings(); + } + + _loadSettings() async { + SharedPreferences prefs = await SharedPreferences.getInstance(); setState(() { - entities = json.decode(response.body); + _hassioUrl = "https://" + prefs.getString('hassio-domain') +":" + prefs.getString('hassio-port') + "/api/states"; + _hassioPassword = prefs.getString('hassio-password'); }); + } + + void _getHassioEntities() async { + await _loadSettings(); + http.Response response = await http.get(_hassioUrl, headers: {"X-HA-Access": _hassioPassword}); setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. + _entities = json.decode(response.body); }); } @@ -61,8 +77,8 @@ class _MyHomePageState extends State { children: [ new ListTile( leading: const Icon(Icons.device_hub), - title: Text("${entities[i]["entity_id"]}"), - subtitle: Text("${entities[i]["state"]}"), + title: Text("${_entities[i]["entity_id"]}"), + subtitle: Text("${_entities[i]["state"]}"), ), ], ), @@ -72,7 +88,7 @@ class _MyHomePageState extends State { return Padding( padding: EdgeInsets.all(10.0), - child: Text("Row ${entities[i]["entity_id"]}") + child: Text("Row ${_entities[i]["entity_id"]}") ); } @@ -93,10 +109,19 @@ class _MyHomePageState extends State { drawer: new Drawer( child: ListView( children: [ - new DrawerHeader(child: Text("Menu")), + new UserAccountsDrawerHeader( + accountName: Text("Edwin Home"), + accountEmail: Text("edwin-home.duckdns.org"), + currentAccountPicture: new CircleAvatar( + backgroundImage: new NetworkImage("https://edwin-home.duckdns.org:8123/static/icons/favicon-192x192.png"), + ), + ), new ListTile( leading: Icon(Icons.settings), title: Text("Settings"), + onTap: () { + Navigator.pushNamed(context, '/settings'); + }, ), new AboutListTile( applicationName: "Hass Client", @@ -107,12 +132,12 @@ class _MyHomePageState extends State { ), ), body: ListView.builder( - itemCount: entities.length, + itemCount: _entities.length, itemBuilder: (BuildContext context, int position) { return parseEntity(position); }), floatingActionButton: new FloatingActionButton( - onPressed: _getHassStates, + onPressed: _getHassioEntities, tooltip: 'Increment', child: new Icon(Icons.refresh), ), // This trailing comma makes auto-formatting nicer for build methods. diff --git a/lib/settings.dart b/lib/settings.dart new file mode 100644 index 0000000..66b24b9 --- /dev/null +++ b/lib/settings.dart @@ -0,0 +1,102 @@ +part of 'main.dart'; + +class SettingsPage extends StatefulWidget { + SettingsPage({Key key, this.title}) : super(key: key); + + final String title; + + @override + _SettingsPageState createState() => new _SettingsPageState(); +} + +class _SettingsPageState extends State { + String _hassioDomain = ""; + String _hassioPort = ""; + String _hassioPassword = ""; + + @override + void initState() { + super.initState(); + _loadSettings(); + } + + _loadSettings() async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + + setState(() { + _hassioDomain = prefs.getString("hassio-domain"); + _hassioPort = prefs.getString("hassio-port"); + _hassioPassword = prefs.getString("hassio-password"); + }); + } + + _saveSettings() async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + + setState(() { + prefs.setString("hassio-domain", _hassioDomain); + prefs.setString("hassio-port", _hassioPort); + prefs.setString("hassio-password", _hassioPassword); + _hassioPassword = prefs.getString('hassio-password'); + }); + } + + @override + Widget build(BuildContext context) { + // This method is rerun every time setState is called, for instance as done + // by the _incrementCounter method above. + // + // The Flutter framework has been optimized to make rerunning build methods + // fast, so that you can just rebuild anything that needs updating rather + // than having to individually change instances of widgets. + return new Scaffold( + appBar: new AppBar( + leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: (){ + _saveSettings(); + Navigator.pop(context); + }), + // Here we take the value from the MyHomePage object that was created by + // the App.build method, and use it to set our appbar title. + title: new Text(widget.title), + ), + body: ListView( + padding: const EdgeInsets.all(20.0), + children: [ + new TextField( + decoration: InputDecoration( + labelText: "Home Assistant domain or ip address" + ), + controller: TextEditingController( + text: _hassioDomain + ), + onChanged: (value) { + _hassioDomain = value; + }, + ), + new TextField( + decoration: InputDecoration( + labelText: "Home Assistant port" + ), + controller: TextEditingController( + text: _hassioPort + ), + onChanged: (value) { + _hassioPort = value; + }, + ), + new TextField( + decoration: InputDecoration( + labelText: "Home Assistant password" + ), + controller: TextEditingController( + text: _hassioPassword + ), + onChanged: (value) { + _hassioPassword = value; + }, + ) + ], + ), + ); + } +} \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 498026c..15d4bbc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -242,6 +242,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.0+1" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.2" shelf: dependency: transitive description: @@ -382,3 +389,4 @@ packages: version: "2.1.15" sdks: dart: ">=2.0.0-dev.68.0 <3.0.0" + flutter: ">=0.1.4 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 2618f90..f83101a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,6 +15,7 @@ environment: dependencies: flutter: sdk: flutter + shared_preferences: any # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.