implemented Settings functions

This commit is contained in:
Simeon5566 2022-11-24 16:59:53 +01:00
parent e1a4cc6f5a
commit 66de5e6d91
5 changed files with 68 additions and 29 deletions

View File

@ -7,7 +7,7 @@ import 'dart:developer';
Future<Map<String, dynamic>> readJson() async { Future<Map<String, dynamic>> readJson() async {
final data = await rootBundle.loadString('config/credentials.json'); final data = await rootBundle.loadString('config/credentials.json');
var data_ = json.decode(data) as Map<String, dynamic>; var data_ = json.decode(data) as Map<String, dynamic>;
debugPrint(data_.toString()); log("loaded json input $data_");
return data_; return data_;
} }
@ -16,6 +16,8 @@ class CloudServiceAPI {
static late Future loadJson; static late Future loadJson;
static late String basicAuth; static late String basicAuth;
static late String address; static late String address;
static late String username;
static late String password;
static late Map<String, String> headers; static late Map<String, String> headers;
static bool initState = false; static bool initState = false;
@ -28,8 +30,8 @@ class CloudServiceAPI {
Future loadConfig() async{ Future loadConfig() async{
credentials = await readJson(); credentials = await readJson();
String username = credentials['username']; username = credentials['username'];
String password = credentials['password']; password = credentials['password'];
address = credentials['address']; address = credentials['address'];
basicAuth = 'Basic ${base64.encode(utf8.encode('$username:$password'))}'; basicAuth = 'Basic ${base64.encode(utf8.encode('$username:$password'))}';
headers = { headers = {
@ -37,6 +39,15 @@ class CloudServiceAPI {
'content-type': 'application/json', 'content-type': 'application/json',
'accept': '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<List> getDevices() async { Future<List> getDevices() async {
var url = Uri.https(address, '/api/devices'); var url = Uri.https(address, '/api/devices');
@ -70,4 +81,25 @@ class CloudServiceAPI {
debugPrint('Error createDevice: ${r.statusCode.toString()}'); debugPrint('Error createDevice: ${r.statusCode.toString()}');
return false; 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();
}
} }

View File

@ -145,3 +145,6 @@ class _BluetoothScreen extends State<BluetoothScreen> {
fontSize: 16.0); fontSize: 16.0);
} }
} }
class BluetoothScan{
}

View File

@ -1,11 +1,11 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_provisioning_for_iot/objects/cloud_service_api.dart'; import 'package:flutter_provisioning_for_iot/objects/cloud_service_api.dart';
import 'package:flutter_provisioning_for_iot/widgets/sidebar.dart'; import 'package:flutter_provisioning_for_iot/widgets/sidebar.dart';
import 'package:shared_preferences/shared_preferences.dart';
class CloudService extends StatefulWidget { class CloudService extends StatefulWidget {
const CloudService({Key? key}) : super(key: key); const CloudService({Key? key}) : super(key: key);
@ -24,7 +24,8 @@ Future<Map<String, dynamic>> readJson() async {
class _CloudService extends State<CloudService>{ class _CloudService extends State<CloudService>{
late final Map<String, dynamic> credentials; late final Map<String, dynamic> credentials;
final CloudServiceAPI cloudServiceAPI = CloudServiceAPI(); CloudServiceAPI cloudServiceAPI = CloudServiceAPI();
static SharedPreferences? preferencesInstance;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; 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 'package:flutter_provisioning_for_iot/widgets/sidebar.dart';
import '../objects/create_material_color.dart'; import '../objects/create_material_color.dart';
@ -38,7 +39,7 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
int _counter = 0; int _counter = 0;
CloudServiceAPI cloudServiceAPI = CloudServiceAPI();
void _incrementCounter() { void _incrementCounter() {
setState(() { setState(() {
_counter++; _counter++;

View File

@ -1,21 +1,15 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter_provisioning_for_iot/objects/cloud_service_api.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../widgets/sidebar.dart'; import '../widgets/sidebar.dart';
class Settings extends StatefulWidget { class Settings extends StatefulWidget {
const Settings({Key? key}) : super(key: key); const Settings({Key? key}) : super(key: key);
static CloudServiceAPI cloudService = CloudServiceAPI();
static const initIp = ""; static String address = cloudService.getAddress();
static String username = cloudService.getUsername();
static var ip = initIp; static String password = cloudService.getPassword();
static const initPort = "";
static var port = initPort;
static SharedPreferences? preferencesInstance;
@override @override
State<Settings> createState() => _SettingsState(); State<Settings> createState() => _SettingsState();
@ -40,23 +34,31 @@ class _SettingsState extends State<Settings> {
TextFormField( TextFormField(
decoration: const InputDecoration( decoration: const InputDecoration(
labelText: "Address for the Device Manager"), labelText: "Address for the Device Manager"),
initialValue: Settings.ip, initialValue: Settings.address,
keyboardType: TextInputType.text,
onChanged: (String newValue) { onChanged: (String newValue) {
Settings.ip = newValue; Settings.address = newValue;
Settings.preferencesInstance?.setString("IP", newValue); Settings.cloudService.setAddress(newValue);
}, },
), ),
TextFormField( TextFormField(
decoration: const InputDecoration( decoration: const InputDecoration(
labelText: "Port for the Device Manager"), labelText: "Username"),
keyboardType: TextInputType.number, keyboardType: TextInputType.text,
inputFormatters: <TextInputFormatter>[ initialValue: Settings.username,
FilteringTextInputFormatter.digitsOnly
],
initialValue: Settings.port,
onChanged: (String newValue) { onChanged: (String newValue) {
Settings.port = newValue; Settings.username = newValue;
Settings.preferencesInstance?.setString("Port", 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);
}, },
) )
], ],