Skip to content

Instantly share code, notes, and snippets.

@febritecno
Last active October 10, 2022 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save febritecno/771fde6d0ebcb8f29c24ebba1afe9565 to your computer and use it in GitHub Desktop.
Save febritecno/771fde6d0ebcb8f29c24ebba1afe9565 to your computer and use it in GitHub Desktop.
Prefs Collection
// prefs_secure, rx_prefs, prefs
import 'package:flutter/foundation.dart';
class Prefs {
static SharedPreferences? _prefs;
// call this method from iniState() function of mainApp().
static Future<SharedPreferences?> init() async {
_prefs = await SharedPreferences.getInstance();
return _prefs;
}
//sets
static Future<bool> setBool(String key, bool value) async =>
await _prefs!.setBool(key, value);
static Future<bool> setDouble(String key, double value) async =>
await _prefs!.setDouble(key, value);
static Future<bool> setInt(String key, int value) async =>
await _prefs!.setInt(key, value);
static Future<bool> setString(String key, String value) async =>
await _prefs!.setString(key, value);
static Future<bool> setStringList(String key, List<String> value) async =>
await _prefs!.setStringList(key, value);
//gets
static bool? getBool(String key) => _prefs!.getBool(key);
static double? getDouble(String key) => _prefs!.getDouble(key);
static int? getInt(String key) => _prefs!.getInt(key);
static String? getString(String key) => _prefs!.getString(key);
static List<String>? getStringList(String key) => _prefs!.getStringList(key);
//deletes..
static Future<bool> remove(String key) async => await _prefs!.remove(key);
static Future<bool> clear() async => await _prefs!.clear();
}
class RxPrefs {
static RxSharedPreferences? _prefs;
// call this method from iniState() function of mainApp().
static Future<RxSharedPreferences?> init() async {
_prefs = await RxSharedPreferences(SharedPreferences.getInstance(),
kReleaseMode ? null : RxSharedPreferencesDefaultLogger());
return _prefs;
}
//methods
static Future<Map<String, Object?>> reload() async => await _prefs!.reload();
static Future<Map<String, Object?>> readAll() async =>
await _prefs!.readAll();
static Future<bool> containsKey(String key) async =>
await _prefs!.containsKey(key);
//listen
static Stream<bool?> getBoolStream(String key) => _prefs!.getBoolStream(key);
static Stream<double?> getDoubleStream(String key) =>
_prefs!.getDoubleStream(key);
static Stream<int?> getIntStream(String key) => _prefs!.getIntStream(key);
static Stream<String?> getStringStream(String key) =>
_prefs!.getStringStream(key);
static Stream<List<String>?> getStringListStream(String key) =>
_prefs!.getStringListStream(key);
static Stream<Set<String>> getKeysStream() => _prefs!.getKeysStream();
//execute
static Future<void> executeUpdateBool(
String key, Transformer<bool?> transformer) =>
_prefs!.executeUpdateBool(key, transformer);
static Future<void> executeUpdateDouble(
String key, Transformer<double?> transformer) =>
_prefs!.executeUpdateDouble(key, transformer);
static Future<void> executeUpdateInt(
String key, Transformer<int?> transformer) =>
_prefs!.executeUpdateInt(key, transformer);
static Future<void> executeUpdateString(
String key, Transformer<String?> transformer) =>
_prefs!.executeUpdateString(key, transformer);
static Future<void> executeUpdateStringList(
String key, Transformer<List<String>?> transformer) =>
_prefs!.executeUpdateStringList(key, transformer);
//sets
static Future<void> setBool(String key, bool value) async =>
await _prefs!.setBool(key, value);
static Future<void> setDouble(String key, double value) async =>
await _prefs!.setDouble(key, value);
static Future<void> setInt(String key, int value) async =>
await _prefs!.setInt(key, value);
static Future<void> setString(String key, String value) async =>
await _prefs!.setString(key, value);
static Future<void> setStringList(String key, List<String> value) async =>
await _prefs!.setStringList(key, value);
//gets
static Future<bool?> getBool(String key) => _prefs!.getBool(key);
static Future<double?> getDouble(String key) => _prefs!.getDouble(key);
static Future<int?> getInt(String key) => _prefs!.getInt(key);
static Future<String?> getString(String key) => _prefs!.getString(key);
static Future<List<String>?> getStringList(String key) =>
_prefs!.getStringList(key);
//deletes..
static Future<void> remove(String key) async => await _prefs!.remove(key);
static Future<void> clear() async => await _prefs!.clear();
}
class PrefsSecure {
static SecureSharedPref? _prefs;
static Future<SecureSharedPref?> init() async {
_prefs = await SecureSharedPref.getInstance();
return _prefs;
}
//sets
static Future setBool(String key, bool value, {isEncrypted = true}) async =>
await _prefs!.putBool(key, value, isEncrypted: isEncrypted);
static Future setDouble(String key, double value,
{isEncrypted = true}) async =>
await _prefs!.putDouble(key, value);
static Future setInt(String key, int value, {isEncrypted = true}) async =>
await _prefs!.putInt(key, value);
static Future setString(String key, String value,
{isEncrypted = true}) async =>
await _prefs!.putString(key, value);
static Future setStringList(String key, List<String> value,
{isEncrypted = true}) async =>
await _prefs!.putStringList(key, value);
//gets
static getBool(String key, {isEncrypted = true}) => _prefs!.getBool(key);
static getDouble(String key, {isEncrypted = true}) => _prefs!.getDouble(key);
static getInt(String key, {isEncrypted = true}) => _prefs!.getInt(key);
static getString(String key, {isEncrypted = true}) => _prefs!.getString(key);
static getStringList(String key, {isEncrypted = true}) =>
_prefs!.getStringList(key);
static clear() async => await _prefs!.clearAll();
}
@febritecno
Copy link
Author

febritecno commented Sep 10, 2022

thanks, that's very helpful ~

@febritecno
Copy link
Author

febritecno commented Sep 11, 2022

 AppDialog.showAlert(
                                              btnLeft: "Tidak",
                                              btnRight: "Iya",
                                              desc:
                                                  "pilihan tidak akan bisa diubah setelah data tersimpan",
                                              onBtnRight: () {
                                                Get.back();
                                                Get.back();
                                                controller.submitOption();
                                              },
                                              title: "Apakah anda yakin ?",
                                            );

@febritecno
Copy link
Author

febritecno commented Sep 11, 2022

import 'dart:async';
import 'package:absen/helpers/app_key.dart';
import 'package:absen/helpers/helpers.dart';
import 'package:absen/services/exceptions/app_exception.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:latlong2/latlong.dart';

class HomeController extends GetxController {
  var isLoading = false.obs;
  var textNow = {'date': '-', 'time': '-'}.obs;
  var currentLocation = [0.0, 0.0].obs;
  var targetLocation = ['0.0', '0.0'].obs;
  var setDistance = 0.obs;

  var cancelTimer = false.obs;

  @override
  void onInit() {
    super.onInit();
    initHome();
    getTimeNow();
    cancelTimer(false);
  }

  @override
  void onReady() {
    super.onReady();
    setInterval();
  }

  initHome() async {
    isLoading(true);
    try {
      targetLocation
          .assignAll(AppKey.getTargetLocation().toString().split(','));
      await distanceInMeter();
      isLoading(false);
    } catch (e) {
      isLoading(false);
      throw AppException(message: e.toString());
    }
  }

  getTimeNow() {
    final now = DateTime.now();
    textNow()['date'] = DateFormat('dd MMM yyyy').format(now);
    textNow()['time'] = DateFormat.jm().format(now);
  }

  setInterval() async {
    var _record = 0;
    await Timer.periodic(Duration(seconds: 1), (timer) {
      if (cancelTimer.value == true) {
        // print("stop timer");
        timer.cancel();
      } else {
        _record += 1;
        if (_record == Duration(minutes: 1).inSeconds) {
          initHome();
          _record = 0;
        }
        // print(_record);
      }
    });
  }

  distanceInMeter() async {
    final Distance _distance = new Distance();
    await Helpers.determinePosition()
        .then((data) => currentLocation([data.latitude, data.longitude]));
    final meter = _distance(
        LatLng(currentLocation[0], currentLocation[1]),
        LatLng(
            double.parse(targetLocation[0]), double.parse(targetLocation[1])));
    setDistance(meter.toInt());
  }

  submitAttendance() {}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment