fetchFlags method

  1. @override
Future<FetchResponse?> fetchFlags()
override

Implementation

@override
Future<FetchResponse?> fetchFlags() async {
  Flagship.logger(Level.ALL, SYNCHRONIZE_MODIFICATIONS);
  // get actual state flagship sdk
  FSSdkStatus state = Flagship.getStatus();
  DataUsageTracking.sharedInstance().processDataUsageTracking(visitor);
  try {
    var camp = await visitor.decisionManager.getCampaigns(
        Flagship.sharedInstance().envId ?? "",
        visitor.visitorId,
        visitor.anonymousId,
        visitor.getConsent(),
        visitor.getContext());
    // Clear the previous modifications
    visitor.modifications.clear();
    // Update panic value
    visitor.decisionManager.updatePanicMode(camp.panic);
    if (camp.panic) {
      state = FSSdkStatus.SDK_PANIC;
      // Stop batching loop when the panic mode is ON
      visitor.trackingManager?.stopBatchingLoop();
    } else {
      state = FSSdkStatus.SDK_INITIALIZED;
      var modif = visitor.decisionManager.getModifications(camp.campaigns);
      visitor.modifications.addAll(modif);
      // Start Batching loop
      visitor.trackingManager?.startBatchingLoop();
      Flagship.logger(
          Level.INFO,
          SYNCHRONIZE_MODIFICATIONS_RESULTS.replaceFirst(
              "%s", "${visitor.modifications.keys}"));
    }
    // Update the state for Flagship
    visitor.flagshipDelegate.onUpdateState(state);

    // Save the response for the visitor database
    cacheVisitor(visitor.visitorId,
        jsonEncode(VisitorCache.fromVisitor(this.visitor).toJson()));
    // Update the dataUsage tracking
    visitor.dataUsageTracking
        .updateTroubleshooting(camp.accountSettings?.troubleshooting);
    // Notify the data report
    DataUsageTracking.sharedInstance().processTSFetching(this.visitor);

    return FetchResponse(
        camp.panic ? FlagStatus.PANIC : FlagStatus.FETCHED, null);
    // return null;
  } catch (error) {
    // Report the error
    Flagship.logger(Level.EXCEPTIONS,
        EXCEPTION.replaceFirst("%s", "${error.toString()}"));
    DataUsageTracking.sharedInstance()
        .processTroubleShootingException(visitor, error);
    return FetchResponse(FlagStatus.FETCH_REQUIRED, Error());
  }
}