Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

86 linhas
2.4 KiB

  1. import 'dart:io';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:hive/hive.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:qadirneyriz/utils/enums/status.dart';
  6. import 'package:qadirneyriz/utils/hive/local_user.dart';
  7. class UserLocalDb {
  8. Box? initialbox;
  9. Box? box;
  10. bool isInitialized = false;
  11. initialized() async {
  12. if (!kIsWeb) {
  13. Directory appDocDirectory = await getApplicationDocumentsDirectory();
  14. var rootPath = Directory('${appDocDirectory.path}/dir');
  15. await rootPath.create(recursive: true);
  16. Hive.init(rootPath.path);
  17. }
  18. isInitialized = true;
  19. }
  20. openBox() async {
  21. await initialized();
  22. initialbox ??= await Hive.openBox('user_hive');
  23. return initialbox;
  24. }
  25. open() {
  26. box = Hive.box('user_hive');
  27. return box;
  28. }
  29. LocalUser getUser() {
  30. if (box == null) {
  31. open();
  32. }
  33. String name = box!.get('name', defaultValue: '');
  34. String token = box!.get('token', defaultValue: '');
  35. String mobile = box!.get('mobile', defaultValue: '');
  36. int role = box!.get('role', defaultValue: 0);
  37. List access = box!.get('access', defaultValue: []);
  38. String language = box!.get('language', defaultValue: 'en');
  39. return LocalUser(
  40. name: name,
  41. token: token,
  42. mobile: mobile,
  43. access: access,
  44. role: role,
  45. language: language);
  46. }
  47. saveUserField(String key, value) async {
  48. open();
  49. await box!.put(key, value);
  50. }
  51. Future<bool> logOut() async {
  52. await openBox(); // باز کردن Box
  53. await box!.clear(); // حذف تمامی داده‌ها از Box
  54. // بررسی تعداد آیتم‌های باقی‌مانده
  55. final int remainingItems = box!.length;
  56. // بازگرداندن true اگر داده‌ها حذف شدند، false در غیر این صورت
  57. return remainingItems == 0;
  58. }
  59. // Future<void> updateUser(LocalUser user) async {
  60. // await openBox();
  61. // box!.put('name', user.userName);
  62. // box!.put('token', user.token);
  63. // // box!.put('phone', user.phoneNumber);
  64. // // box!.put('isLogin', user.isLogin);
  65. // // box!.put('password', user.password);
  66. // await initialbox!.put('name', user.userName);
  67. // await initialbox!.put('token', user.token);
  68. // // await initialbox!.put('phone', user.phoneNumber);
  69. // // await initialbox!.put('isLogin', user.isLogin);
  70. // // await initialbox!.put('password', user.password);
  71. // }
  72. }