Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirai State Management #297

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

iampranabray
Copy link
Contributor

@iampranabray iampranabray commented May 6, 2024

Description

This PR is intended to kickstart the process of adding a base implementation Mirai State Management using ChangeNotifier and InheritedWidget

create mirai_sm.dart

Here we are going to extend ChangeNotifier. instances of MiraiSM gain the ability to notify listeners when their internal state changes. state of type T. This variable will hold the internal state of the MiraiSM instance.

abstract class MiraiSM<T> extends ChangeNotifier {
  T state;
  MiraiSM(this.state);
}

create mirai_Inherited_widget.dart

Overall, MiraiInheritedWidget is a wrapper around InheritedWidget that specifically works with MiraiSM instances. It allows widgets in the widget tree to access the MiraiSM instance provided by the nearest ancestor MiraiInheritedWidget. When the state managed by MiraiSM changes, widgets that depend on it will automatically rebuild.

class MiraiInheritedWidget<T> extends InheritedWidget {
   final MiraiSM<T> state;

   const MiraiInheritedWidget({
    super.key,
    required this.state,
    required Widget child,
  }) : super(child: child);

  static MiraiInheritedWidget<T>? of<T>(BuildContext context) {
    return context
        .dependOnInheritedWidgetOfExactType<MiraiInheritedWidget<T>>();
  }

  @override
  bool updateShouldNotify(MiraiInheritedWidget oldWidget) {
    return state != oldWidget.state;
  }
}

create number_sm_example.dart

NumberStateManagement is a specific implementation of MiraiSM, intended for managing state of type T. It allows you to create instances of NumberStateManagement with an initial state provided by the superclass constructor.

class NumberStateManagement<T> extends MiraiSM<T> {
  NumberStateManagement(super.state);
}

******* Increment Operation *******

extension Increment<T> on MiraiSM {
  void increment() {
    if (state is int) {
      state = (state as int) + 1 as T;
      notifyListeners();
    } else {
      throw Exception('Data type does not support increment operation');
    }
  }
}

******* Decrement Operation ********

extension Decrement<T> on MiraiSM {
  void decrement() {
    if (state is int) {
      state = (state as int) - 1 as T;
      notifyListeners();
    } else {
      throw Exception('Data type does not support decrement operation');
    }
  }
}

main.dart

  void main() {
  runApp(
    MiraiInheritedWidget<int>(
      state: NumberStateManagement(10),
      child:  MyWidget<int>(),
    ),
  );
}
class MyWidget<T> extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    **// Access the data provided by MiraiInheritedWidget**
    final myData = MiraiInheritedWidget.of<T>(context)!.state;

    return ListenableBuilder(
      builder: (context, widget) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Data: ${myData.state}'),
            ElevatedButton(
              onPressed: () {
                // Increment the data when the button is pressed
                myData.increment();
              },
              child: const Text('Increment'),
            ),
            const SizedBox(
              height: 20,
            ),
            ElevatedButton(
              onPressed: () {
                // Increment the data when the button is pressed
                myData.decrement();
              },
              child: const Text('Decrement'),
            ),
          ],
        );
      },
      listenable: myData,
    );
  }
}

Related Issues

Closes #

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Code refactor
  • Build configuration change
  • Documentation
  • Chore

@iampranabray iampranabray changed the title Mirai State Management #1 from Securrency-OSS/main Mirai State Management May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant