@@ -9,6 +9,10 @@ import 'services/notification_service.dart';
99import 'services/theme_services.dart' ;
1010import 'services/language_service.dart' ;
1111import 'services/bookmark_service.dart' ;
12+ import 'services/bookmark_service_v2.dart' ;
13+ import 'services/history_service_v2.dart' ;
14+ import 'services/novel_database_service.dart' ;
15+ import 'services/novel_data_migration_service.dart' ;
1216import 'services/proxy_service.dart' ;
1317import 'services/http_client.dart' ;
1418import 'services/dns_service.dart' ;
@@ -21,15 +25,12 @@ import 'services/crawler_service.dart';
2125import 'services/preferences_service.dart' ;
2226import 'services/preferences_recovery_service.dart' ;
2327
24-
2528// Firebase
2629// import 'package:firebase_core/firebase_core.dart';
2730// import 'firebase_options.dart';
2831// import 'package:firebase_crashlytics/firebase_crashlytics.dart';
2932// import 'package:firebase_analytics/firebase_analytics.dart';
3033
31-
32-
3334// Dart libs
3435import 'dart:ui' ;
3536
@@ -65,6 +66,73 @@ Future<void> migratePreferences() async {
6566 }
6667}
6768
69+ // Function to migrate novel data from JSON to database
70+ Future <void > migrateNovelData () async {
71+ try {
72+ debugPrint ('🔄 Checking if novel data migration is needed...' );
73+
74+ final migrationService = NovelDataMigrationService ();
75+
76+ // Check if we need to fix a previous migration (backup exists but migration done)
77+ final prefs = PreferencesService ();
78+ await prefs.initialize ();
79+
80+ final migrationComplete = prefs.getBool (
81+ 'novel_data_migration_complete_v2' ,
82+ defaultValue: false ,
83+ );
84+ final hasBackup = prefs.getString ('bookmarked_novels_backup' ).isNotEmpty;
85+
86+ // If migration was done but we still have backup, fix the migration
87+ if (migrationComplete && hasBackup) {
88+ debugPrint ('🔧 Detected previous migration with URL issues, fixing...' );
89+
90+ final fixSuccess = await migrationService.fixMigration ();
91+
92+ if (fixSuccess) {
93+ debugPrint ('✅ Migration fix successful!' );
94+
95+ // Show stats
96+ final info = await migrationService.getMigrationInfo ();
97+ debugPrint (' Fixed bookmarks: ${info ['newData' ]?['bookmarks' ] ?? 0 }' );
98+ debugPrint (' Fixed history: ${info ['newData' ]?['history' ] ?? 0 }' );
99+ } else {
100+ debugPrint ('⚠️ Migration fix had issues, using existing data' );
101+ }
102+
103+ return ;
104+ }
105+
106+ if (await migrationService.needsMigration ()) {
107+ debugPrint ('📚 Starting novel data migration (JSON → Database)...' );
108+
109+ final success = await migrationService.migrate ();
110+
111+ if (success) {
112+ debugPrint ('✅ Novel data migration successful!' );
113+
114+ // Show migration info
115+ final info = await migrationService.getMigrationInfo ();
116+ debugPrint (' Old bookmarks: ${info ['oldData' ]?['bookmarks' ] ?? 0 }' );
117+ debugPrint (' Old history: ${info ['oldData' ]?['history' ] ?? 0 }' );
118+ debugPrint (' New bookmarks: ${info ['newData' ]?['bookmarks' ] ?? 0 }' );
119+ debugPrint (' New history: ${info ['newData' ]?['history' ] ?? 0 }' );
120+
121+ // Clean up old JSON data (backed up first)
122+ await migrationService.cleanupOldData ();
123+ debugPrint ('🧹 Old JSON data cleaned up (backups saved)' );
124+ } else {
125+ debugPrint ('⚠️ Novel data migration had issues, but app will continue' );
126+ }
127+ } else {
128+ debugPrint ('ℹ️ Novel data migration not needed (already completed)' );
129+ }
130+ } catch (e) {
131+ debugPrint ('❌ Error during novel data migration: $e ' );
132+ debugPrint ('App will continue with current data' );
133+ }
134+ }
135+
68136void main () async {
69137 WidgetsFlutterBinding .ensureInitialized ();
70138
@@ -98,30 +166,35 @@ void main() async {
98166 await serverManagement.initialize ();
99167 await urlMigration.migrateNovelUrls ();
100168
169+ // Migrate novel data from JSON to database (CRITICAL FIX!)
170+ await migrateNovelData ();
171+
101172 final themeService = ThemeServices ();
102173 final languageService = LanguageService ();
103174 final notificationService = NotificationService ();
104175 final bookmarkService = BookmarkService ();
105- final historyService = HistoryService ();
176+ final bookmarkServiceV2 = BookmarkServiceV2 ();
177+ // final historyService = HistoryService(); // Old service removed
178+ final historyServiceV2 = HistoryServiceV2 ();
106179 final proxyService = ProxyService ();
107180 final httpClient = AppHttpClient ();
108181 final dnsService = DnsService ();
109182 final crawlerService = CrawlerService ();
110183 final preferencesService = PreferencesService ();
111184
112-
113185 await Future .wait ([
114186 preferencesService.initialize (),
115187 themeService.init (),
116188 languageService.init (),
117189 notificationService.init (),
118190 bookmarkService.init (),
119- historyService.loadHistory (),
191+ bookmarkServiceV2.init (),
192+ // historyService.loadHistory(), // Old service removed
193+ historyServiceV2.init (),
120194 proxyService.initialize (),
121195 httpClient.initialize (),
122196 dnsService.initialize (),
123197 crawlerService.initialize (),
124-
125198 ]);
126199
127200 // Set initial system UI styling
@@ -151,7 +224,11 @@ void main() async {
151224 value: notificationService,
152225 ),
153226 ChangeNotifierProvider <BookmarkService >.value (value: bookmarkService),
154- ChangeNotifierProvider <HistoryService >.value (value: historyService),
227+ ChangeNotifierProvider <BookmarkServiceV2 >.value (
228+ value: bookmarkServiceV2,
229+ ),
230+ // ChangeNotifierProvider<HistoryService>.value(value: historyService), // Old service removed
231+ ChangeNotifierProvider <HistoryServiceV2 >.value (value: historyServiceV2),
155232 ChangeNotifierProvider <ServerManagementService >.value (
156233 value: serverManagement,
157234 ),
@@ -161,8 +238,6 @@ void main() async {
161238 );
162239}
163240
164-
165-
166241class MainApp extends StatelessWidget {
167242 const MainApp ({super .key});
168243
0 commit comments