mirrored_repositories/FlutterCryptoFreebie/lib
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

class ThemeService {
  final _box = GetStorage();
  final _key = 'isDarkMode';

  ThemeService._() {
    setStatusStyle();
    _box.listenKey(_key, (value) => setStatusStyle());
  }

  static late final ThemeService instance = ThemeService._();

  /// Get isDarkMode info from local storage and return ThemeMode
  ThemeMode get theme => isDarkMode() ? ThemeMode.dark : ThemeMode.light;

  /// Load isDArkMode from local storage and if it's empty, returns false (that means default theme is light)
  bool isDarkMode() => _box.read(_key) ?? false;

  /// Save isDarkMode to local storage
  _saveThemeToBox(bool isDarkMode) => _box.write(_key, isDarkMode);

  /// Switch theme and save to local storage
  switchTheme() {
    Get.changeThemeMode(isDarkMode() ? ThemeMode.light : ThemeMode.dark);
    _saveThemeToBox(!isDarkMode());
  }

  setStatusStyle() {
    SystemChrome.setSystemUIOverlayStyle(
        isDarkMode() ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark);
  }
}
