getInternalState<T> method

Future<T?> getInternalState<T>(
  1. String key
)

Obtiene estado interno

Implementation

Future<T?> getInternalState<T>(String key) async {
  ObslyLogger.debug('πŸ” ObslyStorage.getInternalState<$T>() called - key: $key, initialized: $_isInitialized');

  if (!_isInitialized) {
    ObslyLogger.verbose('πŸ” Storage not initialized, calling initialize()...');
    await initialize();
  }

  try {
    ObslyLogger.debug(
        'πŸ” Storage state: useHive=$_useHive, internalStateBox=${_internalStateBox != null}, prefs=${_prefs != null}');

    if (_useHive && _internalStateBox != null) {
      ObslyLogger.verbose('πŸ” Reading from Hive box...');
      final rawResult = _internalStateBox!.get(key);
      ObslyLogger.debug(
          'πŸ” Hive raw result for "$key": ${rawResult?.runtimeType} - ${rawResult != null ? rawResult.toString() : 'NULL'}');

      if (rawResult == null) {
        return null;
      }

      // Manejar conversiΓ³n de tipos para Maps (preservado de HEAD)
      if (rawResult is Map && T.toString().contains('Map<String, dynamic>')) {
        ObslyLogger.debug('πŸ” Converting Map to Map<String, dynamic> for "$key"');
        return Map<String, dynamic>.from(rawResult) as T;
      }

      // Safe type conversion (de develop)
      try {
        if (rawResult is T) {
          ObslyLogger.debug('πŸ” Direct type match for "$key"');
          return rawResult;
        } else {
          ObslyLogger.debug('πŸ” Type mismatch for "$key": expected $T, got ${rawResult.runtimeType}');
          return null;
        }
      } catch (e) {
        ObslyLogger.error('πŸ” Type conversion error for "$key": $e');
        return null;
      }
    } else if (_prefs != null) {
      ObslyLogger.verbose('πŸ” Reading from SharedPreferences...');
      final typeString = T.toString(); // Necesario para el manejo de Maps mΓ‘s abajo

      if (T == String) {
        final result = _prefs!.getString('obsly_state_$key') as T?;
        ObslyLogger.debug(
            'πŸ” SharedPreferences string result for "$key": ${result != null ? 'FOUND (${result.toString().length} chars)' : 'NULL'}');
        return result;
      } else if (T == int) {
        try {
          // DEFENSIVE: Check actual stored type to prevent crashes from corrupted data
          final anyValue = _prefs!.get('obsly_state_$key');

          if (anyValue == null) {
            ObslyLogger.debug('πŸ” SharedPreferences int result for "$key": NULL');
            return null;
          }

          if (anyValue is int) {
            ObslyLogger.debug('πŸ” SharedPreferences int result for "$key": $anyValue');
            return anyValue as T;
          }

          // Corrupted data - log and return null
          ObslyLogger.error('❌ Corrupted data for key "$key": expected int, found ${anyValue.runtimeType}');
          return null;
        } catch (e) {
          ObslyLogger.error('❌ Error getting internal state for key "$key": $e');
          return null;
        }
      } else if (T == bool) {
        try {
          // DEFENSIVE: Check actual stored type to prevent crashes from corrupted data
          final anyValue = _prefs!.get('obsly_state_$key');

          if (anyValue == null) {
            ObslyLogger.debug('πŸ” SharedPreferences bool result for "$key": NULL');
            return null;
          }

          if (anyValue is bool) {
            ObslyLogger.debug('πŸ” SharedPreferences bool result for "$key": $anyValue');
            return anyValue as T;
          }

          // Corrupted data - log and return null
          ObslyLogger.error('❌ Corrupted data for key "$key": expected bool, found ${anyValue.runtimeType}');
          return null;
        } catch (e) {
          ObslyLogger.error('❌ Error getting internal state for key "$key": $e');
          return null;
        }
      } else {
        final json = _prefs!.getString('obsly_state_$key');
        ObslyLogger.debug(
            'πŸ” SharedPreferences JSON for "$key": ${json != null ? 'FOUND (${json.length} chars)' : 'NULL'}');
        if (json != null) {
          final decoded = jsonDecode(json);
          // Manejar el caso especΓ­fico de Map<String, dynamic> tambiΓ©n para SharedPreferences
          if (typeString.contains('Map<String, dynamic>') && decoded is Map) {
            return Map<String, dynamic>.from(decoded) as T;
          }
          return decoded as T?;
        }
      }
    } else {
      ObslyLogger.error('❌ No storage backend available for reading!');
    }

    ObslyLogger.debug('πŸ” Returning null for key "$key"');
    return null;
  } catch (e) {
    ObslyLogger.error('❌ Error getting internal state for key "$key": $e');
    return null;
  }
}