mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 16:05:18 +00:00
fix(database): add conditional imports for web/native database connections
- Add connection_web.dart for IndexedDB via drift - Add connection_native.dart for sqlite3 - Remove direct platform-specific imports from database.dart - Enable web compilation without FFI dependencies
This commit is contained in:
parent
e830a8bf6c
commit
cba8016e94
25
lib/models/database/connection_native.dart
Normal file
25
lib/models/database/connection_native.dart
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
import 'package:drift/drift.dart';
|
||||||
|
import 'package:drift/native.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
import 'package:path/path.dart' as p;
|
||||||
|
import 'package:sqlite3/sqlite3.dart';
|
||||||
|
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';
|
||||||
|
|
||||||
|
DatabaseConnection connect() {
|
||||||
|
return DatabaseConnection.delayed(Future(() async {
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
|
||||||
|
}
|
||||||
|
|
||||||
|
final cacheBase = (await getTemporaryDirectory()).path;
|
||||||
|
// Make sqlite3 pick a more suitable location for temporary files - the
|
||||||
|
// one from the system may be inaccessible due to sandboxing.
|
||||||
|
// We can't access /tmp on Android, which sqlite3 would try by default.
|
||||||
|
sqlite3.tempDirectory = cacheBase;
|
||||||
|
|
||||||
|
final appDocDir = await getApplicationDocumentsDirectory();
|
||||||
|
final dbPath = p.join(appDocDir.path, 'spotube.db');
|
||||||
|
return NativeDatabase(File(dbPath));
|
||||||
|
}));
|
||||||
|
}
|
8
lib/models/database/connection_web.dart
Normal file
8
lib/models/database/connection_web.dart
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import 'package:drift/drift.dart';
|
||||||
|
import 'package:drift/web.dart';
|
||||||
|
|
||||||
|
DatabaseConnection connect() {
|
||||||
|
// IndexedDB vía drift para web
|
||||||
|
final db = WebDatabase('tunestream_db');
|
||||||
|
return DatabaseConnection.fromExecutor(db);
|
||||||
|
}
|
@ -1,13 +1,10 @@
|
|||||||
library database;
|
library database;
|
||||||
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
|
||||||
|
|
||||||
import 'package:drift/drift.dart';
|
import 'package:drift/drift.dart';
|
||||||
import 'package:encrypt/encrypt.dart';
|
import 'package:encrypt/encrypt.dart';
|
||||||
import 'package:media_kit/media_kit.dart' hide Track;
|
import 'package:media_kit/media_kit.dart' hide Track;
|
||||||
import 'package:path/path.dart';
|
|
||||||
import 'package:path_provider/path_provider.dart';
|
|
||||||
import 'package:shadcn_flutter/shadcn_flutter.dart' show ThemeMode, Colors;
|
import 'package:shadcn_flutter/shadcn_flutter.dart' show ThemeMode, Colors;
|
||||||
import 'package:spotify/spotify.dart' hide Playlist;
|
import 'package:spotify/spotify.dart' hide Playlist;
|
||||||
import 'package:spotube/models/database/database.steps.dart';
|
import 'package:spotube/models/database/database.steps.dart';
|
||||||
@ -17,12 +14,13 @@ import 'package:spotube/services/kv_store/kv_store.dart';
|
|||||||
import 'package:spotube/services/sourced_track/enums.dart';
|
import 'package:spotube/services/sourced_track/enums.dart';
|
||||||
import 'package:flutter/widgets.dart' hide Table, Key, View;
|
import 'package:flutter/widgets.dart' hide Table, Key, View;
|
||||||
import 'package:spotube/modules/settings/color_scheme_picker_dialog.dart';
|
import 'package:spotube/modules/settings/color_scheme_picker_dialog.dart';
|
||||||
import 'package:drift/native.dart';
|
|
||||||
import 'package:spotube/services/youtube_engine/newpipe_engine.dart';
|
import 'package:spotube/services/youtube_engine/newpipe_engine.dart';
|
||||||
import 'package:spotube/services/youtube_engine/youtube_explode_engine.dart';
|
import 'package:spotube/services/youtube_engine/youtube_explode_engine.dart';
|
||||||
import 'package:spotube/services/youtube_engine/yt_dlp_engine.dart';
|
import 'package:spotube/services/youtube_engine/yt_dlp_engine.dart';
|
||||||
import 'package:sqlite3/sqlite3.dart';
|
|
||||||
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';
|
// Conditional import para diferentes plataformas
|
||||||
|
import 'connection_native.dart'
|
||||||
|
if (dart.library.html) 'connection_web.dart';
|
||||||
|
|
||||||
part 'database.g.dart';
|
part 'database.g.dart';
|
||||||
|
|
||||||
@ -126,26 +124,6 @@ class AppDatabase extends _$AppDatabase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LazyDatabase _openConnection() {
|
DatabaseConnection _openConnection() {
|
||||||
// the LazyDatabase util lets us find the right location for the file async.
|
return connect();
|
||||||
return LazyDatabase(() async {
|
|
||||||
// put the database file, called db.sqlite here, into the documents folder
|
|
||||||
// for your app.
|
|
||||||
final dbFolder = await getApplicationSupportDirectory();
|
|
||||||
final file = File(join(dbFolder.path, 'db.sqlite'));
|
|
||||||
|
|
||||||
// Also work around limitations on old Android versions
|
|
||||||
if (Platform.isAndroid) {
|
|
||||||
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sqlite3 pick a more suitable location for temporary files - the
|
|
||||||
// one from the system may be inaccessible due to sandboxing.
|
|
||||||
final cacheBase = (await getTemporaryDirectory()).path;
|
|
||||||
// We can't access /tmp on Android, which sqlite3 would try by default.
|
|
||||||
// Explicitly tell it about the correct temporary directory.
|
|
||||||
sqlite3.tempDirectory = cacheBase;
|
|
||||||
|
|
||||||
return NativeDatabase.createInBackground(file);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user