Added archive and unarchive

This commit is contained in:
Juan Gilsanz Polo
2024-02-26 22:14:41 +01:00
parent fa0441a0e4
commit 5faf08e597
9 changed files with 73 additions and 35 deletions

View File

@@ -118,8 +118,9 @@ class Bookmarks extends _$Bookmarks {
bookmark: bookmark,
apiClient: ref.read(apiClientProvider)!,
);
if (result != null) {
state.bookmarks = state.bookmarks.map((b) => b.id == result.id ? result : b).toList();
if (result == true) {
// On this case the bookmark always will pass from unarchived to archived
state.bookmarks = state.bookmarks.where((b) => b.id != bookmark.id).toList();
ref.notifyListeners();
}
}

View File

@@ -75,30 +75,21 @@ class BookmarkCommonFunctions {
}
}
static Future<Bookmark?> archiveUnarchive<T>({
static Future<bool> archiveUnarchive<T>({
required AutoDisposeNotifierProviderRef<T> ref,
required Bookmark bookmark,
required ApiClientService apiClient,
}) async {
final processModal = ProcessModal();
processModal.open(
bookmark.unread == true
? t.bookmarks.bookmarkOptions.archivingBookmark
: t.bookmarks.bookmarkOptions.unarchivingBookmark,
bookmark.isArchived == true
? t.bookmarks.bookmarkOptions.unarchivingBookmark
: t.bookmarks.bookmarkOptions.archivingBookmark,
);
final result = await apiClient.putUpdateBookmark(
bookmark.id!,
SetBookmarkData(
url: bookmark.url!,
description: bookmark.description ?? '',
unread: bookmark.unread ?? false,
shared: bookmark.shared ?? false,
isArchived: bookmark.isArchived == true ? false : true,
tagNames: bookmark.tagNames?.join(",") ?? '',
title: bookmark.title ?? '',
),
);
final result = bookmark.isArchived == true
? await apiClient.postUnrchiveBookmark(bookmark.id!)
: await apiClient.postArchiveBookmark(bookmark.id!);
processModal.close();
if (result.successful == true) {
@@ -108,7 +99,6 @@ class BookmarkCommonFunctions {
: t.bookmarks.bookmarkOptions.bookmarkArchivedSuccessfully,
color: Colors.green,
);
return result.content;
} else {
ref.read(snackbarProvider.notifier).showSnacbkar(
label: bookmark.isArchived == true
@@ -116,7 +106,7 @@ class BookmarkCommonFunctions {
: t.bookmarks.bookmarkOptions.bookmarkNotArchived,
color: Colors.red,
);
return null;
}
return result.successful;
}
}

View File

@@ -129,17 +129,18 @@ class SearchBookmarks extends _$SearchBookmarks {
}
void archiveUnarchive(Bookmark bookmark) async {
final result = await BookmarkCommonFunctions.markAsReadUnread<SearchBookmarksModel>(
final result = await BookmarkCommonFunctions.archiveUnarchive<SearchBookmarksModel>(
ref: ref,
bookmark: bookmark,
apiClient: ref.read(apiClientProvider)!,
);
if (result != null) {
state.bookmarks = state.bookmarks.map((b) => b.id == result.id ? result : b).toList();
if (result == true) {
// On this case the bookmark always will pass from unarchived to archived
state.bookmarks = state.bookmarks.where((b) => b.id != bookmark.id).toList();
ref.notifyListeners();
ref
.read(bookmarksProvider.notifier)
.setBookmarks(ref.read(bookmarksProvider).bookmarks.map((b) => b.id == result.id ? result : b).toList());
.setBookmarks(ref.read(bookmarksProvider).bookmarks.where((b) => b.id != bookmark.id).toList());
}
}
}

View File

@@ -20,12 +20,14 @@ class BookmarkItem extends ConsumerWidget {
final Bookmark bookmark;
final void Function(Bookmark bookmark) onDelete;
final void Function(Bookmark bookmark) onReadUnread;
final void Function(Bookmark bookmark) onArchiveUnarchive;
const BookmarkItem({
Key? key,
required this.bookmark,
required this.onDelete,
required this.onReadUnread,
required this.onArchiveUnarchive,
}) : super(key: key);
@override
@@ -45,7 +47,7 @@ class BookmarkItem extends ConsumerWidget {
groupTag: ConfigOptions.slidableGroupTag,
startActionPane: ActionPane(
motion: const DrawerMotion(),
extentRatio: 0.5,
extentRatio: 0.75,
children: [
SlidableAction(
onPressed: (_) => onReadUnread(bookmark),
@@ -54,13 +56,13 @@ class BookmarkItem extends ConsumerWidget {
icon: bookmark.unread == true ? Icons.mark_email_read_rounded : Icons.mark_as_unread_rounded,
padding: const EdgeInsets.all(4),
),
// SlidableAction(
// onPressed: (ctx) => {},
// backgroundColor: Colors.grey,
// label: t.bookmarks.bookmarkOptions.archive,
// icon: Icons.archive_rounded,
// padding: const EdgeInsets.all(4),
// ),
SlidableAction(
onPressed: (ctx) => onArchiveUnarchive(bookmark),
backgroundColor: Colors.grey,
label: t.bookmarks.bookmarkOptions.archive,
icon: Icons.archive_rounded,
padding: const EdgeInsets.all(4),
),
SlidableAction(
onPressed: (ctx) => Share.shareUri(Uri.parse(bookmark.url!)),
backgroundColor: Colors.orange,

View File

@@ -134,6 +134,7 @@ class BookmarksScreen extends ConsumerWidget {
onDelete: ref.read(bookmarksProvider.notifier).deleteBookmark,
),
),
onArchiveUnarchive: ref.read(bookmarksProvider.notifier).archiveUnarchive,
);
},
),

View File

@@ -124,6 +124,7 @@ class SearchBookmarksScreen extends ConsumerWidget {
onDelete: ref.read(searchBookmarksProvider.notifier).deleteBookmark,
),
),
onArchiveUnarchive: ref.read(searchBookmarksProvider.notifier).archiveUnarchive,
);
},
),

View File

@@ -122,13 +122,14 @@ class TagBookmarks extends _$TagBookmarks {
}
void archiveUnarchive(Bookmark bookmark) async {
final result = await BookmarkCommonFunctions.markAsReadUnread<TagBookmarksModel>(
final result = await BookmarkCommonFunctions.archiveUnarchive<TagBookmarksModel>(
ref: ref,
bookmark: bookmark,
apiClient: ref.read(apiClientProvider)!,
);
if (result != null) {
state.bookmarks = state.bookmarks.map((b) => b.id == result.id ? result : b).toList();
if (result == true) {
// On this case the bookmark always will pass from unarchived to archived
state.bookmarks = state.bookmarks.where((b) => b.id != bookmark.id).toList();
ref.notifyListeners();
}
}

View File

@@ -139,6 +139,7 @@ class TagBookmarksScreenState extends ConsumerState<TagBookmarksScreen> {
onDelete: ref.read(tagBookmarksProvider.notifier).deleteBookmark,
),
),
onArchiveUnarchive: ref.read(tagBookmarksProvider.notifier).archiveUnarchive,
);
},
),

View File

@@ -46,6 +46,8 @@ class ApiClientService {
successful: true,
content: BookmarksResponse.fromJson(response.data),
);
} on DioException {
return const ApiResponse(successful: false);
} catch (e, stackTrace) {
Sentry.captureException(e, stackTrace: stackTrace);
return const ApiResponse(successful: false);
@@ -64,6 +66,8 @@ class ApiClientService {
successful: true,
content: CheckBookmark.fromJson(response.data),
);
} on DioException {
return const ApiResponse(successful: false);
} catch (e, stackTrace) {
Sentry.captureException(e, stackTrace: stackTrace);
return const ApiResponse(successful: false);
@@ -80,6 +84,8 @@ class ApiClientService {
successful: true,
content: Bookmark.fromJson(response.data),
);
} on DioException {
return const ApiResponse(successful: false);
} catch (e, stackTrace) {
Sentry.captureException(e, stackTrace: stackTrace);
return const ApiResponse(successful: false);
@@ -100,6 +106,8 @@ class ApiClientService {
successful: true,
content: TagsResponse.fromJson(response.data),
);
} on DioException {
return const ApiResponse(successful: false);
} catch (e, stackTrace) {
Sentry.captureException(e, stackTrace: stackTrace);
return const ApiResponse(successful: false);
@@ -116,6 +124,8 @@ class ApiClientService {
successful: true,
content: Tag.fromJson(response.data),
);
} on DioException {
return const ApiResponse(successful: false);
} catch (e, stackTrace) {
Sentry.captureException(e, stackTrace: stackTrace);
return const ApiResponse(successful: false);
@@ -129,6 +139,8 @@ class ApiClientService {
successful: true,
content: Tag.fromJson(response.data),
);
} on DioException {
return const ApiResponse(successful: false);
} catch (e, stackTrace) {
Sentry.captureException(e, stackTrace: stackTrace);
return const ApiResponse(successful: false);
@@ -139,6 +151,8 @@ class ApiClientService {
try {
await dioInstance.delete("/bookmarks/$bookmarkId/");
return const ApiResponse(successful: true);
} on DioException {
return const ApiResponse(successful: false);
} catch (e, stackTrace) {
Sentry.captureException(e, stackTrace: stackTrace);
return const ApiResponse(successful: false);
@@ -155,6 +169,32 @@ class ApiClientService {
successful: true,
content: Bookmark.fromJson(result.data),
);
} on DioException {
return const ApiResponse(successful: false);
} catch (e, stackTrace) {
Sentry.captureException(e, stackTrace: stackTrace);
return const ApiResponse(successful: false);
}
}
Future<ApiResponse<bool>> postArchiveBookmark(int bookmarkId) async {
try {
await dioInstance.post("/bookmarks/$bookmarkId/archive/");
return const ApiResponse(successful: true);
} on DioException {
return const ApiResponse(successful: false);
} catch (e, stackTrace) {
Sentry.captureException(e, stackTrace: stackTrace);
return const ApiResponse(successful: false);
}
}
Future<ApiResponse<bool>> postUnrchiveBookmark(int bookmarkId) async {
try {
await dioInstance.post("/bookmarks/$bookmarkId/unarchive/");
return const ApiResponse(successful: true);
} on DioException {
return const ApiResponse(successful: false);
} catch (e, stackTrace) {
Sentry.captureException(e, stackTrace: stackTrace);
return const ApiResponse(successful: false);