feat: save module
This commit is contained in:
84
lib/infrastructure/service/local_storage_service.dart
Normal file
84
lib/infrastructure/service/local_storage_service.dart
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import 'package:rasadyar_app/presentation/routes/app_pages.dart';
|
||||||
|
import 'package:rasadyar_chicken/data/di/chicken_di.dart';
|
||||||
|
import 'package:rasadyar_chicken/presentation/routes/routes.dart';
|
||||||
|
import 'package:rasadyar_core/core.dart';
|
||||||
|
import 'package:rasadyar_core/data/model/local/target_page/target_page.dart';
|
||||||
|
import 'package:rasadyar_core/hive_registrar.g.dart';
|
||||||
|
import 'package:rasadyar_inspection/injection/inspection_di.dart';
|
||||||
|
import 'package:rasadyar_inspection/inspection.dart';
|
||||||
|
import 'package:rasadyar_livestock/injection/live_stock_di.dart';
|
||||||
|
import 'package:rasadyar_livestock/presentation/routes/app_pages.dart';
|
||||||
|
|
||||||
|
class LocalStorageService extends GetxService {
|
||||||
|
static const String _targetPageBox = 'targetPageBox';
|
||||||
|
|
||||||
|
final HiveLocalStorage _localStorage = diCore.get<HiveLocalStorage>();
|
||||||
|
late Box<TargetPage> _targetBox;
|
||||||
|
|
||||||
|
Future<void> init() async {
|
||||||
|
Hive.registerAdapters();
|
||||||
|
await _localStorage.init();
|
||||||
|
await _localStorage.openBox<TargetPage>(_targetPageBox);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> seedTargetPage() async {
|
||||||
|
var existing = getTargetPage(null);
|
||||||
|
if (existing == null) {
|
||||||
|
_localStorage.addAll(
|
||||||
|
boxName: _targetPageBox,
|
||||||
|
values: [
|
||||||
|
TargetPage(
|
||||||
|
route: InspectionRoutes.init,
|
||||||
|
module: Module.inspection,
|
||||||
|
functions: ["setupInspectionDI"],
|
||||||
|
),
|
||||||
|
TargetPage(
|
||||||
|
route: LiveStockRoutes.init,
|
||||||
|
module: Module.liveStocks,
|
||||||
|
functions: ["setupLiveStockDI"],
|
||||||
|
),
|
||||||
|
TargetPage(
|
||||||
|
route: ChickenRoutes.initSteward,
|
||||||
|
module: Module.chicken,
|
||||||
|
functions: ["setupChickenDI"],
|
||||||
|
),
|
||||||
|
TargetPage(route: AppPaths.moduleList),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TargetPage? getTargetPage(Module? module) {
|
||||||
|
var res = _localStorage
|
||||||
|
.readBox<TargetPage>(boxName: _targetPageBox)
|
||||||
|
?.firstWhereOrNull((element) => element.module == module);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveTargetPage(TargetPage targetPage) async {
|
||||||
|
await _localStorage.add(boxName: _targetPageBox, value: targetPage);
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterable<Future?>? getFunctionsList(List<String>? functions) {
|
||||||
|
return functions?.map((e) => getFunctionByName(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
Future? getFunctionByName(String? name) {
|
||||||
|
switch (name) {
|
||||||
|
case "setupInspectionDI":
|
||||||
|
return setupInspectionDI();
|
||||||
|
case "removeInspectionDI":
|
||||||
|
return removeInspectionDI();
|
||||||
|
case "setupLiveStockDI":
|
||||||
|
return setupLiveStockDI();
|
||||||
|
case "removeLiveStockDI":
|
||||||
|
return removeLiveStockDI();
|
||||||
|
case "setupChickenDI":
|
||||||
|
return setupChickenDI();
|
||||||
|
case "removeChickenDI":
|
||||||
|
return removeChickenDI();
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ import 'package:rasadyar_app/infrastructure/service/app_navigation_observer.dart
|
|||||||
import 'package:rasadyar_app/presentation/routes/app_pages.dart';
|
import 'package:rasadyar_app/presentation/routes/app_pages.dart';
|
||||||
import 'package:rasadyar_core/core.dart';
|
import 'package:rasadyar_core/core.dart';
|
||||||
import 'infrastructure/di/di.dart';
|
import 'infrastructure/di/di.dart';
|
||||||
|
import 'infrastructure/service/local_storage_service.dart';
|
||||||
import 'presentation/routes/auth_route_resolver_impl.dart';
|
import 'presentation/routes/auth_route_resolver_impl.dart';
|
||||||
|
|
||||||
|
|
||||||
@@ -11,6 +12,7 @@ Future<void> main() async {
|
|||||||
await Hive.initFlutter();
|
await Hive.initFlutter();
|
||||||
await setupPreInjection();
|
await setupPreInjection();
|
||||||
Get.put(TokenStorageService());
|
Get.put(TokenStorageService());
|
||||||
|
Get.put(LocalStorageService());
|
||||||
await Get.find<TokenStorageService>().init();
|
await Get.find<TokenStorageService>().init();
|
||||||
Get.put<AuthRouteResolver>(AppAuthRouteResolver());
|
Get.put<AuthRouteResolver>(AppAuthRouteResolver());
|
||||||
Get.put(AuthMiddleware());
|
Get.put(AuthMiddleware());
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import 'dart:io';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:rasadyar_app/data/model/app_info_model.dart';
|
import 'package:rasadyar_app/data/model/app_info_model.dart';
|
||||||
import 'package:rasadyar_app/presentation/routes/app_pages.dart';
|
import 'package:rasadyar_app/infrastructure/service/local_storage_service.dart';
|
||||||
import 'package:rasadyar_core/core.dart';
|
import 'package:rasadyar_core/core.dart';
|
||||||
|
|
||||||
class SplashLogic extends GetxController with GetTickerProviderStateMixin {
|
class SplashLogic extends GetxController with GetTickerProviderStateMixin {
|
||||||
@@ -18,6 +18,7 @@ class SplashLogic extends GetxController with GetTickerProviderStateMixin {
|
|||||||
final platform = MethodChannel('apk_installer');
|
final platform = MethodChannel('apk_installer');
|
||||||
final Dio _dio = Dio();
|
final Dio _dio = Dio();
|
||||||
var tokenService = Get.find<TokenStorageService>();
|
var tokenService = Get.find<TokenStorageService>();
|
||||||
|
var localService = Get.find<LocalStorageService>();
|
||||||
AppInfoModel? appInfoModel;
|
AppInfoModel? appInfoModel;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -86,25 +87,26 @@ class SplashLogic extends GetxController with GetTickerProviderStateMixin {
|
|||||||
children: [
|
children: [
|
||||||
const Text('در حال دانلود بروزرسانی برنامه...'),
|
const Text('در حال دانلود بروزرسانی برنامه...'),
|
||||||
Obx(
|
Obx(
|
||||||
() => Row(
|
() =>
|
||||||
spacing: 8,
|
Row(
|
||||||
children: [
|
spacing: 8,
|
||||||
Expanded(
|
children: [
|
||||||
child: LinearProgressIndicator(
|
Expanded(
|
||||||
value: percent.value,
|
child: LinearProgressIndicator(
|
||||||
color: AppColor.greenNormal,
|
value: percent.value,
|
||||||
minHeight: 4,
|
color: AppColor.greenNormal,
|
||||||
),
|
minHeight: 4,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(
|
||||||
|
width: 55.w,
|
||||||
|
child: Text(
|
||||||
|
'${(percent.value * 100).toStringAsFixed(2)}%',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
SizedBox(
|
|
||||||
width: 55.w,
|
|
||||||
child: Text(
|
|
||||||
'${(percent.value * 100).toStringAsFixed(2)}%',
|
|
||||||
textAlign: TextAlign.center,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
|
|
||||||
Row(
|
Row(
|
||||||
@@ -117,9 +119,9 @@ class SplashLogic extends GetxController with GetTickerProviderStateMixin {
|
|||||||
height: 40.h,
|
height: 40.h,
|
||||||
onPressed: data.value != null
|
onPressed: data.value != null
|
||||||
? () {
|
? () {
|
||||||
installApk();
|
installApk();
|
||||||
Get.back();
|
Get.back();
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
text: 'نصب',
|
text: 'نصب',
|
||||||
);
|
);
|
||||||
@@ -154,11 +156,17 @@ class SplashLogic extends GetxController with GetTickerProviderStateMixin {
|
|||||||
if (isUpdateNeeded) return;
|
if (isUpdateNeeded) return;
|
||||||
tokenService.getModule();
|
tokenService.getModule();
|
||||||
final module = tokenService.appModule.value;
|
final module = tokenService.appModule.value;
|
||||||
final target = getTargetModule(module);
|
final target = localService.getTargetPage(module);
|
||||||
if (target.values.first != null) {
|
|
||||||
await target.values.first;
|
if (target != null) {
|
||||||
|
var funs = localService.getFunctionsList(target.functions);
|
||||||
|
await Future.wait(funs ?? []);
|
||||||
|
|
||||||
|
Get.offAndToNamed(target.route!);
|
||||||
|
|
||||||
}
|
}
|
||||||
Get.offAndToNamed(target.keys.first);
|
|
||||||
|
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
debugPrint("onReady error: $e\n$st");
|
debugPrint("onReady error: $e\n$st");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import 'package:rasadyar_core/core.dart';
|
||||||
|
|
||||||
|
part 'target_page.g.dart';
|
||||||
|
|
||||||
|
@HiveType(typeId: targetPageTypeId)
|
||||||
|
class TargetPage extends HiveObject {
|
||||||
|
@HiveField(0)
|
||||||
|
String? route;
|
||||||
|
|
||||||
|
@HiveField(1)
|
||||||
|
List<String>? functions;
|
||||||
|
|
||||||
|
@HiveField(2)
|
||||||
|
Module? module;
|
||||||
|
|
||||||
|
TargetPage({required this.route, this.functions, this.module});
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'target_page.dart';
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// TypeAdapterGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
class TargetPageAdapter extends TypeAdapter<TargetPage> {
|
||||||
|
@override
|
||||||
|
final typeId = 2;
|
||||||
|
|
||||||
|
@override
|
||||||
|
TargetPage read(BinaryReader reader) {
|
||||||
|
final numOfFields = reader.readByte();
|
||||||
|
final fields = <int, dynamic>{
|
||||||
|
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||||
|
};
|
||||||
|
return TargetPage(
|
||||||
|
route: fields[0] as String,
|
||||||
|
functions: (fields[1] as List?)?.cast<String>(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void write(BinaryWriter writer, TargetPage obj) {
|
||||||
|
writer
|
||||||
|
..writeByte(2)
|
||||||
|
..writeByte(0)
|
||||||
|
..write(obj.route)
|
||||||
|
..writeByte(1)
|
||||||
|
..write(obj.functions);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => typeId.hashCode;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(Object other) =>
|
||||||
|
identical(this, other) ||
|
||||||
|
other is TargetPageAdapter &&
|
||||||
|
runtimeType == other.runtimeType &&
|
||||||
|
typeId == other.typeId;
|
||||||
|
}
|
||||||
@@ -3,11 +3,13 @@
|
|||||||
// Check in to version control
|
// Check in to version control
|
||||||
|
|
||||||
import 'package:hive_ce/hive.dart';
|
import 'package:hive_ce/hive.dart';
|
||||||
|
import 'package:rasadyar_core/data/model/local/target_page/target_page.dart';
|
||||||
import 'package:rasadyar_core/data/model/local/user_local/user_local_model.dart';
|
import 'package:rasadyar_core/data/model/local/user_local/user_local_model.dart';
|
||||||
|
|
||||||
extension HiveRegistrar on HiveInterface {
|
extension HiveRegistrar on HiveInterface {
|
||||||
void registerAdapters() {
|
void registerAdapters() {
|
||||||
registerAdapter(ModuleAdapter());
|
registerAdapter(ModuleAdapter());
|
||||||
|
registerAdapter(TargetPageAdapter());
|
||||||
registerAdapter(UserLocalModelAdapter());
|
registerAdapter(UserLocalModelAdapter());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,6 +17,7 @@ extension HiveRegistrar on HiveInterface {
|
|||||||
extension IsolatedHiveRegistrar on IsolatedHiveInterface {
|
extension IsolatedHiveRegistrar on IsolatedHiveInterface {
|
||||||
void registerAdapters() {
|
void registerAdapters() {
|
||||||
registerAdapter(ModuleAdapter());
|
registerAdapter(ModuleAdapter());
|
||||||
|
registerAdapter(TargetPageAdapter());
|
||||||
registerAdapter(UserLocalModelAdapter());
|
registerAdapter(UserLocalModelAdapter());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
//Auth
|
//Auth
|
||||||
const int authUserLocalModelTypeId = 0;
|
const int authUserLocalModelTypeId = 0;
|
||||||
const int authModuleTypeId = 1;
|
const int authModuleTypeId = 1;
|
||||||
|
const int targetPageTypeId = 2;
|
||||||
|
|
||||||
//chicken
|
//chicken
|
||||||
const int chickenWidelyUsedLocalModelTypeId = 2;
|
const int chickenWidelyUsedLocalModelTypeId = 3;
|
||||||
const int chickenWidelyUsedLocalItemTypeId = 3;
|
const int chickenWidelyUsedLocalItemTypeId = 4;
|
||||||
|
|
||||||
//liveStock
|
//liveStock
|
||||||
|
|
||||||
const int liveStockDataLocalModelTypeId = 4;
|
const int liveStockDataLocalModelTypeId = 5;
|
||||||
const int liveStockDataRancherLocalModelTypeId = 5;
|
const int liveStockDataRancherLocalModelTypeId = 6;
|
||||||
const int liveStockDataHerdLocalModelTypeId = 6;
|
const int liveStockDataHerdLocalModelTypeId = 7;
|
||||||
const int liveStockDataLocationLocalModelTypeId = 7;
|
const int liveStockDataLocationLocalModelTypeId = 8;
|
||||||
const int liveStockDataLivestockLocalModelTypeId = 8;
|
const int liveStockDataLivestockLocalModelTypeId = 9;
|
||||||
Reference in New Issue
Block a user