getDataRepo method

Future<ResponseData> getDataRepo({
  1. required String userID,
  2. required CachingStrategy cachingStrategy,
})

Implementation

Future<ResponseData> getDataRepo(
    {required String userID,
    required CachingStrategy cachingStrategy}) async {
  final info = await PackageInfo.fromPlatform();
  final packagename = info.packageName;
  try {
    if (cachingStrategy == CachingStrategy.None) {
      //Network
      final data = await getDataNetwork(userID, packagename);
      await setDataSavedTimeSP();
      await setDataSP(data);
      return data;
    }

    final lastSaved = await getDataSavedTimeSP();
    if (lastSaved == null) {
      //Network
      final data = await getDataNetwork(userID, packagename);
      await setDataSavedTimeSP();
      await setDataSP(data);
      return data;
    }

    final lastDay = DateTime.now().add(cachingStrategy.getDuration());
    if ((lastDay.day - lastSaved.day) > cachingStrategy.getValue()) {
      //Network
      final data = await getDataNetwork(userID, packagename);
      await setDataSavedTimeSP();
      await setDataSP(data);
      return data;
    }

    return await getDataSP();
  } catch (e) {
    rethrow;
  }
}