Add bookmark request
This commit is contained in:
29
lib/models/data/post_bookmark.dart
Normal file
29
lib/models/data/post_bookmark.dart
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
class PostBookmark {
|
||||||
|
final String url;
|
||||||
|
final String title;
|
||||||
|
final String description;
|
||||||
|
final bool isArchived;
|
||||||
|
final bool unread;
|
||||||
|
final bool shared;
|
||||||
|
final List<String> tagNames;
|
||||||
|
|
||||||
|
const PostBookmark({
|
||||||
|
required this.url,
|
||||||
|
required this.title,
|
||||||
|
required this.description,
|
||||||
|
required this.isArchived,
|
||||||
|
required this.unread,
|
||||||
|
required this.shared,
|
||||||
|
required this.tagNames,
|
||||||
|
});
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
"url": url,
|
||||||
|
"title": title,
|
||||||
|
"description": description,
|
||||||
|
"isArchived": isArchived,
|
||||||
|
"unread": unread,
|
||||||
|
"shared": shared,
|
||||||
|
"tagNames": tagNames,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,9 +1,14 @@
|
|||||||
import 'package:expandable/expandable.dart';
|
import 'package:expandable/expandable.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:linkdy/utils/snackbar.dart';
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
|
|
||||||
|
import 'package:linkdy/screens/links/provider/links.provider.dart';
|
||||||
import 'package:linkdy/screens/links/model/add_link.model.dart';
|
import 'package:linkdy/screens/links/model/add_link.model.dart';
|
||||||
|
|
||||||
|
import 'package:linkdy/models/data/post_bookmark.dart';
|
||||||
|
import 'package:linkdy/providers/router_provider.dart';
|
||||||
|
import 'package:linkdy/models/data/bookmarks.dart';
|
||||||
import 'package:linkdy/constants/enums.dart';
|
import 'package:linkdy/constants/enums.dart';
|
||||||
import 'package:linkdy/constants/regexp.dart';
|
import 'package:linkdy/constants/regexp.dart';
|
||||||
import 'package:linkdy/models/api_response.dart';
|
import 'package:linkdy/models/api_response.dart';
|
||||||
@@ -18,6 +23,12 @@ Future<ApiResponse<CheckBookmark>> checkBookmark(CheckBookmarkRef ref, String ur
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@riverpod
|
||||||
|
FutureOr<ApiResponse<Bookmark>> addBookmark(AddBookmarkRef ref, PostBookmark newBookmark) async {
|
||||||
|
final result = await ref.watch(apiClientProviderProvider)!.fetchPostBookmark(newBookmark);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
@riverpod
|
@riverpod
|
||||||
class AddLink extends _$AddLink {
|
class AddLink extends _$AddLink {
|
||||||
@override
|
@override
|
||||||
@@ -63,4 +74,30 @@ class AddLink extends _$AddLink {
|
|||||||
state.markAsUnread = value;
|
state.markAsUnread = value;
|
||||||
ref.notifyListeners();
|
ref.notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void addBookmark() async {
|
||||||
|
final newBookmark = PostBookmark(
|
||||||
|
url: state.urlController.text,
|
||||||
|
title: state.titleController.text != "" ? state.titleController.text : state.checkBookmark?.metadata?.title ?? '',
|
||||||
|
description: state.descriptionController.text != ""
|
||||||
|
? state.descriptionController.text
|
||||||
|
: state.checkBookmark?.metadata?.description ?? '',
|
||||||
|
isArchived: false,
|
||||||
|
unread: state.markAsUnread,
|
||||||
|
shared: false,
|
||||||
|
tagNames: [],
|
||||||
|
);
|
||||||
|
print(newBookmark.toJson());
|
||||||
|
final result = await ref.watch(addBookmarkProvider(newBookmark).future);
|
||||||
|
print(result.content);
|
||||||
|
if (result.successful == true) {
|
||||||
|
ref.invalidate(linksRequestProvider);
|
||||||
|
ref.watch(routerProvider).pop();
|
||||||
|
} else {
|
||||||
|
showSnacbkar(
|
||||||
|
label: "Add bookmark failed",
|
||||||
|
color: Colors.red,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -160,7 +160,137 @@ class _CheckBookmarkProviderElement
|
|||||||
String get url => (origin as CheckBookmarkProvider).url;
|
String get url => (origin as CheckBookmarkProvider).url;
|
||||||
}
|
}
|
||||||
|
|
||||||
String _$addLinkHash() => r'd971275978e113b16d1bc34a2370d9d3feba60b5';
|
String _$addBookmarkHash() => r'94d54330832874c142e417df21522f5faac27f9e';
|
||||||
|
|
||||||
|
/// See also [addBookmark].
|
||||||
|
@ProviderFor(addBookmark)
|
||||||
|
const addBookmarkProvider = AddBookmarkFamily();
|
||||||
|
|
||||||
|
/// See also [addBookmark].
|
||||||
|
class AddBookmarkFamily extends Family<AsyncValue<ApiResponse<Bookmark>>> {
|
||||||
|
/// See also [addBookmark].
|
||||||
|
const AddBookmarkFamily();
|
||||||
|
|
||||||
|
/// See also [addBookmark].
|
||||||
|
AddBookmarkProvider call(
|
||||||
|
PostBookmark newBookmark,
|
||||||
|
) {
|
||||||
|
return AddBookmarkProvider(
|
||||||
|
newBookmark,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
AddBookmarkProvider getProviderOverride(
|
||||||
|
covariant AddBookmarkProvider provider,
|
||||||
|
) {
|
||||||
|
return call(
|
||||||
|
provider.newBookmark,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const Iterable<ProviderOrFamily>? _dependencies = null;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
|
||||||
|
|
||||||
|
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
|
||||||
|
_allTransitiveDependencies;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String? get name => r'addBookmarkProvider';
|
||||||
|
}
|
||||||
|
|
||||||
|
/// See also [addBookmark].
|
||||||
|
class AddBookmarkProvider
|
||||||
|
extends AutoDisposeFutureProvider<ApiResponse<Bookmark>> {
|
||||||
|
/// See also [addBookmark].
|
||||||
|
AddBookmarkProvider(
|
||||||
|
PostBookmark newBookmark,
|
||||||
|
) : this._internal(
|
||||||
|
(ref) => addBookmark(
|
||||||
|
ref as AddBookmarkRef,
|
||||||
|
newBookmark,
|
||||||
|
),
|
||||||
|
from: addBookmarkProvider,
|
||||||
|
name: r'addBookmarkProvider',
|
||||||
|
debugGetCreateSourceHash:
|
||||||
|
const bool.fromEnvironment('dart.vm.product')
|
||||||
|
? null
|
||||||
|
: _$addBookmarkHash,
|
||||||
|
dependencies: AddBookmarkFamily._dependencies,
|
||||||
|
allTransitiveDependencies:
|
||||||
|
AddBookmarkFamily._allTransitiveDependencies,
|
||||||
|
newBookmark: newBookmark,
|
||||||
|
);
|
||||||
|
|
||||||
|
AddBookmarkProvider._internal(
|
||||||
|
super._createNotifier, {
|
||||||
|
required super.name,
|
||||||
|
required super.dependencies,
|
||||||
|
required super.allTransitiveDependencies,
|
||||||
|
required super.debugGetCreateSourceHash,
|
||||||
|
required super.from,
|
||||||
|
required this.newBookmark,
|
||||||
|
}) : super.internal();
|
||||||
|
|
||||||
|
final PostBookmark newBookmark;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Override overrideWith(
|
||||||
|
FutureOr<ApiResponse<Bookmark>> Function(AddBookmarkRef provider) create,
|
||||||
|
) {
|
||||||
|
return ProviderOverride(
|
||||||
|
origin: this,
|
||||||
|
override: AddBookmarkProvider._internal(
|
||||||
|
(ref) => create(ref as AddBookmarkRef),
|
||||||
|
from: from,
|
||||||
|
name: null,
|
||||||
|
dependencies: null,
|
||||||
|
allTransitiveDependencies: null,
|
||||||
|
debugGetCreateSourceHash: null,
|
||||||
|
newBookmark: newBookmark,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
AutoDisposeFutureProviderElement<ApiResponse<Bookmark>> createElement() {
|
||||||
|
return _AddBookmarkProviderElement(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(Object other) {
|
||||||
|
return other is AddBookmarkProvider && other.newBookmark == newBookmark;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode {
|
||||||
|
var hash = _SystemHash.combine(0, runtimeType.hashCode);
|
||||||
|
hash = _SystemHash.combine(hash, newBookmark.hashCode);
|
||||||
|
|
||||||
|
return _SystemHash.finish(hash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mixin AddBookmarkRef on AutoDisposeFutureProviderRef<ApiResponse<Bookmark>> {
|
||||||
|
/// The parameter `newBookmark` of this provider.
|
||||||
|
PostBookmark get newBookmark;
|
||||||
|
}
|
||||||
|
|
||||||
|
class _AddBookmarkProviderElement
|
||||||
|
extends AutoDisposeFutureProviderElement<ApiResponse<Bookmark>>
|
||||||
|
with AddBookmarkRef {
|
||||||
|
_AddBookmarkProviderElement(super.provider);
|
||||||
|
|
||||||
|
@override
|
||||||
|
PostBookmark get newBookmark => (origin as AddBookmarkProvider).newBookmark;
|
||||||
|
}
|
||||||
|
|
||||||
|
String _$addLinkHash() => r'0e9fcd2cca9fa5e195072163a11ae09b7bbe903a';
|
||||||
|
|
||||||
/// See also [AddLink].
|
/// See also [AddLink].
|
||||||
@ProviderFor(AddLink)
|
@ProviderFor(AddLink)
|
||||||
|
|||||||
@@ -246,7 +246,7 @@ class AddLinkModalState extends ConsumerState<AddLinkModal> with SingleTickerPro
|
|||||||
),
|
),
|
||||||
if (provider.checkBookmark != null)
|
if (provider.checkBookmark != null)
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () => Navigator.pop(context),
|
onPressed: ref.read(addLinkProvider.notifier).addBookmark,
|
||||||
child: Text(t.generic.confirm),
|
child: Text(t.generic.confirm),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import 'package:dio/dio.dart';
|
|||||||
import 'package:linkdy/models/api_response.dart';
|
import 'package:linkdy/models/api_response.dart';
|
||||||
import 'package:linkdy/models/data/bookmarks.dart';
|
import 'package:linkdy/models/data/bookmarks.dart';
|
||||||
import 'package:linkdy/models/data/check_bookmark.dart';
|
import 'package:linkdy/models/data/check_bookmark.dart';
|
||||||
|
import 'package:linkdy/models/data/post_bookmark.dart';
|
||||||
import 'package:linkdy/models/server_instance.dart';
|
import 'package:linkdy/models/server_instance.dart';
|
||||||
|
|
||||||
class ApiClient {
|
class ApiClient {
|
||||||
@@ -58,4 +59,19 @@ class ApiClient {
|
|||||||
return const ApiResponse(successful: false);
|
return const ApiResponse(successful: false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<ApiResponse<Bookmark>> fetchPostBookmark(PostBookmark bookmark) async {
|
||||||
|
try {
|
||||||
|
final response = await dioInstance.post(
|
||||||
|
"/bookmarks/",
|
||||||
|
data: FormData.fromMap(bookmark.toJson()),
|
||||||
|
);
|
||||||
|
return ApiResponse(
|
||||||
|
successful: true,
|
||||||
|
content: Bookmark.fromJson(response.data),
|
||||||
|
);
|
||||||
|
} catch (e, stackTrace) {
|
||||||
|
return const ApiResponse(successful: false);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user