Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 33 additions & 32 deletions lib/core/installation_tracker.dart
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
import 'package:dpip/core/preference.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:dpip/utils/log.dart';
import 'package:uuid/uuid.dart';

final talker = TalkerManager.instance;
const _uuid = Uuid();

Future<void> initializeInstallationData() async {
final packageInfo = await PackageInfo.fromPlatform();
final currentVersion = packageInfo.version;
final currentBuildNumber = packageInfo.buildNumber;

final PackageInfo packageInfo = await PackageInfo.fromPlatform();
final int? installTime = packageInfo.installTime?.millisecondsSinceEpoch;
final String currentVersion = packageInfo.version;
String? installId = await Preference.installId;
final storedVersion = Preference.version;
final storedBuildNumber = Preference.buildNumber;

if (installTime == null) {
talker.warning('無法獲取目前的應用程式安裝時間,無法執行安裝狀態檢查。');
return;
}
if (installId == null) {
talker.info('首次安裝或資料重置,建立新的 installId');

final String? storedVersion = Preference.version;
final int? storedInstallTime = Preference.installTime;
installId = _uuid.v4();
await Preference.setInstallId(installId);

if (storedInstallTime == null || storedVersion == null) {
talker.info('這是 App 首次安裝。');

Preference.instance.clear();
Preference.installTime = installTime;
Preference.version = currentVersion;
talker.info('已儲存新的安裝時間和版本,並執行了數據初始化/重置。');
} else if (storedVersion != currentVersion) {
Preference.buildNumber = currentBuildNumber;
talker.info(
'已建立新的 installId 並儲存版本資訊',
'version: $currentVersion | buildNumber: $currentBuildNumber\n'
'installId: $installId'
);
return;
}

if (storedVersion != currentVersion || storedBuildNumber != currentBuildNumber) {
talker.info('偵測到版本變更:$storedVersion → $currentVersion');

if (storedInstallTime != installTime) {
talker.info('安裝時間也已變更,這是版本升級。');
Preference.installTime = installTime;

if (storedBuildNumber != currentBuildNumber) {
talker.info('Build 號也已變更,這是版本升級。');
}

Preference.version = currentVersion;
Preference.buildNumber = currentBuildNumber;
talker.info('已更新版本資訊,保留現有數據。');
} else if (storedInstallTime != installTime) {
talker.warning('偵測到相同版本但安裝時間不同。');
talker.log('系統安裝時間: $installTime, 儲存的安裝時間: $storedInstallTime');
talker.info('這是解除安裝後重新安裝相同版本。');

Preference.instance.clear();
Preference.installTime = installTime;
Preference.version = currentVersion;
talker.info('已執行數據重置。');
} else {
talker.info('應用程式未重新安裝也未升級。使用現有安裝資料。');
return;
}

talker.info(
'應用程式未重新安裝也未升級,使用現有的安裝資料。\n'
'version: $currentVersion | buildNumber: $currentBuildNumber\n'
'installId: $installId'
);
}
24 changes: 20 additions & 4 deletions lib/core/preference.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import 'package:i18n_extension/i18n_extension.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

import 'package:dpip/core/i18n.dart';
import 'package:dpip/utils/extensions/preference.dart';

class PreferenceKeys {
static const lastUpdateToServerTime = 'lastUpdateToServerTime';

// region App Installation
static const appVersion = 'app-version';
static const appBuildNumber = 'app-build-number';
static const installId = 'install-id';
// endregion

// #region Location
static const locationAuto = 'location:auto';
static const locationCode = 'location:code';
Expand Down Expand Up @@ -44,6 +51,7 @@ class Preference {
Preference._();

static late SharedPreferencesWithCache instance;
static const _secureStorage = FlutterSecureStorage();

static Future<void> init() async {
instance = await SharedPreferencesWithCache.create(cacheOptions: const SharedPreferencesWithCacheOptions());
Expand All @@ -54,11 +62,19 @@ class Preference {
await instance.reloadCache();
}

static String? get version => instance.getString('app-version');
static set version(String? value) => instance.set('app-version', value);
// #region App Installation
static String? get version => instance.getString(PreferenceKeys.appVersion);
static set version(String? value) => instance.set(PreferenceKeys.appVersion, value);

static String? get buildNumber => instance.getString(PreferenceKeys.appBuildNumber);
static set buildNumber(String? value) => instance.set(PreferenceKeys.appBuildNumber, value);

static int? get installTime => instance.getInt('install-time');
static set installTime(int? value) => instance.set('install-time', value);
static Future<String?> get installId => _secureStorage.read(key: PreferenceKeys.installId);
static Future<void> setInstallId(String? value) =>
value == null
? _secureStorage.delete(key: PreferenceKeys.installId)
: _secureStorage.write(key: PreferenceKeys.installId, value: value);
// #endregion

static int? get lastUpdateToServerTime => instance.getInt(PreferenceKeys.lastUpdateToServerTime);
static set lastUpdateToServerTime(int? value) => instance.set(PreferenceKeys.lastUpdateToServerTime, value);
Expand Down
24 changes: 14 additions & 10 deletions lib/core/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ class LocationService {

DartPluginRegistrant.ensureInitialized();

await Preference.init();
await AppLocalizations.load();
await LocationNameLocalizations.load();

if (Preference.locationAuto != true) {
await _$onStop();
return;
}

_$geoJsonData = await Global.loadTownGeojson();
_$locationData = await Global.loadLocationData();

_$service.setAutoStartOnBootMode(true);

await AwesomeNotifications().createNotification(
content: NotificationContent(
id: LocationServiceManager.kNotificationId,
Expand All @@ -230,16 +244,6 @@ class LocationService {
);

await _$service.setAsForegroundService();

await Preference.init();
await AppLocalizations.load();
await LocationNameLocalizations.load();

_$geoJsonData = await Global.loadTownGeojson();
_$locationData = await Global.loadLocationData();

_$service.setAutoStartOnBootMode(true);

_$service.on(LocationServiceEvent.stop).listen((_) => _$onStop());

// Start the periodic location update task
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dynamic_system_colors
import firebase_core
import firebase_crashlytics
import firebase_messaging
import flutter_secure_storage_macos
import gal
import geolocator_apple
import in_app_purchase_storekit
Expand All @@ -28,6 +29,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin"))
FLTFirebaseCrashlyticsPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCrashlyticsPlugin"))
FLTFirebaseMessagingPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseMessagingPlugin"))
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
GalPlugin.register(with: registry.registrar(forPlugin: "GalPlugin"))
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))
InAppPurchasePlugin.register(with: registry.registrar(forPlugin: "InAppPurchasePlugin"))
Expand Down
66 changes: 61 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,54 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.7.7"
flutter_secure_storage:
dependency: "direct main"
description:
name: flutter_secure_storage
sha256: "9cad52d75ebc511adfae3d447d5d13da15a55a92c9410e50f67335b6d21d16ea"
url: "https://pub.dev"
source: hosted
version: "9.2.4"
flutter_secure_storage_linux:
dependency: transitive
description:
name: flutter_secure_storage_linux
sha256: be76c1d24a97d0b98f8b54bce6b481a380a6590df992d0098f868ad54dc8f688
url: "https://pub.dev"
source: hosted
version: "1.2.3"
flutter_secure_storage_macos:
dependency: transitive
description:
name: flutter_secure_storage_macos
sha256: "6c0a2795a2d1de26ae202a0d78527d163f4acbb11cde4c75c670f3a0fc064247"
url: "https://pub.dev"
source: hosted
version: "3.1.3"
flutter_secure_storage_platform_interface:
dependency: transitive
description:
name: flutter_secure_storage_platform_interface
sha256: cf91ad32ce5adef6fba4d736a542baca9daf3beac4db2d04be350b87f69ac4a8
url: "https://pub.dev"
source: hosted
version: "1.1.2"
flutter_secure_storage_web:
dependency: transitive
description:
name: flutter_secure_storage_web
sha256: f4ebff989b4f07b2656fb16b47852c0aab9fed9b4ec1c70103368337bc1886a9
url: "https://pub.dev"
source: hosted
version: "1.2.1"
flutter_secure_storage_windows:
dependency: transitive
description:
name: flutter_secure_storage_windows
sha256: b20b07cb5ed4ed74fc567b78a72936203f587eba460af1df11281c9326cd3709
url: "https://pub.dev"
source: hosted
version: "3.1.2"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -742,10 +790,10 @@ packages:
dependency: transitive
description:
name: js
sha256: "53385261521cc4a0c4658fd0ad07a7d14591cf8fc33abbceae306ddb974888dc"
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
url: "https://pub.dev"
source: hosted
version: "0.7.2"
version: "0.6.7"
json_annotation:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1011,6 +1059,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.3.0"
permission_handler_windows:
dependency: transitive
description:
name: permission_handler_windows
sha256: "1a790728016f79a41216d88672dbc5df30e686e811ad4e698bfc51f76ad91f1e"
url: "https://pub.dev"
source: hosted
version: "0.2.1"
petitparser:
dependency: transitive
description:
Expand Down Expand Up @@ -1433,13 +1489,13 @@ packages:
source: hosted
version: "3.1.4"
uuid:
dependency: transitive
dependency: "direct main"
description:
name: uuid
sha256: a5be9ef6618a7ac1e964353ef476418026db906c4facdedaa299b7a2e71690ff
sha256: a11b666489b1954e01d992f3d601b1804a33937b5a8fe677bd26b8a9f96f96e8
url: "https://pub.dev"
source: hosted
version: "4.5.1"
version: "4.5.2"
vector_math:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ dependencies:
styled_text: ^8.1.0
talker_flutter: ^4.7.2
timezone: ^0.10.0
uuid: ^4.5.2
url_launcher: ^6.3.1
ip_country_lookup: ^1.0.0
flutter_icmp_ping: ^3.1.3
geojson_vi: ^2.2.5
crypto: ^3.0.6
async: ^2.13.0
flutter_secure_storage: ^9.0.0

dev_dependencies:
build_runner: ^2.10.0
Expand Down