Added share and unshare bookmarks

This commit is contained in:
Juan Gilsanz Polo
2024-02-27 18:20:00 +01:00
parent 5ab3b59152
commit 3f298cc22d
17 changed files with 390 additions and 9 deletions

View File

@@ -128,4 +128,17 @@ class Bookmarks extends _$Bookmarks {
ref.notifyListeners();
}
}
void shareUnshare(Bookmark bookmark) async {
final result = await BookmarkCommonFunctions.shareUnshare<BookmarksModel>(
scaffoldMessengerKey: ScaffoldMessengerKeys.bookmarks,
ref: ref,
bookmark: bookmark,
apiClient: ref.read(apiClientProvider)!,
);
if (result != null) {
state.bookmarks = state.bookmarks.map((b) => b.id == result.id ? result : b).toList();
ref.notifyListeners();
}
}
}

View File

@@ -172,7 +172,7 @@ final bookmarksRequestLoadMoreProvider =
);
typedef BookmarksRequestLoadMoreRef = AutoDisposeFutureProviderRef<void>;
String _$bookmarksHash() => r'84a236ccce48c0a0069ca2b754da59ff88b17ef2';
String _$bookmarksHash() => r'602a50d02cde9a67335372659f8e2f656ac21995';
/// See also [Bookmarks].
@ProviderFor(Bookmarks)

View File

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:linkdy/i18n/strings.g.dart';
import 'package:linkdy/models/data/patch_bookmark_data.dart';
import 'package:linkdy/models/data/bookmarks.dart';
import 'package:linkdy/utils/snackbar.dart';
import 'package:linkdy/models/data/set_bookmark_data.dart';
@@ -121,4 +122,44 @@ class BookmarkCommonFunctions {
}
return result.successful;
}
static Future<Bookmark?> shareUnshare<T>({
required AutoDisposeNotifierProviderRef<T> ref,
required Bookmark bookmark,
required ApiClientService apiClient,
required GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey,
}) async {
final processModal = ProcessModal();
processModal.open(
bookmark.shared == true ? t.bookmarks.shareOptions.unsharingBookmark : t.bookmarks.shareOptions.sharingBookmark,
);
final result = await apiClient.patchUpdateBookmark(
bookmark.id!,
PatchBookmarkData(
shared: !bookmark.shared!,
),
);
processModal.close();
if (result.successful == true) {
showSnackbar(
key: scaffoldMessengerKey,
label: bookmark.isArchived == true
? t.bookmarks.shareOptions.bookmarkUnsharedSuccessfully
: t.bookmarks.shareOptions.bookmarkSharedSuccessfully,
color: Colors.green,
);
return result.content;
} else {
showSnackbar(
key: scaffoldMessengerKey,
label: bookmark.isArchived == true
? t.bookmarks.shareOptions.bookmarkNotUnshared
: t.bookmarks.shareOptions.bookmarkNotShared,
color: Colors.red,
);
return null;
}
}
}

View File

@@ -147,4 +147,20 @@ class SearchBookmarks extends _$SearchBookmarks {
.setBookmarks(ref.read(bookmarksProvider).bookmarks.where((b) => b.id != bookmark.id).toList());
}
}
void shareUnshare(Bookmark bookmark) async {
final result = await BookmarkCommonFunctions.shareUnshare<SearchBookmarksModel>(
scaffoldMessengerKey: ScaffoldMessengerKeys.search,
ref: ref,
bookmark: bookmark,
apiClient: ref.read(apiClientProvider)!,
);
if (result != null) {
state.bookmarks = state.bookmarks.map((b) => b.id == result.id ? result : b).toList();
ref.notifyListeners();
ref
.read(bookmarksProvider.notifier)
.setBookmarks(ref.read(bookmarksProvider).bookmarks.map((b) => b.id == result.id ? result : b).toList());
}
}
}

View File

@@ -174,7 +174,7 @@ final fetchSearchBookmarksLoadMoreProvider =
);
typedef FetchSearchBookmarksLoadMoreRef = AutoDisposeFutureProviderRef<void>;
String _$searchBookmarksHash() => r'c7c8394ae9ad71f8106a3b73100b88e09c154e13';
String _$searchBookmarksHash() => r'bdb0eedf3ebdde4b2cb4795efe61ce42e4c85d00';
/// See also [SearchBookmarks].
@ProviderFor(SearchBookmarks)

View File

@@ -1,10 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:share_plus/share_plus.dart';
import 'package:skeletonizer/skeletonizer.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:linkdy/screens/bookmarks/ui/share_options_modal.dart';
import 'package:linkdy/screens/bookmarks/provider/favicon_loader.provider.dart';
import 'package:linkdy/config/options.dart';
@@ -22,6 +22,7 @@ class BookmarkItem extends ConsumerWidget {
final void Function(Bookmark bookmark) onReadUnread;
final void Function(Bookmark bookmark) onArchiveUnarchive;
final void Function(Bookmark bookmark) onEdit;
final void Function(Bookmark bookmark) onShareInternally;
const BookmarkItem({
super.key,
@@ -30,6 +31,7 @@ class BookmarkItem extends ConsumerWidget {
required this.onReadUnread,
required this.onArchiveUnarchive,
required this.onEdit,
required this.onShareInternally,
});
@override
@@ -66,7 +68,10 @@ class BookmarkItem extends ConsumerWidget {
padding: const EdgeInsets.all(4),
),
SlidableAction(
onPressed: (ctx) => Share.shareUri(Uri.parse(bookmark.url!)),
onPressed: (ctx) => showDialog(
context: context,
builder: (ctx) => ShareOptionsModal(bookmark: bookmark, onShareInternally: onShareInternally),
),
backgroundColor: Colors.orange,
label: t.bookmarks.bookmarkOptions.share,
icon: Icons.share_rounded,
@@ -214,7 +219,7 @@ class BookmarkItem extends ConsumerWidget {
),
),
),
if (bookmark.unread == true) ...[
if (bookmark.shared == true) ...[
if (bookmark.tagNames?.isNotEmpty == true)
Container(
width: 1,
@@ -222,6 +227,43 @@ class BookmarkItem extends ConsumerWidget {
margin: const EdgeInsets.symmetric(horizontal: 8),
color: Theme.of(context).colorScheme.tertiary.withOpacity(0.3),
),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 6,
vertical: 2,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Theme.of(context).colorScheme.primaryContainer,
),
child: Row(
children: [
Icon(
Icons.share_rounded,
size: 12,
color: Theme.of(context).colorScheme.onPrimaryContainer,
),
const SizedBox(width: 4),
Text(
t.bookmarks.bookmarkOptions.shared,
style: TextStyle(
fontSize: 10,
color: Theme.of(context).colorScheme.onPrimaryContainer,
fontWeight: FontWeight.w700,
),
),
],
),
),
],
if (bookmark.unread == true) ...[
if (bookmark.tagNames?.isNotEmpty == true || bookmark.shared == true)
Container(
width: 1,
height: 12,
margin: const EdgeInsets.symmetric(horizontal: 8),
color: Theme.of(context).colorScheme.tertiary.withOpacity(0.3),
),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 6,
@@ -252,7 +294,7 @@ class BookmarkItem extends ConsumerWidget {
),
],
if (bookmark.dateModified != null) ...[
if (bookmark.unread == true)
if (bookmark.unread == true || bookmark.shared == true || bookmark.tagNames?.isNotEmpty == true)
Container(
width: 1,
height: 12,

View File

@@ -132,6 +132,7 @@ class BookmarksScreen extends ConsumerWidget {
),
),
onArchiveUnarchive: ref.read(bookmarksProvider.notifier).archiveUnarchive,
onShareInternally: ref.read(bookmarksProvider.notifier).shareUnshare,
onEdit: (b) => openBookmarkFormModal(context: context, width: width, bookmark: b),
);
},

View File

@@ -140,6 +140,7 @@ class SearchBookmarksModal extends ConsumerWidget {
),
),
onArchiveUnarchive: ref.read(searchBookmarksProvider.notifier).archiveUnarchive,
onShareInternally: ref.read(searchBookmarksProvider.notifier).shareUnshare,
onEdit: (b) => openBookmarkFormModal(context: context, width: width, bookmark: b),
);
},

View File

@@ -0,0 +1,76 @@
import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';
import 'package:linkdy/widgets/custom_list_tile.dart';
import 'package:linkdy/i18n/strings.g.dart';
import 'package:linkdy/models/data/bookmarks.dart';
class ShareOptionsModal extends StatelessWidget {
final Bookmark bookmark;
final void Function(Bookmark bookmark) onShareInternally;
const ShareOptionsModal({
super.key,
required this.bookmark,
required this.onShareInternally,
});
@override
Widget build(BuildContext context) {
return AlertDialog(
scrollable: true,
contentPadding: const EdgeInsets.symmetric(horizontal: 0, vertical: 24),
title: Column(
children: [
Icon(
Icons.share_rounded,
size: 24,
color: Theme.of(context).colorScheme.secondary,
),
const SizedBox(height: 16),
Text(t.bookmarks.shareOptions.shareOptions),
],
),
content: Column(
children: [
CustomListTile(
icon: Icons.input_rounded,
title: bookmark.shared == true
? t.bookmarks.shareOptions.unshareInternally
: t.bookmarks.shareOptions.shareInternally,
subtitle: bookmark.shared == true
? t.bookmarks.shareOptions.unshareInternallyDescription
: t.bookmarks.shareOptions.shareInternallyDescription,
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
onTap: () {
Navigator.pop(context);
onShareInternally(bookmark);
},
),
CustomListTile(
icon: Icons.output_rounded,
title: t.bookmarks.shareOptions.shareExternally,
subtitle: t.bookmarks.shareOptions.shareExternallyDescription,
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
onTap: () {
Navigator.pop(context);
Share.shareUri(Uri.parse(bookmark.url!));
},
),
],
),
actions: [
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(t.generic.close),
),
],
),
],
);
}
}

View File

@@ -137,4 +137,17 @@ class TagBookmarks extends _$TagBookmarks {
ref.notifyListeners();
}
}
void shareUnshare(Bookmark bookmark) async {
final result = await BookmarkCommonFunctions.shareUnshare<TagBookmarksModel>(
scaffoldMessengerKey: ScaffoldMessengerKeys.tagBookmarks,
ref: ref,
bookmark: bookmark,
apiClient: ref.read(apiClientProvider)!,
);
if (result != null) {
state.bookmarks = state.bookmarks.map((b) => b.id == result.id ? result : b).toList();
ref.notifyListeners();
}
}
}

View File

@@ -206,7 +206,7 @@ final tagBookmarksRequestLoadMoreProvider =
);
typedef TagBookmarksRequestLoadMoreRef = AutoDisposeFutureProviderRef<void>;
String _$tagBookmarksHash() => r'd5004cac68d86490b49fc6f722ec819267aedf17';
String _$tagBookmarksHash() => r'35a9ed7751c2d7e9644907f969fb03950b7b9a04';
/// See also [TagBookmarks].
@ProviderFor(TagBookmarks)

View File

@@ -147,6 +147,7 @@ class TagBookmarksScreenState extends ConsumerState<TagBookmarksScreen> {
),
),
onArchiveUnarchive: ref.read(tagBookmarksProvider.notifier).archiveUnarchive,
onShareInternally: ref.read(tagBookmarksProvider.notifier).shareUnshare,
onEdit: (b) => openBookmarkFormModal(context: context, width: width, bookmark: b),
);
},