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 {
final data = await rootBundle.loadString('config/credentials.json');
var data_ = json.decode(data) as Map<String, dynamic>;
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<String, String> 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<List> 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();
}
}

View File

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

View File

@ -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<Map<String, dynamic>> readJson() async {
class _CloudService extends State<CloudService>{
late final Map<String, dynamic> credentials;
final CloudServiceAPI cloudServiceAPI = CloudServiceAPI();
CloudServiceAPI cloudServiceAPI = CloudServiceAPI();
static SharedPreferences? preferencesInstance;
@override
Widget build(BuildContext context) {

View File

@ -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<MyHomePage> {
int _counter = 0;
CloudServiceAPI cloudServiceAPI = CloudServiceAPI();
void _incrementCounter() {
setState(() {
_counter++;

View File

@ -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<Settings> createState() => _SettingsState();
@ -40,23 +34,31 @@ class _SettingsState extends State<Settings> {
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: <TextInputFormatter>[
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);
},
)
],