Untitled

mail@pastecode.io avatar
unknown
dart
7 months ago
3.8 kB
2
Indexable
Never
import 'package:erp/data/data_source/dio/dio_service.dart';
import 'package:erp/data/repositories/local/sqflite/all_tables/product_table_class.dart';
import 'package:erp/data/repositories/remote/sqflite_repo/sqflite_repo.dart';
import 'package:erp/utils/conts/api.dart';
import 'package:flutter/cupertino.dart';

import '../../data/models/base_model/api_response_model.dart';
import '../../data/repositories/local/sqflite/all_tables/client_table_class.dart';
import '../../data/repositories/local/sqflite/sqf_lite_db.dart';
import '../../utils/global/global_classes/global_classes.dart';

class SqfLiteProvider with ChangeNotifier{
  DioService dioService;
  SqfLiteRepo sqfLiteRepo;

   SqfLiteProvider({required this.dioService, required this.sqfLiteRepo});

  List<Map<String, dynamic>> tableAndApiList = [
    {
      'table_name': ClientTableClass.tableClient,
      'create_table_info': ClientTableClass.createTableClient,
      'columnList':{
        'column_one': ClientTableClass.tableClientColumnId,
        'column_two': ClientTableClass.tableClientColumnName,
      },
      'api_url': Api.supplierNameIdOnly
    },
    {
      'table_name': ProductTableClass.tableProduct,
      'create_table_info': ProductTableClass.createTableProduct,
      'columnList':{
        'column_one': ProductTableClass.tableProductColumnId,
        'column_two': ProductTableClass.tableProductColumnName,
        'column_three': ProductTableClass.tableProductColumnTest,
      },
      'api_url': Api.selectListProduct
    },
  ];

  Future<void> fetchingDataAndSetDataInLocalDb(BuildContext context)async{

    for(int i=0; i<tableAndApiList.length; i++){

      String apiEndpoint = tableAndApiList[i]["api_url"];
      await sqfLiteRepo.getAnyDataFromApi(context: context, apiEndPoint: apiEndpoint).then((value)async{
        Map<String, dynamic> map = tableAndApiList[i];
        await setDataInLocalDb(value, context, map, i);
      });

    }

  }

  Future<void> setDataInLocalDb(ApiResponseModel apiResponseModel, BuildContext context, Map<String, dynamic> tableInfo, int createFirstTable)async{
    String message = "";
    if(apiResponseModel.response == null){
      message = apiResponseModel.apiMessage.toString();
      if(context.mounted){
        OverlaySnackBar.show(context, message);
        FocusScope.of(context).unfocus();
      }
    }

    List<String> keys = tableInfo["columnList"].keys.toList();

    if(apiResponseModel.response!.statusCode == 200){
      for(int i=0; i<apiResponseModel.response!.data["results"]!.length; i++){
        Map<String, dynamic> map = {};
        map.clear();

        if(keys.length == 2){
           map = <String, dynamic>{
             tableInfo["columnList"][keys[0]] : apiResponseModel.response!.data["results"]![i]["id"],
             tableInfo["columnList"][keys[1]] : apiResponseModel.response!.data["results"]![i]["name"],
          };
        }else if(keys.length == 3){
          map = <String, dynamic>{
            tableInfo["columnList"][keys[0]] : apiResponseModel.response!.data["results"]![i]["id"],
            tableInfo["columnList"][keys[1]] : apiResponseModel.response!.data["results"]![i]["name"],
            tableInfo["columnList"][keys[2]] : "testtt",
          };
        }

        if(createFirstTable == 0){
          int num = await SqfLitDb.insertDataInTable(tableName: tableInfo["table_name"], createTableInformation: tableInfo["create_table_info"], map: map);
        }else{
          int num = await SqfLitDb.insertDataInTableWithoutBuildINFunction(tableName: tableInfo["table_name"], createTableInformation: tableInfo["create_table_info"], map: map);
        }

      }
    }

  }

}