Skip to content

Commit

Permalink
feat: finish subject collection update.
Browse files Browse the repository at this point in the history
  • Loading branch information
li-guohao committed Aug 23, 2023
1 parent f33c294 commit 0e621e3
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 13 deletions.
124 changes: 114 additions & 10 deletions lib/api/collection/model/SubjectCollectionApi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ import 'package:ikaros/api/collection/model/SubjectCollection.dart';
import 'package:ikaros/api/common/PagingWrap.dart';

class SubjectCollectionApi {
PagingWrap error = PagingWrap(page: -1, size: -1, total: -1, items: List.empty());

Future<PagingWrap> fetchSubjectCollections(int? page, int? size, CollectionType? type) async {
SubjectCollection error = SubjectCollection(
id: -1,
userId: -1,
subjectId: -1,
type: CollectionType.WISH,
isPrivate: false,
name: "name",
nsfw: false,
cover: "cover");
PagingWrap errors =
PagingWrap(page: -1, size: -1, total: -1, items: List.empty());

Future<PagingWrap> fetchSubjectCollections(
int? page, int? size, CollectionType? type) async {
AuthParams authParams = await AuthApi().getAuthParams();
if (authParams.baseUrl == '' ||
authParams.username == '' ||
authParams.basicAuth == '') {
return error;
return errors;
}

page ??= 1;
Expand All @@ -23,13 +33,41 @@ class SubjectCollectionApi {
String baseUrl = authParams.baseUrl;
String basicAuth = authParams.basicAuth;
String userId = authParams.userId;
String apiUrl =
"$baseUrl/api/v1alpha1/collection/subject/$userId"
"?page=$page&size=$size";
if(type != null) {
String apiUrl = "$baseUrl/api/v1alpha1/collection/subject/$userId"
"?page=$page&size=$size";
if (type != null) {
apiUrl = "${apiUrl}&type=${type.name}";
}

try {
BaseOptions options = BaseOptions();
options.headers.putIfAbsent("Authorization", () => basicAuth);

var response = await Dio(options).get(apiUrl);
// print("response status code: ${response.statusCode}");
if (response.statusCode != 200) {
return errors;
}
return PagingWrap.fromJson(response.data);
} catch (e) {
print(e);
return errors;
}
}

Future<SubjectCollection> findCollectionBySubjectId(int subjectId) async {
AuthParams authParams = await AuthApi().getAuthParams();
if (authParams.baseUrl == '' ||
authParams.username == '' ||
authParams.basicAuth == '' ||
authParams.userId == '') {
return error;
}

String baseUrl = authParams.baseUrl;
String basicAuth = authParams.basicAuth;
String userId = authParams.userId;
String apiUrl = "$baseUrl/api/v1alpha1/collection/subject/$userId/$subjectId";

try {
BaseOptions options = BaseOptions();
Expand All @@ -40,10 +78,76 @@ class SubjectCollectionApi {
if (response.statusCode != 200) {
return error;
}
return PagingWrap.fromJson(response.data);
return SubjectCollection.fromJson(response.data);
} catch (e) {
print(e);
return error;
}
}

Future updateCollection(int subjectId, CollectionType type, bool? isPrivate) async {
AuthParams authParams = await AuthApi().getAuthParams();
if (authParams.baseUrl == '' ||
authParams.username == '' ||
authParams.basicAuth == '' ||
authParams.userId == '') {
return error;
}

String baseUrl = authParams.baseUrl;
String basicAuth = authParams.basicAuth;
String userId = authParams.userId;
String apiUrl = "$baseUrl/api/v1alpha1/collection/subject/collect"
"?userId=$userId&subjectId=$subjectId&type=${type.name}";

if(isPrivate != null) {
apiUrl = "$apiUrl&isPrivate=$isPrivate";
}

try {
BaseOptions options = BaseOptions();
options.headers.putIfAbsent("Authorization", () => basicAuth);

var response = await Dio(options).post(apiUrl);
// print("response status code: ${response.statusCode}");
if (response.statusCode != 200) {
return error;
}
return ;
} catch (e) {
print(e);
return error;
}
}

Future removeCollection(int subjectId,) async {
AuthParams authParams = await AuthApi().getAuthParams();
if (authParams.baseUrl == '' ||
authParams.username == '' ||
authParams.basicAuth == '' ||
authParams.userId == '') {
return error;
}

String baseUrl = authParams.baseUrl;
String basicAuth = authParams.basicAuth;
String userId = authParams.userId;
String apiUrl = "$baseUrl/api/v1alpha1/collection/subject/collect"
"?userId=$userId&subjectId=$subjectId";

try {
BaseOptions options = BaseOptions();
options.headers.putIfAbsent("Authorization", () => basicAuth);

var response = await Dio(options).delete(apiUrl);
// print("response status code: ${response.statusCode}");
if (response.statusCode != 200) {
return error;
}
return ;
} catch (e) {
print(e);
return error;
}
}
}
}
4 changes: 1 addition & 3 deletions lib/collection/collections.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,9 @@ class CollectionsState extends State<CollectionPage> {
actions: [
Row(
children: [
GFDropdown(
DropdownButton(
// padding: const EdgeInsets.all(15),
borderRadius: BorderRadius.circular(5),
border: const BorderSide(color: Colors.black12, width: 1),
dropdownButtonColor: Colors.white,
value: _type,
onChanged: (newValue) {
setState(() {
Expand Down
104 changes: 104 additions & 0 deletions lib/subject/subject-details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import 'package:fluttertoast/fluttertoast.dart';
import 'package:getwidget/getwidget.dart';
import 'package:ikaros/api/auth/AuthApi.dart';
import 'package:ikaros/api/auth/AuthParams.dart';
import 'package:ikaros/api/collection/enums/CollectionType.dart';
import 'package:ikaros/api/collection/model/SubjectCollection.dart';
import 'package:ikaros/api/collection/model/SubjectCollectionApi.dart';
import 'package:ikaros/api/subject/model/Episode.dart';
import 'package:ikaros/api/subject/model/EpisodeResource.dart';
import 'package:ikaros/api/subject/model/Subject.dart';
Expand Down Expand Up @@ -39,6 +42,9 @@ class _SubjectDetailsView extends State<SubjectDetailsPage> {
late Function _onPlayerInitialized;
bool _isPlayerInitializedCallOnce = false;

late CollectionType? _collectionType;
late SubjectCollection _subjectCollection;

_updateIsFullScreen(bool val) {
isFullScreen = val;
}
Expand Down Expand Up @@ -220,6 +226,90 @@ class _SubjectDetailsView extends State<SubjectDetailsPage> {
style: const TextStyle(
color: Colors.black, backgroundColor: Colors.white),
),
actions: [
FutureBuilder<String>(
future: _loadSubjectCollection(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return const Text("Load subject collection error.");
} else {
return Row(
children: [
GFButton(
onPressed: (_subjectCollection.id != -1)
? () async {
// 取消收藏
await SubjectCollectionApi().removeCollection(widget.subject.id);
Fluttertoast.showToast(
msg: "取消收藏成功",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.blue,
textColor: Colors.white,
fontSize: 16.0);
setState(() {
_loadSubjectCollection();
});
}
: () async {
await _updateSubjectCollection();
Fluttertoast.showToast(
msg: "收藏成功",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.blue,
textColor: Colors.white,
fontSize: 16.0);
setState(() {
_loadSubjectCollection();
});
},
text: (_subjectCollection.id != -1) ? '取消收藏' : '收藏',
textColor: Colors.black,
// disabledColor: Colors.grey,
color: Colors.white70,
boxShadow: const BoxShadow(),
),
Container(width: 10,),
DropdownButton(
borderRadius: BorderRadius.circular(5),
value: _collectionType,
onChanged: (_subjectCollection.id != -1)
? (newValue) {
setState(() {
_collectionType = newValue as CollectionType?;
});
_updateSubjectCollection();
} : null,
items: [
CollectionType.WISH,
CollectionType.DOING,
CollectionType.DONE,
CollectionType.SHELVE,
CollectionType.DISCARD,
CollectionType.SHELVE,
]
.map((value) => DropdownMenuItem(
value: value,
child: Text(value.name),
))
.toList(),
),
],
);
}
} else {
return Padding(
padding: EdgeInsets.all(20),
child: CircularProgressIndicator(),
);
}
},
)
],
),
),
),
Expand Down Expand Up @@ -466,4 +556,18 @@ class _SubjectDetailsView extends State<SubjectDetailsPage> {
),
);
}

Future<String> _loadSubjectCollection() async {
_subjectCollection = await SubjectCollectionApi()
.findCollectionBySubjectId(widget.subject.id);
_collectionType = _subjectCollection.type;
return "";
}

_updateSubjectCollection() async {
if (_collectionType != null) {
await SubjectCollectionApi()
.updateCollection(widget.subject.id, _collectionType!, null);
}
}
}

0 comments on commit 0e621e3

Please sign in to comment.