Giter VIP home page Giter VIP logo

easy_hive's Introduction

Easy Hive

Easy Hive is wrapper of Hive database for easier & simpler usage.

Outline ๐Ÿ“‹

Features ๐ŸŽ

Easy ๐ŸฆŠ
๐Ÿ” Encryption โœ…
๐Ÿข Lazy loading โœ…
๐Ÿ”‘ Enum key support โœ…
๐ŸŽง Listenable โœ…

Installation ๐Ÿ’ป

Add easy_hive to your pubspec.yaml:

dependencies:

  easy_hive: ^1.0.1+2

Install it:

flutter pub get

Usage ๐Ÿ“–

You can either define your boxes as Singleton classes or use a service locator like get_it.

1. Define box keys ๐Ÿ”‘

enum Settings {
  key, // Use as box key. You can use a String constant instead.

  /// Other keys below...
  themeMode,
  counter,
}

1. Define a box ๐Ÿ“ฆ

import 'package:easy_hive/easy_hive.dart';

class SettingsBox extends EasyBox {
  @override
  String get boxKey => Settings.key.toString();

  /// Singleton.
  static final SettingsBox _instance = SettingsBox._();

  factory SettingsBox() => _instance;

  SettingsBox._();
}
Or to use with get_it
import 'package:easy_hive/easy_hive.dart';

class SettingsBox extends EasyBox {
  @override
  String get boxKey => Settings.key.toString();
}

2. Initialize box ๐Ÿš€

import 'package:easy_hive/easy_hive.dart';

Future<void> main() async {
  await EasyBox.initialize();

  await SettingsBox().init();

  // runApp...
}
Or to use with get_it
import 'package:easy_hive/easy_hive.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Hive.initFlutter();

  final settingsBox = SettingsBox();
  await settingsBox.init();
  GetIt.I.registerSingleton<SettingsBox>(settingsBox);

  // runApp...
}

3. Define getter & setter for your data ๐Ÿ’„

extension GeneralSettingsExtension on SettingsBox {
  ThemeMode get themeMode {
    final index = get(
      Settings.themeMode,
      defaultValue: 0,
    );
    return ThemeMode.values[index];
  }

  set themeMode(ThemeMode value) => put(Settings.themeMode, value.index);

  int get counter => get(Settings.counter, defaultValue: 0);

  set counter(int value) => put(Settings.counter, value);
}

4. Use it anywhere ๐Ÿ”ฅ

  Text(
    'You have pushed: ${SettingsBox().counter} times.',
    style: Theme.of(context).textTheme.headlineMedium,
  ),
  FilledButton(
    onPressed: () {
      SettingsBox().counter++;
    },
    child: Text('Increment'),
  ),
  FilledButton(
    onPressed: () {
      SettingsBox().themeMode = ThemeMode.dark;
    },
    child: Text('Dark Theme'),
  ),
Or to use with get_it
  Text(
    'Count: ${GetIt.I<SettingsBox>().counter}',
    style: Theme.of(context).textTheme.headlineMedium,
  ),

Advanced Usage ๐Ÿ˜ˆ

Enable encryption ๐Ÿ”

2. Add EncryptionMixin to your box class:

class SettingsBox extends EasyBox with EncryptionMixin {
  @override
  String get boxKey => Settings.key.toString();

  /// Override encryption key name (optional).
  @override
  String get encryptionKeyName => "your-own-key-name";
}

3. Follow flutter_secure_storage's guide for specific platform setup.

Enable lazy loading ๐Ÿข

1. Add LazyMixin to your box class:

class SettingsBox extends EasyBox with LazyMixin {
  @override
  String get boxKey => Settings.key.toString();
}

2. Use await to get your value:

extension GeneralSettingsExtension on SettingsBox {
  Future<ThemeMode> getThemeMode() async {
    final index = await get(
      Settings.themeMode,
      defaultValue: 0,
    );
    return ThemeMode.values[index];
  }
}

Listen to value changes ๐ŸŽง

Recommended: Use RefreshableBox + provider:

1. Extends RefreshableBox instead of EasyBox:

class SettingsBox extends RefreshableBox {
  @override
  String get boxKey => Settings.key.toString();
}

2. Use it as a provider:

  ChangeNotifierProvider(
    create: (_) => SettingsBox(),
    child: SomeWidget(),
  ),
// Inside SomeWidget.
Text(
  'You have pushed: '
  '${context.select((SettingsBox _) => _.counter)} times.',
),

For more info, see provider package.


Or if you don't want RefreshableBox:

Just use ValueListenableBuilder to listen to changes.

ValueListenableBuilder(
  valueListenable: [
    Settings.counter,
  ].of(SettingsBox()),
  builder: (context, _, __) {
    return Text(
      '${SettingsBox().counter}',
    );
  },
),

Happy Coding ๐ŸฆŠ

Made with โค๏ธ by Simon Pham

easy_hive's People

Contributors

simonpham avatar

Stargazers

Bora Cobanoglu avatar  avatar Huynh Nhu avatar  avatar

Watchers

 avatar Kostas Georgiou avatar

easy_hive's Issues

feat: support code generation

Description

As a developer, I expect to only define my keys as an Enum class, then build_runner will help me to generate the required code. For example:

settings.dart

part 'settings.g.dart';

@EasyBoxClass(
  isLazy: false,
  isEncrypted: true,
  boxKey: 'settings',
  generatedClassName: 'SettingsBox',
)
enum Settings {
  @EasyBoxField(ThemeMode, ThemeMode.system)
  themeMode,
  @EasyBoxField(int, 0)
  counter,
}

then after run flutter pub run build_runner build, I got the full implementation of SettingsBox as following easy_hive documentation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.