From 66de5e6d91348861d8388aedb7ecbc594a0429cd Mon Sep 17 00:00:00 2001 From: Simeon5566 Date: Thu, 24 Nov 2022 16:59:53 +0100 Subject: [PATCH] implemented Settings functions --- lib/objects/cloud_service_api.dart | 38 ++++++++++++++++++++++-- lib/screens/bluetooth_screen.dart | 3 ++ lib/screens/cloud_service_ui.dart | 7 +++-- lib/screens/main_page.dart | 3 +- lib/screens/settings.dart | 46 ++++++++++++++++-------------- 5 files changed, 68 insertions(+), 29 deletions(-) diff --git a/lib/objects/cloud_service_api.dart b/lib/objects/cloud_service_api.dart index bc49006..3727e8d 100644 --- a/lib/objects/cloud_service_api.dart +++ b/lib/objects/cloud_service_api.dart @@ -7,7 +7,7 @@ import 'dart:developer'; Future> readJson() async { final data = await rootBundle.loadString('config/credentials.json'); var data_ = json.decode(data) as Map; - debugPrint(data_.toString()); + log("loaded json input $data_"); return data_; } @@ -16,6 +16,8 @@ class CloudServiceAPI { static late Future loadJson; static late String basicAuth; static late String address; + static late String username; + static late String password; static late Map headers; static bool initState = false; @@ -28,8 +30,8 @@ class CloudServiceAPI { Future loadConfig() async{ credentials = await readJson(); - String username = credentials['username']; - String password = credentials['password']; + username = credentials['username']; + password = credentials['password']; address = credentials['address']; basicAuth = 'Basic ${base64.encode(utf8.encode('$username:$password'))}'; headers = { @@ -37,6 +39,15 @@ class CloudServiceAPI { 'content-type': 'application/json', 'accept': 'application/json', }; + log("init Config $headers"); + } + void reloadConfig() { + basicAuth = 'Basic ${base64.encode(utf8.encode('$username:$password'))}'; + headers = { + 'authorization': basicAuth, + 'content-type': 'application/json', + 'accept': 'application/json', + }; } Future getDevices() async { var url = Uri.https(address, '/api/devices'); @@ -70,4 +81,25 @@ class CloudServiceAPI { debugPrint('Error createDevice: ${r.statusCode.toString()}'); return false; } + String getAddress(){ + return address; + } + String getUsername(){ + return username; + } + String getPassword(){ + return password; + } + void setAddress(String input){ + address = input; + reloadConfig(); + } + void setUsername(String input){ + username = input; + reloadConfig(); + } + void setPassword(String input){ + password = input; + reloadConfig(); + } } \ No newline at end of file diff --git a/lib/screens/bluetooth_screen.dart b/lib/screens/bluetooth_screen.dart index 3e6144f..34f0cf5 100644 --- a/lib/screens/bluetooth_screen.dart +++ b/lib/screens/bluetooth_screen.dart @@ -145,3 +145,6 @@ class _BluetoothScreen extends State { fontSize: 16.0); } } + +class BluetoothScan{ +} diff --git a/lib/screens/cloud_service_ui.dart b/lib/screens/cloud_service_ui.dart index 90b4b6b..76825d5 100644 --- a/lib/screens/cloud_service_ui.dart +++ b/lib/screens/cloud_service_ui.dart @@ -1,11 +1,11 @@ import 'dart:convert'; - +import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_provisioning_for_iot/objects/cloud_service_api.dart'; import 'package:flutter_provisioning_for_iot/widgets/sidebar.dart'; - +import 'package:shared_preferences/shared_preferences.dart'; class CloudService extends StatefulWidget { const CloudService({Key? key}) : super(key: key); @@ -24,7 +24,8 @@ Future> readJson() async { class _CloudService extends State{ late final Map credentials; - final CloudServiceAPI cloudServiceAPI = CloudServiceAPI(); + CloudServiceAPI cloudServiceAPI = CloudServiceAPI(); + static SharedPreferences? preferencesInstance; @override Widget build(BuildContext context) { diff --git a/lib/screens/main_page.dart b/lib/screens/main_page.dart index ed5c06c..1573ce3 100644 --- a/lib/screens/main_page.dart +++ b/lib/screens/main_page.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter_provisioning_for_iot/objects/cloud_service_api.dart'; import 'package:flutter_provisioning_for_iot/widgets/sidebar.dart'; import '../objects/create_material_color.dart'; @@ -38,7 +39,7 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { int _counter = 0; - + CloudServiceAPI cloudServiceAPI = CloudServiceAPI(); void _incrementCounter() { setState(() { _counter++; diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index 3b3a843..82220e5 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -1,21 +1,15 @@ import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; -import 'package:shared_preferences/shared_preferences.dart'; +import 'package:flutter_provisioning_for_iot/objects/cloud_service_api.dart'; import '../widgets/sidebar.dart'; class Settings extends StatefulWidget { const Settings({Key? key}) : super(key: key); + static CloudServiceAPI cloudService = CloudServiceAPI(); - static const initIp = ""; - - static var ip = initIp; - - static const initPort = ""; - - static var port = initPort; - - static SharedPreferences? preferencesInstance; + static String address = cloudService.getAddress(); + static String username = cloudService.getUsername(); + static String password = cloudService.getPassword(); @override State createState() => _SettingsState(); @@ -40,23 +34,31 @@ class _SettingsState extends State { TextFormField( decoration: const InputDecoration( labelText: "Address for the Device Manager"), - initialValue: Settings.ip, + initialValue: Settings.address, + keyboardType: TextInputType.text, onChanged: (String newValue) { - Settings.ip = newValue; - Settings.preferencesInstance?.setString("IP", newValue); + Settings.address = newValue; + Settings.cloudService.setAddress(newValue); }, ), TextFormField( decoration: const InputDecoration( - labelText: "Port for the Device Manager"), - keyboardType: TextInputType.number, - inputFormatters: [ - FilteringTextInputFormatter.digitsOnly - ], - initialValue: Settings.port, + labelText: "Username"), + keyboardType: TextInputType.text, + initialValue: Settings.username, onChanged: (String newValue) { - Settings.port = newValue; - Settings.preferencesInstance?.setString("Port", newValue); + Settings.username = newValue; + Settings.cloudService.setUsername(newValue); + }, + ), + TextFormField( + decoration: const InputDecoration( + labelText: "Password"), + keyboardType: TextInputType.text, + initialValue: Settings.password, + onChanged: (String newValue) { + Settings.password = newValue; + Settings.cloudService.setPassword(newValue); }, ) ],