adguard-home-manager/lib/providers/servers_provider.dart

128 lines
3.3 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'package:adguard_home_manager/functions/conversions.dart';
import 'package:adguard_home_manager/models/server.dart';
class ServersProvider with ChangeNotifier {
Database? _dbInstance;
List<Server> _serversList = [];
Server? _selectedServer;
List<Server> get serversList {
return _serversList;
}
Server? get selectedServer {
return _selectedServer;
}
void setDbInstance(Database db) {
_dbInstance = db;
}
void addServer(Server server) {
_serversList.add(server);
notifyListeners();
}
2022-09-26 18:49:41 +02:00
Future<bool> createServer(Server server) async {
final saved = await saveServerIntoDb(server);
if (saved == true) {
if (server.defaultServer == true) {
final defaultServer = await setDefaultServer(server);
if (defaultServer == true) {
_serversList.add(server);
notifyListeners();
return true;
}
else {
return false;
}
}
else {
_serversList.add(server);
notifyListeners();
return true;
}
}
else {
return false;
}
}
Future<bool> setDefaultServer(Server server) async {
final updated = await setDefaultServerDb(server.id);
if (updated == true) {
List<Server> newServers = _serversList.map((s) {
if (s.id == server.id) {
s.defaultServer = true;
return s;
}
else {
s.defaultServer = false;
return s;
}
}).toList();
_serversList = newServers;
notifyListeners();
return true;
}
else {
return false;
}
}
Future<bool> saveServerIntoDb(Server server) async {
try {
return await _dbInstance!.transaction((txn) async {
await txn.rawInsert(
'INSERT INTO servers (id, name, connectionMethod, domain, path, port, user, password, defaultServer) VALUES ("${server.id}", "${server.name}", "${server.connectionMethod}", "${server.domain}", "${server.path}", ${server.port}, "${server.user}", "${server.password}", 0)',
);
return true;
});
} catch (e) {
return false;
}
}
Future<bool> setDefaultServerDb(String id) async {
try {
return await _dbInstance!.transaction((txn) async {
await txn.rawUpdate(
'UPDATE servers SET defaultServer = 0 WHERE defaultServer = 1',
);
await txn.rawUpdate(
'UPDATE servers SET defaultServer = 1 WHERE id = "$id"',
);
return true;
});
} catch (e) {
return false;
}
}
void saveFromDb(List<Map<String, dynamic>>? data) async {
if (data != null) {
for (var server in data) {
final Server serverObj = Server(
2022-09-26 18:49:41 +02:00
id: server['id'],
name: server['name'],
connectionMethod: server['connectionMethod'],
domain: server['domain'],
path: server['path'],
port: server['port'],
user: server['user'],
password: server['password'],
2022-09-26 18:49:41 +02:00
defaultServer: convertFromIntToBool(server['defaultServer'])!,
);
_serversList.add(serverObj);
2022-09-26 18:49:41 +02:00
if (convertFromIntToBool(server['defaultServer']) == true) {
_selectedServer = serverObj;
}
}
}
notifyListeners();
}
}