setUnitExtraProps static method

Set<String> setUnitExtraProps(
  1. HashMap<String, String> extraProperties,
  2. HashMap<String, TBLExtraPropertyWithLevel> unitExtraProperties,
  3. String unitType,
  4. String uniqueUnitId,
)

Set the extra properties for a unit Stores locally with unit-level tag (always overwrites page/global) Returns a set of property keys that were updated

Implementation

static Set<String> setUnitExtraProps(
    HashMap<String, String> extraProperties,
    HashMap<String, TBLExtraPropertyWithLevel> unitExtraProperties,
    String unitType,
    String uniqueUnitId) {
  final updatedKeys = <String>{};

  try {
    if (extraProperties.isEmpty) {
      return updatedKeys;
    }

    TBLLogger.log(
        "TBLPropertiesUtils | setUnitExtraProps | unitType: $unitType, unitId: $uniqueUnitId, properties: $extraProperties");

    extraProperties.forEach((propertyKey, propertyValue) {
      if (!TextUtils.isEmptyOrNull(propertyKey) && !TextUtils.isEmptyOrNull(propertyValue)) {
        try {
          // Value format e.g: {\"value\":\"asd\",\"level\":\"global\"}
          // In case decoding works it means that the property came from page level from notify units flow
          Map decodedPageOrGlobalExtraProperties = jsonDecode(propertyValue);

          TBLExtraPropertyWithLevel pageOrGlobalProperty =
              TBLExtraPropertyWithLevel.fromJson(decodedPageOrGlobalExtraProperties);
          // check if this property already already exists on unit
          final pageOrGlobalExtraPropertyExistOnUnit = unitExtraProperties[propertyKey];
          // Only update if no existing property, or existing is not unit-level
          if (pageOrGlobalExtraPropertyExistOnUnit == null ||
              pageOrGlobalExtraPropertyExistOnUnit.level != TBLExtraPropertyLevel.unit) {
            unitExtraProperties[propertyKey] = pageOrGlobalProperty;
            updatedKeys.add(propertyKey);
          }
        } catch (e) {
          // If decoding fails, treat the property as a publisher-defined unit-level property and store it with the highest priority (unit-level tag).
          unitExtraProperties[propertyKey] = TBLExtraPropertyWithLevel(
            value: propertyValue,
            level: TBLExtraPropertyLevel.unit,
          );
          updatedKeys.add(propertyKey);
        }
      }
    });

    TBLLogger.log(
        "TBLPropertiesUtils | setUnitExtraProps | Total properties: ${unitExtraProperties.length}");
  } catch (e) {
    TBLLogger.log("TBLPropertiesUtils | setUnitExtraProps | Error: $e");
  }

  return updatedKeys;
}