Skip to content

Commit b51e720

Browse files
committed
Create new database system to store data
1 parent 1bdd9b2 commit b51e720

14 files changed

Lines changed: 1828 additions & 694 deletions

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ SETTINGS_SCREEN_UPDATE_SUMMARY.md
8585
WALLPAPER_COLORS_PERSISTENCE_FIX.md
8686
CLOUDFLARE_WARP_INTEGRATION.md
8787
WARP_SETUP_GUIDE.md
88+
DATABASE_LOCK_FIX.md
89+
IMAGE_LOADING_FIX.md
90+
IMPLEMENTATION_GUIDE.md
91+
MIGRATION_COMPLETE.md
92+
MIGRATION_URL_FIX.md
93+
NOVEL_STORAGE_MIGRATION.md

lib/debug_migration.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import 'dart:convert';
2+
import 'package:flutter/material.dart';
3+
import 'services/preferences_service.dart';
4+
5+
/// Debug utility to examine backup data and fix migration issues
6+
class DebugMigration {
7+
static Future<void> printBackupData() async {
8+
final prefs = PreferencesService();
9+
await prefs.initialize();
10+
11+
print('=== BACKUP DATA EXAMINATION ===\n');
12+
13+
// Get backup bookmarks
14+
final backupBookmarks = prefs.getString('bookmarked_novels_backup');
15+
if (backupBookmarks.isNotEmpty) {
16+
print('📚 BACKUP BOOKMARKS:');
17+
try {
18+
final bookmarksList = jsonDecode(backupBookmarks) as List;
19+
print(' Count: ${bookmarksList.length}');
20+
21+
if (bookmarksList.isNotEmpty) {
22+
print('\n First bookmark sample:');
23+
final firstNovel = bookmarksList[0] as Map<String, dynamic>;
24+
print(' - Title: ${firstNovel['title']}');
25+
print(' - URL: ${firstNovel['url']}');
26+
print(' - Cover URL: ${firstNovel['coverUrl']}');
27+
print(' - Full JSON:');
28+
print(' ${jsonEncode(firstNovel)}');
29+
}
30+
} catch (e) {
31+
print(' ERROR parsing: $e');
32+
}
33+
} else {
34+
print('📚 No backup bookmarks found');
35+
}
36+
37+
print('\n');
38+
39+
// Get backup history
40+
final backupHistory = prefs.getString('reading_history_backup');
41+
if (backupHistory.isNotEmpty) {
42+
print('📖 BACKUP HISTORY:');
43+
try {
44+
final historyList = jsonDecode(backupHistory) as List;
45+
print(' Count: ${historyList.length}');
46+
47+
if (historyList.isNotEmpty) {
48+
print('\n First history sample:');
49+
final firstHistory = historyList[0] as Map<String, dynamic>;
50+
final novel = firstHistory['novel'] as Map<String, dynamic>;
51+
print(' - Title: ${novel['title']}');
52+
print(' - URL: ${novel['url']}');
53+
print(' - Cover URL: ${novel['coverUrl']}');
54+
print(' - Last Chapter: ${firstHistory['lastReadChapter']}');
55+
print(' - Full JSON:');
56+
print(' ${jsonEncode(firstHistory)}');
57+
}
58+
} catch (e) {
59+
print(' ERROR parsing: $e');
60+
}
61+
} else {
62+
print('📖 No backup history found');
63+
}
64+
65+
print('\n=== END BACKUP DATA ===');
66+
}
67+
}

lib/main.dart

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import 'services/notification_service.dart';
99
import 'services/theme_services.dart';
1010
import 'services/language_service.dart';
1111
import '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';
1216
import 'services/proxy_service.dart';
1317
import 'services/http_client.dart';
1418
import 'services/dns_service.dart';
@@ -21,15 +25,12 @@ import 'services/crawler_service.dart';
2125
import 'services/preferences_service.dart';
2226
import '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
3435
import '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+
68136
void 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-
166241
class MainApp extends StatelessWidget {
167242
const MainApp({super.key});
168243

0 commit comments

Comments
 (0)