Skip to content
Draft
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
115 changes: 115 additions & 0 deletions lib/account/models/custom_sort_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import 'package:flutter/foundation.dart';

import 'package:drift/drift.dart';
import 'package:lemmy_api_client/v3.dart';

import 'package:thunder/core/database/database.dart';
import 'package:thunder/core/database/type_converters.dart';
import 'package:thunder/main.dart';

class CustomSortType {
/// The type of sort (community/feed)
final SortType sortType;

/// The account id
final int accountId;

/// The community id
final int? communityId;

/// The feed type
final ListingType? feedType;

const CustomSortType({
required this.sortType,
required this.accountId,
this.communityId,
this.feedType,
});

CustomSortType copyWith({
SortType? sortType,
int? accountId,
int? communityId,
ListingType? feedType,
}) =>
CustomSortType(
sortType: sortType ?? this.sortType,
accountId: accountId ?? this.accountId,
communityId: communityId ?? this.communityId,
feedType: feedType ?? this.feedType,
);

/// Create or update a custom sort type in the db
static Future<CustomSortType?> upsertCustomSortType(CustomSortType customSortType) async {
try {
final existingCustomSortType = await (database.select(database.customSortType)
..where((t) => t.accountId.equals(customSortType.accountId))
..where((t) => customSortType.communityId == null ? t.communityId.isNull() : t.communityId.equals(customSortType.communityId!))
..where((t) => customSortType.feedType == null ? t.feedType.isNull() : t.feedType.equals(const ListingTypeConverter().toSql(customSortType.feedType!))))
.getSingleOrNull();

if (existingCustomSortType == null) {
final id = await database.into(database.customSortType).insert(
CustomSortTypeCompanion.insert(
sortType: customSortType.sortType,
accountId: customSortType.accountId,
communityId: Value(customSortType.communityId),
feedType: Value(customSortType.feedType),
),
);
return customSortType;
}

await database.update(database.customSortType).replace(
CustomSortTypeCompanion(
id: Value(existingCustomSortType.id),
sortType: Value(customSortType.sortType),
accountId: Value(customSortType.accountId),
communityId: Value(customSortType.communityId),
feedType: Value(customSortType.feedType),
),
);
return customSortType;
} catch (e) {
debugPrint(e.toString());
return null;
}
}

/// Retrieve a custom sort type from the db
static Future<CustomSortType?> fetchCustomSortType(int accountId, int? communityId, ListingType? feedType) async {
try {
final customSortType = await (database.select(database.customSortType)
..where((t) => t.accountId.equals(accountId))
..where((t) => communityId == null ? t.communityId.isNull() : t.communityId.equals(communityId))
..where((t) => feedType == null ? t.feedType.isNull() : t.feedType.equals(const ListingTypeConverter().toSql(feedType))))
.getSingleOrNull();

if (customSortType == null) return null;

return CustomSortType(
sortType: customSortType.sortType,
accountId: customSortType.accountId,
communityId: customSortType.communityId,
feedType: customSortType.feedType,
);
} catch (e) {
debugPrint(e.toString());
return null;
}
}

/// Delete a custom sort type from the db
static Future<void> deleteCustomSortType(int accountId, int? communityId, ListingType? feedType) async {
try {
await (database.delete(database.customSortType)
..where((t) => t.accountId.equals(accountId))
..where((t) => communityId == null ? t.communityId.isNull() : t.communityId.equals(communityId))
..where((t) => feedType == null ? t.feedType.isNull() : t.feedType.equals(const ListingTypeConverter().toSql(feedType))))
.go();
} catch (e) {
debugPrint(e.toString());
}
}
}
3 changes: 0 additions & 3 deletions lib/community/widgets/community_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ class _CommunityDrawerState extends State<CommunityDrawer> {
context.read<FeedBloc>().add(
FeedFetchedEvent(
feedType: FeedType.community,
sortType: authState.getSiteResponse?.myUser?.localUserView.localUser.defaultSortType ?? thunderState.sortTypeForInstance,
communityId: community.id,
reset: true,
),
Expand Down Expand Up @@ -336,7 +335,6 @@ class FavoriteCommunities extends StatelessWidget {
context.read<FeedBloc>().add(
FeedFetchedEvent(
feedType: FeedType.community,
sortType: authState.getSiteResponse?.myUser?.localUserView.localUser.defaultSortType ?? thunderState.sortTypeForInstance,
communityId: community.id,
reset: true,
),
Expand Down Expand Up @@ -397,7 +395,6 @@ class ModeratedCommunities extends StatelessWidget {
context.read<FeedBloc>().add(
FeedFetchedEvent(
feedType: FeedType.community,
sortType: authState.getSiteResponse?.myUser?.localUserView.localUser.defaultSortType ?? thunderState.sortTypeForInstance,
communityId: community.id,
reset: true,
),
Expand Down
2 changes: 1 addition & 1 deletion lib/core/auth/bloc/auth_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {

/// This event occurs whenever you switch to a different authenticated account
on<SwitchAccount>((event, emit) async {
emit(state.copyWith(status: AuthStatus.loading, isLoggedIn: false, reload: event.reload));
emit(state.copyWith(status: AuthStatus.loading, isLoggedIn: false, account: null, reload: event.reload));

Account? account = await Account.fetchAccount(event.accountId);
if (account == null) return emit(state.copyWith(status: AuthStatus.success, account: null, isLoggedIn: false));
Expand Down
17 changes: 15 additions & 2 deletions lib/core/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:flutter/material.dart' hide Table;
import 'package:flutter_file_dialog/flutter_file_dialog.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqlite3/sqlite3.dart';
Expand All @@ -15,12 +16,12 @@ import 'package:thunder/drafts/draft_type.dart';

part 'database.g.dart';

@DriftDatabase(tables: [Accounts, Favorites, LocalSubscriptions, UserLabels, Drafts])
@DriftDatabase(tables: [Accounts, Favorites, LocalSubscriptions, UserLabels, Drafts, CustomSortType])
class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openConnection());

@override
int get schemaVersion => 3;
int get schemaVersion => 4;

@override
MigrationStrategy get migration => MigrationStrategy(
Expand All @@ -39,6 +40,12 @@ class AppDatabase extends _$AppDatabase {
await migrator.createTable(drafts);
}

// If we are migrating from 3 or lower to anything higher
if (from <= 3 && to > 3) {
// Create the CustomSortType table
await migrator.createTable(customSortType);
}

// --- DOWNGRADES ---

// If we are downgrading from 2 or higher to 1
Expand All @@ -52,6 +59,12 @@ class AppDatabase extends _$AppDatabase {
// Delete the Drafts table
await migrator.deleteTable('drafts');
}

// If we are downgrading from 4 or higher to 3 or lower
if (from >= 4 && to <= 3) {
// Delete the CustomSortType table
await migrator.deleteTable('custom_sort_type');
}
},
);
}
Expand Down
Loading