Untitled

mail@pastecode.io avatar
unknown
dart
2 years ago
4.6 kB
1
Indexable
Never
import 'package:aicycle_insurance/common/utils/loading/loading_service.dart';
import 'package:aicycle_insurance/common/utils/snack_bar/snack_bar_helper.dart';
import 'package:aicycle_insurance/di/di_setup.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:aicycle_insurance/common/local_data/index.dart';

import 'bloc/index.dart';

abstract class BaseState<W extends StatefulWidget, E, S extends BaseBlocState,
        B extends BaseBloc<E, S>> extends State<W>
    with BaseMethodMixin<S>, AutomaticKeepAliveClientMixin {
  late B bloc;
  final localPref = getIt<LocalStorage>();
  // late StreamSubscription connectivitySub;

  B provideBloc(BuildContext context) {
    return getIt.get<B>();
  }

  void initBloc(BuildContext context) {
    bloc = provideBloc(context);
  }

  Widget blocBuilder({
    required Widget Function(BuildContext c, S) builder,
    bool Function(S, S)? buildWhen,
  }) {
    return BlocBuilder<B, S>(
      bloc: bloc,
      buildWhen: buildWhen,
      builder: builder,
    );
  }

  @override
  bool get wantKeepAlive => wannaKeepAlive;

  @override
  void initState() {
    // bloc = getIt.get<B>();
    initBloc(context);

    /*
    FirebaseDynamicLinks.instance.onLink.listen((dynamicLinkData) {
      if (!mounted) return;
      context.pushNamed(
        Routes.forgotPasswordDynamicLink,
        params: {
          RouterParamConstants.deepLink: dynamicLinkData.link.toString()
        },
      );
    }).onError((error) {
      //DO NOTHING
      injector<LogUtils>().logD(error.toString());
    });
    */

    super.initState();
  }

  @override
  void dispose() {
    bloc.close();
    // connectivitySub.cancel();
    // LoadingService().hideLoading();
    super.dispose();
  }

  @required
  Widget renderUI(BuildContext context);

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BlocProvider(
      create: (context) => bloc,
      child: BlocListener<B, S>(
        listenWhen: listenWhen,
        listener: listener,
        child: renderUI(context),
      ),
    );
  }
}

abstract class BaseShareState<
    W extends StatefulWidget,
    E,
    S extends BaseBlocState,
    B extends BaseBloc<E, S>> extends State<W> with BaseMethodMixin<S> {
  late B bloc;

  B provideBloc() {
    return getIt.get<B>();
  }

  void initBloc() {
    bloc = provideBloc();
  }

  @override
  void initState() {
    initBloc();
    /*
    FirebaseDynamicLinks.instance.onLink.listen((dynamicLinkData) {
      if (!mounted) return;
      context.pushNamed(
        Routes.forgotPasswordDynamicLink,
        params: {
          RouterParamConstants.deepLink: dynamicLinkData.link.toString()
        },
      );
    }).onError((error) {
      //DO NOTHING
      injector<LogUtils>().logD(error.toString());
    });
    */
    super.initState();
  }

  Widget blocBuilder({
    required Widget Function(BuildContext c, S) builder,
    bool Function(S, S)? buildWhen,
  }) {
    return BlocBuilder<B, S>(
      bloc: bloc,
      buildWhen: buildWhen,
      builder: builder,
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  @required
  Widget renderUI(BuildContext context);

  @override
  Widget build(BuildContext context) {
    return BlocProvider.value(
      value: bloc,
      child: BlocListener<B, S>(
        listenWhen: listenWhen,
        listener: listener,
        child: renderUI(context),
      ),
    );
  }
}

mixin BaseMethodMixin<S extends BaseBlocState> {
  void showMessage(
    BuildContext context,
    String message, {
    type = SnackBarType.success,
  }) {
    final SnackBarHelper helper = getIt<SnackBarHelper>();
    switch (type) {
      case SnackBarType.error:
        helper.showError(context, message);
        break;
      case SnackBarType.info:
        helper.showInfo(context, message);
        break;
      default:
        helper.showSuccess(context, message);
        break;
    }
  }

  bool wannaKeepAlive = true;

  bool listenWhen(S previous, S current) {
    return previous != current;
  }

  void listener(BuildContext context, S state) {
    if (state.status == BaseStateStatus.loading) {
      getIt<LoadingService>().showLoading(context);
    } else {
      getIt<LoadingService>().hideLoading();
      if (state.status == BaseStateStatus.failed) {
        showMessage(context, state.message ?? "", type: SnackBarType.error);
      } else if (state.status == BaseStateStatus.success &&
          (state.message ?? "").isNotEmpty) {
        showMessage(context, state.message ?? "", type: SnackBarType.success);
      }
    }
  }
}