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(); // } // ini sudah benar // Future addAllPetiDataAPI( // List petiListApi, int chunkSize) async { // var dbclient = await conn.db; // // Mulai batch // Batch batch = dbclient!.batch(); // // Iterasi melalui data dalam chunk // for (int i = 0; i < petiListApi.length; i += chunkSize) { // // Ambil chunk dari data // List chunk = petiListApi.skip(i).take(chunkSize).toList(); // // Insert setiap item dalam chunk ke dalam batch // for (var peti in chunk) { // batch.insert( // SqfliteDatabaseHelper.petiTable, // peti.toJson(), // ); // } // } // // Commit batch // await batch.commit(); // } // Future addAllPetiDataAPI(List petiListApi) async { // var dbclient = await conn.db; // Batch batch = dbclient!.batch(); // // Tentukan ukuran chunk yang diinginkan // int chunkSize = 100; // // Iterasi melalui data dalam chunk // for (int i = 0; i < petiListApi.length; i += chunkSize) { // // Ambil chunk dari data // List chunk = petiListApi.skip(i).take(chunkSize).toList(); // // Insert setiap item dalam chunk ke dalam batch // for (var peti in chunk) { // batch.insert( // SqfliteDatabaseHelper.petiTable, // peti.toJson(), // ); // } // // Commit batch setelah menambahkan semua item dalam chunk // await batch.commit(); // } // } Future addAllPetiDataAPI( List petiListApi, int chunkSize) async { try { String? formatDateTime(DateTime? dateTime) { return dateTime ?.toIso8601String(); // Ubah DateTime ke dalam format ISO 8601 } var dbclient = await conn?.db; if (dbclient == null) { print("Error: Database connection is null"); return; } await dbclient.transaction((txn) async { Batch batch = txn.batch(); // Iterasi melalui data dalam chunk for (int i = 0; i < petiListApi.length; i += chunkSize) { List chunk = petiListApi.skip(i).take(chunkSize).toList(); for (var peti in chunk) { // Hanya ambil kolom yang diinginkan var petiData = { 'id': peti.id, 'fix_lot': peti.fix_lot, 'status': peti.status, 'customer_id': peti.customer_id, 'warehouse_id': peti.warehouse_id, 'kondisipeti_id': peti.kondisipeti_id, 'deleted_at': formatDateTime(peti.deleted_at), }; batch.insert( SqfliteDatabaseHelper.petiTable, petiData, ); } } await batch.commit(); }); } catch (e) { print("Error during addAllPetiDataAPI: $e"); // Handle error sesuai kebutuhan Anda } } // 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 ------------------------------------------------------------------------------------------------------------------ }