Skip to content

Commit

Permalink
get rid of object box since we're resetting the DB instead of migrating
Browse files Browse the repository at this point in the history
  • Loading branch information
lamarios committed Jan 29, 2024
1 parent b2c03da commit 94f77a0
Show file tree
Hide file tree
Showing 39 changed files with 72 additions and 2,547 deletions.
60 changes: 0 additions & 60 deletions lib/db_reset/migration_utils.dart

This file was deleted.

15 changes: 15 additions & 0 deletions lib/db_reset/reset_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:logging/logging.dart';
import 'package:path_provider/path_provider.dart';

final _logs = Logger('Migration utils');

Future<bool> needsReset() async {
var dir = await getApplicationDocumentsDirectory();
var files = dir.listSync();
var count =
files.where((element) => element.path.contains("impuc-data")).length;
_logs.fine('Found legacy folder, we need to reset the app');
return count > 0;
}

Future<void> clearIsar() async {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import 'package:invidious/utils/views/tv/components/tv_button.dart';
import '../../states/reset.dart';

@RoutePage()
class MigrationScreen extends StatelessWidget {
const MigrationScreen({super.key});
class ResetScreen extends StatelessWidget {
const ResetScreen({super.key});

@override
Widget build(BuildContext context) {
Expand All @@ -26,24 +26,42 @@ class MigrationScreen extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
Icons.warning_amber,
size: 50,
),
const SizedBox(
height: 20,
),
const Text(
'In order to keep the application fully open source, the local database backend needs to be switched. Unfortunately there is no simple way to migrate the data.'),
const SizedBox(
height: 20,
),
const Text(
'The application will be reset',
'The application needs to be reset',
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(
height: 20,
),
const Text(
'The application will close once the process if finished'),
'The application will close once the process is finished'),
const SizedBox(
height: 20,
),
isTv
? TvButton(
child: const Text('Reset application'),
onPressed: (context) => cubit.resetDb())
? Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TvButton(
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Text('Reset application'),
),
onPressed: (context) => cubit.resetDb()),
],
)
: FilledButton.tonal(
onPressed: cubit.resetDb,
child: const Text('Reset application'))
Expand Down
28 changes: 16 additions & 12 deletions lib/downloads/models/downloaded_video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import 'dart:io';

import 'package:invidious/videos/models/base_video.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:objectbox/objectbox.dart' as obox;
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;

part 'downloaded_video.g.dart';

@obox.Entity()
const String _downloadFolder = 'downloads';

@JsonSerializable()
class DownloadedVideo extends IdedVideo {
@obox.Id()
@JsonKey(includeFromJson: false, includeToJson: false)
int id = 0;

String title;
String? author;
String? authorUrl;
Expand All @@ -29,23 +26,30 @@ class DownloadedVideo extends IdedVideo {
@override
String get videoId => super.videoId;

@obox.Transient()
@JsonKey(includeFromJson: false, includeToJson: false)
Future<String> get mediaPath async {
Directory dir = await getApplicationDocumentsDirectory();
Directory dir = await _getDownloadFolder();
return "${dir.path}/$videoId.${audioOnly ? 'webm' : 'mp4'}";
}

@obox.Transient()
@JsonKey(includeFromJson: false, includeToJson: false)
Future<String> get thumbnailPath async {
Directory dir = await getApplicationDocumentsDirectory();
Directory dir = await _getDownloadFolder();
return "${dir.path}/$videoId.jpg";
}

Future<Directory> _getDownloadFolder() async {
Directory dir = await getApplicationDocumentsDirectory();
dir = Directory(p.join(dir.path, _downloadFolder));
if (!(await dir.exists())) {
await dir.create(recursive: true);
}

return dir;
}

DownloadedVideo(
{this.id = 0,
required String videoId,
{required String videoId,
required this.title,
this.author,
this.authorUrl,
Expand Down
4 changes: 2 additions & 2 deletions lib/downloads/states/download_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ class DownloadManagerCubit extends Cubit<DownloadManagerState> {
}
} else {
EasyThrottle.throttle(
'download-${video.id}', const Duration(milliseconds: 500), () {
'download-${video.videoId}', const Duration(milliseconds: 500), () {
log.fine(
'Download of video ${video.id}, $count / $total = ${count / total}, Total: ${state.totalProgress}');
'Download of video ${video.videoId}, $count / $total = ${count / total}, Total: ${state.totalProgress}');
var progresses =
Map<String, DownloadProgress>.from(state.downloadProgresses);
var downloadProgress = progresses[video.videoId];
Expand Down
10 changes: 1 addition & 9 deletions lib/home/models/db/home_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:invidious/videos/models/video_in_list.dart';
import 'package:objectbox/objectbox.dart' as obox;

import '../../../downloads/states/download_manager.dart';
import '../../../globals.dart';
Expand Down Expand Up @@ -208,27 +207,20 @@ enum HomeDataSource {
}

// objectBox
@obox.Entity()
@CopyWith(constructor: "_")
@JsonSerializable()
class HomeLayout {
@obox.Id()
@JsonKey(includeFromJson: false, includeToJson: false)
int id = 0;

@obox.Transient()
@JsonKey(includeFromJson: false, includeToJson: false)
List<HomeDataSource> smallSources = [HomeDataSource.popular];

@obox.Transient()
@JsonKey(includeFromJson: false, includeToJson: false)
HomeDataSource bigSource = HomeDataSource.trending;

bool showBigSource = true;

HomeLayout();

HomeLayout._(this.id, this.smallSources, this.bigSource, this.showBigSource);
HomeLayout._(this.smallSources, this.bigSource, this.showBigSource);

String get dbBigSource => bigSource.name;

Expand Down
11 changes: 0 additions & 11 deletions lib/home/models/db/home_layout.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import 'package:invidious/utils/sembast_sqflite_database.dart';
import 'package:invidious/workmanager.dart';
import 'package:logging/logging.dart';

import 'db_reset/migration_utils.dart';
import 'db_reset/reset_utils.dart';
import 'settings/models/db/app_logs.dart';

const brandColor = Color(0xFF4f0096);
Expand Down Expand Up @@ -59,7 +59,7 @@ Future<void> main() async {
db = await SembastSqfDb.create();
await fileDb.syncWithDb();

final needsDbMigration = await needsMigration();
final needsDbMigration = await needsReset();
late final bool hasServer;
try {
await db.getCurrentlySelectedServer();
Expand Down
7 changes: 0 additions & 7 deletions lib/notifications/models/db/channel_notifications.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:objectbox/objectbox.dart' as obox;

part 'channel_notifications.g.dart';

@obox.Entity()
@JsonSerializable()
class ChannelNotification {
@obox.Id()
@JsonKey(includeFromJson: false, includeToJson: false)
int id = 0;

@obox.Unique(onConflict: obox.ConflictStrategy.replace)
final String channelId;

String lastSeenVideoId;
Expand Down
7 changes: 0 additions & 7 deletions lib/notifications/models/db/playlist_notifications.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:objectbox/objectbox.dart' as obox;

part 'playlist_notifications.g.dart';

@obox.Entity()
@JsonSerializable()
class PlaylistNotification {
@obox.Id()
@JsonKey(includeFromJson: false, includeToJson: false)
int id = 0;

@obox.Unique(onConflict: obox.ConflictStrategy.replace)
final String playlistId;

int lastVideoCount = 0;
Expand Down
6 changes: 0 additions & 6 deletions lib/notifications/models/db/subscription_notifications.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:objectbox/objectbox.dart' as obox;

part 'subscription_notifications.g.dart';

@obox.Entity()
@JsonSerializable()
class SubscriptionNotification {
@obox.Id()
@JsonKey(includeFromJson: false, includeToJson: false)
int id = 0;

String lastSeenVideoId;

int timestamp;
Expand Down
Loading

0 comments on commit 94f77a0

Please sign in to comment.