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 'package:shared_preferences/shared_preferences.dart'; |
||||
import '../pages/sign_in_page.dart'; |
||||
|
||||
import 'package:path_provider/path_provider.dart'; |
||||
import '../pages/login/controller/loginController.dart'; // Sesuaikan path dengan struktur proyek Anda |
||||
|
||||
Future<String> getBaseUrl() async { |
||||
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 |
||||
String baseUrl; |
||||
// Check if savedValue is a domain, IP with or without port |
||||
String baseUrl; |
||||
|
||||
if (savedValue.contains(':')) { |
||||
// If savedValue contains a colon, assume it's an IP with port |
||||
baseUrl = 'http://$savedValue/api/v1'; |
||||
} else if (RegExp(r'^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$').hasMatch(savedValue)) { |
||||
// If savedValue matches the domain pattern, assume it's a domain |
||||
baseUrl = 'https://$savedValue/api/v1'; |
||||
if (savedValue.contains(':')) { |
||||
// If savedValue contains a colon, assume it's an IP with port |
||||
baseUrl = 'http://$savedValue/api/v1'; |
||||
} else if (RegExp(r'^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$') |
||||
.hasMatch(savedValue)) { |
||||
// 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 { |
||||
// If it doesn't match both patterns, assume it's an IP without port |
||||
baseUrl = 'https://$savedValue/api/v1'; |
||||
// Handle jika tidak ada data di database (misalnya, berikan nilai default atau tampilkan pesan kesalahan) |
||||
print('No data found in database.'); |
||||
return ''; // Atau berikan nilai default lainnya sesuai kebutuhan |
||||
} |
||||
|
||||
return baseUrl; |
||||
} catch (e) { |
||||
// Error handling |
||||
print('Error reading SharedPreferences: $e'); |
||||
return ''; // Or any other default value in case of an error |
||||
// Handle kesalahan pembacaan dari database |
||||
print('Error reading data from database: $e'); |
||||
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