injectI18N<T> method Null safety

InjectedI18N<T> injectI18N<T>(
  1. Map<Locale, FutureOr<T> Function()> i18Ns,
  2. {String? persistKey,
  3. SnapState<T>? stateInterceptor(
    1. SnapState<T> currentSnap,
    2. SnapState<T> nextSnap
    )?,
  4. SideEffects<T>? sideEffects,
  5. DependsOn<T>? dependsOn,
  6. int undoStackLength = 0,
  7. String? debugPrintWhenNotifiedPreMessage,
  8. @Deprecated('Use stateInterceptor instead') SnapState<T>? middleSnapState(
    1. MiddleSnapState<T> middleSnap
    )?,
  9. @Deprecated('Use sideEffects instead') void onInitialized(
    1. T? s
    )?,
  10. @Deprecated('Use sideEffects instead') void onDisposed(
    1. T s
    )?,
  11. @Deprecated('Use sideEffects instead') On<void>? onSetState}
)

Injection of a state that handle app internationalization and localization.

This injected state abstracts the best practices of the clean architecture to come out with a simple, clean, and testable approach to manage app localization and internationalization.

The approach consists of the following steps:

  • //TODO

Parameters:

i18Ns: Required Map<T, FutureOr<T> Function()>

Map of supported locales with their language translation

persistKey: Optional String

If defined the app language is persisted to a local storage. The persisted language will be used on app restarting.

You have to provide a class that implements IPersistStore and initialize it in the main method.

For example

class IPersistStoreImp implements IPersistStore{
 // ....
}
void main()async{
 WidgetsFlutterBinding.ensureInitialized();

 await RM.storageInitializer(IPersistStoreImp());
 runApp(MyApp());
}

stateInterceptor: Optional callback that exposes the current and

next SnapState This call back is fired after on state mutation (singed user change) and exposes both the current state just before mutation and the next state.

undoStackLength: Optional integer

It defines the length of the undo/redo stack. If not defined, the undo/redo is disabled.

For the undo/redo state to work properly, the state must be immutable.

Further on, to undo or redo the state just call Injected.undoState and Injected.redoState

sideEffects: Optional SideEffects

Used to handle sideEffects when the state is initialized, mutated and disposed of.

dependsOn: optional DependsOn

Use to defined other injected states that this state depends on. When any of states it depends on is notified, this state is also notified and its creator is re-invoked. The state status will reflect a combination of the state status of dependencies:

  • If any of dependency state isWaiting, this state isWaiting.
  • If any of dependency state hasError, this state hasError.
  • If any of dependency state isIdle, this state isIdle.
  • If all dependency states have data, this state hasData.

debugPrintWhenNotifiedPreMessage: Optional String

if not null, print an informative message when this model is notified in the debug mode. It prints (FROM ==> TO state). The entered message will pré-append the debug message. Useful if the type of the injected model is primitive to distinguish between them.

Implementation

static InjectedI18N<T> injectI18N<T>(
  Map<Locale, FutureOr<T> Function()> i18Ns, {
  String? persistKey,
  //
  SnapState<T>? Function(
    SnapState<T> currentSnap,
    SnapState<T> nextSnap,
  )?
      stateInterceptor,
  SideEffects<T>? sideEffects,
  //
  DependsOn<T>? dependsOn,
  int undoStackLength = 0,
  //
  // bool isLazy = true,
  String? debugPrintWhenNotifiedPreMessage,
  @Deprecated('Use stateInterceptor instead')
      SnapState<T>? Function(MiddleSnapState<T> middleSnap)? middleSnapState,
  @Deprecated('Use sideEffects instead') void Function(T? s)? onInitialized,
  @Deprecated('Use sideEffects instead') void Function(T s)? onDisposed,
  @Deprecated('Use sideEffects instead') On<void>? onSetState,
}) {
  assert(
    T != dynamic && T != Object,
    'Type can not inferred, please declare it explicitly',
  );
  late final InjectedI18NImp<T> inj;
  return inj = InjectedI18NImp<T>(
    i18Ns: i18Ns,
    persistKey: persistKey,
    //
    middleSnapState: stateInterceptor != null
        ? (middleSnap) => stateInterceptor(
              middleSnap.currentSnap,
              middleSnap.nextSnap,
            )
        : middleSnapState,
    onInitialized: sideEffects?.initState != null
        ? (_) => sideEffects!.initState!()
        : onInitialized,
    onDisposed: sideEffects?.dispose != null
        ? (_) => sideEffects!.dispose!()
        : onDisposed,
    onSetState: On(
      () {
        if (sideEffects?.onSetState != null) {
          sideEffects!.onSetState!(inj.snapState);
        } else {
          onSetState?.call(inj.snapState);
        }
        sideEffects?.onAfterBuild?.call();
      },
    ),
    //
    dependsOn: dependsOn,
    undoStackLength: undoStackLength,
    //
    // isLazy: isLazy,
    debugPrintWhenNotifiedPreMessage: debugPrintWhenNotifiedPreMessage,
  );
}