import 'dart:async'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:siopas/models/asset_status_model.dart'; import 'package:siopas/models/customer_model.dart'; import 'package:siopas/models/disposal_model.dart'; import 'package:siopas/models/m_asset_status_model.dart'; import 'package:siopas/models/warehouse_mode.dart'; import 'package:http/http.dart' as http; import 'package:internet_connection_checker/internet_connection_checker.dart'; import 'package:sqflite/sqflite.dart'; import '../migrations/databasehelper.dart'; import '../models/condition_peti_model.dart'; import '../models/type_peti_model.dart'; class ControllerApi { final conn = SqfliteDatabaseHelper.instance; static Future isInternetApi() async { var connectivityResult = await (Connectivity().checkConnectivity()); if (connectivityResult == ConnectivityResult.mobile) { if (await InternetConnectionChecker().hasConnection) { print("Mobile data detected & internet connection confirmed."); return true; } else { print('No internet :( Reason:'); return false; } } else if (connectivityResult == ConnectivityResult.wifi) { if (await InternetConnectionChecker().hasConnection) { print("wifi data detected & internet connection confirmed."); return true; } else { print('No internet :( Reason:'); return false; } } else { print( "Neither mobile data or WIFI detected, not internet connection found."); return false; } } // Asset Status ------------------------------------------------------------------------------------------------------------------ Future fetchAssetStatusLocalControllerApi() async { var dbclient = await conn.db; List assetStatusApiList = []; try { // Ensure that the table name is correct List> maps = await dbclient! .query(SqfliteDatabaseHelper.asset_statusesTable, orderBy: 'id DESC'); for (var item in maps) { assetStatusApiList.add(item); } } catch (e) { print('Error fetching data from SQLite: $e'); } return assetStatusApiList; } Future deleteAllAssetStatusDataAPI() async { var dbClient = await conn.db; await dbClient!.delete(SqfliteDatabaseHelper.asset_statusesTable); } Future addAllAssetStatusDataAPI( List assetStatusListApi) async { var dbclient = await conn.db; Batch batch = dbclient!.batch(); for (var assetStatus in assetStatusListApi) { // Ensure that toJson() correctly converts the model to a map batch.insert( SqfliteDatabaseHelper.asset_statusesTable, assetStatus.toJson(), ); } await batch.commit(); } // End Asset Status ------------------------------------------------------------------------------------------------------------------ // Peti ------------------------------------------------------------------------------------------------------------------ Future fetchPetiDataAPI() async { var dbclient = await conn.db; List petiApiList = []; try { // Ensure that the table name is correct List> maps = await dbclient! .query(SqfliteDatabaseHelper.petiTable, orderBy: 'id DESC'); for (var item in maps) { petiApiList.add(item); } } catch (e) { print('Error fetching data from SQLite: $e'); } return petiApiList; } Future deleteAllPetiDataAPI() async { var dbClient = await conn.db; await dbClient!.delete(SqfliteDatabaseHelper.petiTable); } Future addAllPetiDataAPI(List petiListApi) async { var dbclient = await conn.db; Batch batch = dbclient!.batch(); for (var peti in petiListApi) { // Ensure that toJson() correctly converts the model to a map batch.insert( SqfliteDatabaseHelper.petiTable, peti.toJson(), ); } await batch.commit(); } // End Peti ------------------------------------------------------------------------------------------------------------------ // Warehouse ------------------------------------------------------------------------------------------------------------------ Future fetchWarehouseDataAPI() async { var dbclient = await conn.db; List warehouseApiList = []; try { // Ensure that the table name is correct List> maps = await dbclient! .query(SqfliteDatabaseHelper.warehouseTable, orderBy: 'id DESC'); for (var item in maps) { warehouseApiList.add(item); } } catch (e) { print('Error fetching data from SQLite: $e'); } return warehouseApiList; } Future deleteAllWarehouseDataAPI() async { var dbClient = await conn.db; await dbClient!.delete(SqfliteDatabaseHelper.warehouseTable); } Future addAllWarehouseDataAPI( List warehouseListApi) async { var dbclient = await conn.db; Batch batch = dbclient!.batch(); for (var warehouse in warehouseListApi) { // Ensure that toJson() correctly converts the model to a map batch.insert( SqfliteDatabaseHelper.warehouseTable, warehouse.toJson(), ); } await batch.commit(); } // End Warehouse ------------------------------------------------------------------------------------------------------------------ // Customer ------------------------------------------------------------------------------------------------------------------ Future fetchCustomerDataAPI() async { var dbclient = await conn.db; List customerApiList = []; try { // Ensure that the table name is correct List> maps = await dbclient! .query(SqfliteDatabaseHelper.customerTable, orderBy: 'id DESC'); for (var item in maps) { customerApiList.add(item); } } catch (e) { print('Error fetching data from SQLite: $e'); } return customerApiList; } Future deleteAllCustomerDataAPI() async { var dbClient = await conn.db; await dbClient!.delete(SqfliteDatabaseHelper.customerTable); } Future addAllCustomerDataAPI( List customerApiList) async { var dbclient = await conn.db; Batch batch = dbclient!.batch(); for (var customer in customerApiList) { // Ensure that toJson() correctly converts the model to a map batch.insert( SqfliteDatabaseHelper.customerTable, customer.toJson(), ); } await batch.commit(); } // End Customer ------------------------------------------------------------------------------------------------------------------ // Tipe Peti ------------------------------------------------------------------------------------------------------------------ Future fetchTipePetiDataAPI() async { var dbclient = await conn.db; List tipePetiApiList = []; try { // Ensure that the table name is correct List> maps = await dbclient! .query(SqfliteDatabaseHelper.typePetiTable, orderBy: 'id DESC'); for (var item in maps) { tipePetiApiList.add(item); } } catch (e) { print('Error fetching data from SQLite: $e'); } return tipePetiApiList; } Future deleteAllTipePetiDataAPI() async { var dbClient = await conn.db; await dbClient!.delete(SqfliteDatabaseHelper.typePetiTable); } Future addAllTipePetiDataAPI( List tipePetiApiList) async { var dbclient = await conn.db; Batch batch = dbclient!.batch(); for (var tipePeti in tipePetiApiList) { // Ensure that toJson() correctly converts the model to a map batch.insert( SqfliteDatabaseHelper.typePetiTable, tipePeti.toJson(), ); } await batch.commit(); } // End Tipe Peti ------------------------------------------------------------------------------------------------------------------ // Tipe Kondisi ------------------------------------------------------------------------------------------------------------------ // Future fetchKondisiPetiDataAPI() async { // var dbclient = await conn.db; // List kondisiPetiApiList = []; // try { // // Ensure that the table name is correct // List> maps = await dbclient! // .query(SqfliteDatabaseHelper.conditionPetiTable, orderBy: 'id DESC'); // for (var item in maps) { // kondisiPetiApiList.add(item); // } // } catch (e) { // print('Error fetching data from SQLite: $e'); // } // return kondisiPetiApiList; // } Future fetchKondisiPetiDataAPI() async { var dbclient = await conn.db; List conditionApiList = []; try { // Ensure that the table name is correct List> maps = await dbclient! .query(SqfliteDatabaseHelper.conditionPetiTable, orderBy: 'id DESC'); for (var item in maps) { conditionApiList.add(item); } } catch (e) { print('Error fetching data from SQLite: $e'); } return conditionApiList; } Future deleteAllKondisiPetiDataAPI() async { var dbClient = await conn.db; await dbClient!.delete(SqfliteDatabaseHelper.conditionPetiTable); } Future addAllKondisiPetiDataAPI( List kondisiPetiApiList) async { var dbclient = await conn.db; Batch batch = dbclient!.batch(); for (var kondisiPeti in kondisiPetiApiList) { // Ensure that toJson() correctly converts the model to a map batch.insert( SqfliteDatabaseHelper.conditionPetiTable, kondisiPeti.toJson(), ); } await batch.commit(); } // End Kondisi Peti ------------------------------------------------------------------------------------------------------------------ // Transfer Kondisi ------------------------------------------------------------------------------------------------------------------ Future fetchTransferPetiDataAPI() async { var dbclient = await conn.db; List transferPetiApiList = []; try { // Ensure that the table name is correct List> maps = await dbclient! .query(SqfliteDatabaseHelper.transferPetiTable, orderBy: 'id DESC'); for (var item in maps) { transferPetiApiList.add(item); } } catch (e) { print('Error fetching data from SQLite: $e'); } return transferPetiApiList; } Future deleteAllTransferPetiDataAPI() async { var dbClient = await conn.db; await dbClient!.delete(SqfliteDatabaseHelper.transferPetiTable); } Future addAllTransferPetiDataAPI( List transferPetiApiList) async { var dbclient = await conn.db; Batch batch = dbclient!.batch(); for (var transferPeti in transferPetiApiList) { // Ensure that toJson() correctly converts the model to a map batch.insert( SqfliteDatabaseHelper.transferPetiTable, transferPeti.toJson(), ); } await batch.commit(); } // End Transfer Peti ------------------------------------------------------------------------------------------------------------------ // Disposal ------------------------------------------------------------------------------------------------------------------ Future fetchDisposalDataAPI() async { var dbclient = await conn.db; List disposalApiList = []; try { // Ensure that the table name is correct List> maps = await dbclient! .query(SqfliteDatabaseHelper.disposalTable, orderBy: 'id DESC'); for (var item in maps) { disposalApiList.add(item); } } catch (e) { print('Error fetching data from SQLite: $e'); } return disposalApiList; } Future deleteAllDisposalDataAPI() async { var dbClient = await conn.db; await dbClient!.delete(SqfliteDatabaseHelper.disposalTable); } Future addAllDisposalDataAPI( List disposalApiList) async { var dbclient = await conn.db; Batch batch = dbclient!.batch(); for (var disposal in disposalApiList) { // Ensure that toJson() correctly converts the model to a map batch.insert( SqfliteDatabaseHelper.disposalTable, disposal.toJson(), ); } await batch.commit(); } // End Disposal ------------------------------------------------------------------------------------------------------------------ }