unknown
12 months ago
6 changed files with 217 additions and 66 deletions
@ -1,32 +1,36 @@ |
|||||||
import 'package:shared_preferences/shared_preferences.dart'; |
import '../pages/login/controller/loginController.dart'; // Sesuaikan path dengan struktur proyek Anda |
||||||
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart'; |
|
||||||
import '../pages/sign_in_page.dart'; |
|
||||||
|
|
||||||
import 'package:path_provider/path_provider.dart'; |
|
||||||
|
|
||||||
Future<String> getBaseUrl() async { |
Future<String> getBaseUrl() async { |
||||||
try { |
try { |
||||||
String savedValue = await getSavedValue(); |
List ipDomainList = await ControllerLogin().fetchData(); |
||||||
|
|
||||||
|
if (ipDomainList.isNotEmpty) { |
||||||
|
String savedValue = ipDomainList.first['ipOrDomain'] ?? ''; |
||||||
|
|
||||||
// Check if savedValue is a domain, IP with or without port |
// Check if savedValue is a domain, IP with or without port |
||||||
String baseUrl; |
String baseUrl; |
||||||
|
|
||||||
if (savedValue.contains(':')) { |
if (savedValue.contains(':')) { |
||||||
// If savedValue contains a colon, assume it's an IP with port |
// If savedValue contains a colon, assume it's an IP with port |
||||||
baseUrl = 'http://$savedValue/api/v1'; |
baseUrl = 'http://$savedValue/api/v1'; |
||||||
} else if (RegExp(r'^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$').hasMatch(savedValue)) { |
} else if (RegExp(r'^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$') |
||||||
// If savedValue matches the domain pattern, assume it's a domain |
.hasMatch(savedValue)) { |
||||||
baseUrl = 'https://$savedValue/api/v1'; |
// If savedValue matches the domain pattern, assume it's a domain |
||||||
|
baseUrl = 'https://$savedValue/api/v1'; |
||||||
|
} else { |
||||||
|
// If it doesn't match both patterns, assume it's an IP without port |
||||||
|
baseUrl = 'https://$savedValue/api/v1'; |
||||||
|
} |
||||||
|
|
||||||
|
return baseUrl; |
||||||
} else { |
} else { |
||||||
// If it doesn't match both patterns, assume it's an IP without port |
// Handle jika tidak ada data di database (misalnya, berikan nilai default atau tampilkan pesan kesalahan) |
||||||
baseUrl = 'https://$savedValue/api/v1'; |
print('No data found in database.'); |
||||||
|
return ''; // Atau berikan nilai default lainnya sesuai kebutuhan |
||||||
} |
} |
||||||
|
|
||||||
return baseUrl; |
|
||||||
} catch (e) { |
} catch (e) { |
||||||
// Error handling |
// Handle kesalahan pembacaan dari database |
||||||
print('Error reading SharedPreferences: $e'); |
print('Error reading data from database: $e'); |
||||||
return ''; // Or any other default value in case of an error |
return ''; // Atau berikan nilai default lainnya sesuai kebutuhan |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -0,0 +1,18 @@ |
|||||||
|
class IpDomain { |
||||||
|
int? id; |
||||||
|
String? ipOrDomain; |
||||||
|
|
||||||
|
IpDomain({this.id, this.ipOrDomain}); |
||||||
|
|
||||||
|
factory IpDomain.fromJson(Map<String, dynamic> json) { |
||||||
|
return IpDomain( |
||||||
|
id: json['id'] ? json['id'] : 0, |
||||||
|
ipOrDomain: json['ipOrDomain'] ? json['ipOrDomain'] : '', |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => { |
||||||
|
'id': id, |
||||||
|
'ipOrDomain': ipOrDomain, |
||||||
|
}; |
||||||
|
} |
@ -0,0 +1,101 @@ |
|||||||
|
import 'package:sqflite/sqflite.dart'; |
||||||
|
|
||||||
|
import '../../../migrations/databasehelper.dart'; |
||||||
|
import '../../../models/ip_domain_model.dart'; |
||||||
|
|
||||||
|
class ControllerLogin { |
||||||
|
final conn = SqfliteDatabaseHelper.instance; |
||||||
|
|
||||||
|
Future<int> addData(IpDomain ipDomain) async { |
||||||
|
var dbclient = await conn.db; |
||||||
|
int result = 0; // Provide an initial value |
||||||
|
try { |
||||||
|
result = await dbclient! |
||||||
|
.insert(SqfliteDatabaseHelper.ipDomainTable, ipDomain.toJson()); |
||||||
|
} catch (e) { |
||||||
|
print(e.toString()); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
Future<int> updateData(IpDomain ipDomain) async { |
||||||
|
var dbclient = await conn.db; |
||||||
|
late int result; |
||||||
|
try { |
||||||
|
result = await dbclient!.update( |
||||||
|
SqfliteDatabaseHelper.ipDomainTable, |
||||||
|
ipDomain.toJson(), |
||||||
|
where: 'id=?', |
||||||
|
whereArgs: [ipDomain.id], |
||||||
|
); |
||||||
|
} catch (e) { |
||||||
|
print(e.toString()); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
Future<List> fetchData() async { |
||||||
|
var dbclient = await conn.db; |
||||||
|
List userList = []; |
||||||
|
try { |
||||||
|
List<Map<String, dynamic>> maps = await dbclient! |
||||||
|
.query(SqfliteDatabaseHelper.ipDomainTable, orderBy: 'id DESC'); |
||||||
|
for (var item in maps) { |
||||||
|
userList.add(item); |
||||||
|
} |
||||||
|
} catch (e) { |
||||||
|
print(e.toString()); |
||||||
|
} |
||||||
|
return userList; |
||||||
|
} |
||||||
|
|
||||||
|
Future<List<IpDomain>> fetchDataId() async { |
||||||
|
var dbclient = await conn.db; |
||||||
|
List<IpDomain> ipDomainList = []; |
||||||
|
try { |
||||||
|
List<Map<String, dynamic>> maps = await dbclient! |
||||||
|
.query(SqfliteDatabaseHelper.ipDomainTable, orderBy: 'id DESC'); |
||||||
|
for (var item in maps) { |
||||||
|
ipDomainList.add(IpDomain.fromJson(item)); |
||||||
|
} |
||||||
|
} catch (e) { |
||||||
|
print(e.toString()); |
||||||
|
} |
||||||
|
return ipDomainList; |
||||||
|
} |
||||||
|
|
||||||
|
// Future<void> deleteAllData() async { |
||||||
|
// var dbClient = await conn.db; |
||||||
|
// await dbClient!.delete(SqfliteDatabaseHelper.ipDomainTable); |
||||||
|
// } |
||||||
|
|
||||||
|
Future<void> resetAutoIncrement() async { |
||||||
|
var dbClient = await conn.db; |
||||||
|
await dbClient!.execute( |
||||||
|
'DELETE FROM SQLITE_SEQUENCE WHERE NAME = "${SqfliteDatabaseHelper.ipDomainTable}"'); |
||||||
|
} |
||||||
|
|
||||||
|
Future<void> deleteAllData() async { |
||||||
|
var dbClient = await conn.db; |
||||||
|
|
||||||
|
// Hapus semua data dari tabel |
||||||
|
await dbClient!.delete(SqfliteDatabaseHelper.ipDomainTable); |
||||||
|
|
||||||
|
// Reset auto-increment to 1 |
||||||
|
await resetAutoIncrement(); |
||||||
|
} |
||||||
|
|
||||||
|
Future<void> addAllData(List<IpDomain> ipDomainList) async { |
||||||
|
var dbclient = await conn.db; |
||||||
|
Batch batch = dbclient!.batch(); |
||||||
|
|
||||||
|
for (var ipDomain in ipDomainList) { |
||||||
|
batch.insert( |
||||||
|
SqfliteDatabaseHelper.ipDomainTable, |
||||||
|
ipDomain.toJson(), |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
await batch.commit(); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue