feat: save module

This commit is contained in:
2025-09-02 17:28:02 +03:30
parent 3945e04ae1
commit ebec27b630
7 changed files with 192 additions and 33 deletions

View 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;
}
}
}

View File

@@ -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_core/core.dart';
import 'infrastructure/di/di.dart';
import 'infrastructure/service/local_storage_service.dart';
import 'presentation/routes/auth_route_resolver_impl.dart';
@@ -11,6 +12,7 @@ Future<void> main() async {
await Hive.initFlutter();
await setupPreInjection();
Get.put(TokenStorageService());
Get.put(LocalStorageService());
await Get.find<TokenStorageService>().init();
Get.put<AuthRouteResolver>(AppAuthRouteResolver());
Get.put(AuthMiddleware());

View File

@@ -3,7 +3,7 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.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';
class SplashLogic extends GetxController with GetTickerProviderStateMixin {
@@ -18,6 +18,7 @@ class SplashLogic extends GetxController with GetTickerProviderStateMixin {
final platform = MethodChannel('apk_installer');
final Dio _dio = Dio();
var tokenService = Get.find<TokenStorageService>();
var localService = Get.find<LocalStorageService>();
AppInfoModel? appInfoModel;
@override
@@ -86,25 +87,26 @@ class SplashLogic extends GetxController with GetTickerProviderStateMixin {
children: [
const Text('در حال دانلود بروزرسانی برنامه...'),
Obx(
() => Row(
spacing: 8,
children: [
Expanded(
child: LinearProgressIndicator(
value: percent.value,
color: AppColor.greenNormal,
minHeight: 4,
),
() =>
Row(
spacing: 8,
children: [
Expanded(
child: LinearProgressIndicator(
value: percent.value,
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(
@@ -117,9 +119,9 @@ class SplashLogic extends GetxController with GetTickerProviderStateMixin {
height: 40.h,
onPressed: data.value != null
? () {
installApk();
Get.back();
}
installApk();
Get.back();
}
: null,
text: 'نصب',
);
@@ -154,11 +156,17 @@ class SplashLogic extends GetxController with GetTickerProviderStateMixin {
if (isUpdateNeeded) return;
tokenService.getModule();
final module = tokenService.appModule.value;
final target = getTargetModule(module);
if (target.values.first != null) {
await target.values.first;
final target = localService.getTargetPage(module);
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) {
debugPrint("onReady error: $e\n$st");
}