Skip to content

Commit

Permalink
feat: show comments in moebooru
Browse files Browse the repository at this point in the history
  • Loading branch information
khoadng committed Jun 26, 2023
1 parent 354f355 commit 717b1f3
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/api/moebooru/moebooru_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,12 @@ abstract class MoebooruApi {
@Query('year') int year, {
@CancelRequest() CancelToken? cancelToken,
});

@GET('/comment.json')
Future<HttpResponse> getComments(
@Query('login') String? login,
@Query('password_hash') String? passwordHash,
@Query('post_id') int postId, {
@CancelRequest() CancelToken? cancelToken,
});
}
5 changes: 5 additions & 0 deletions lib/boorus/moebooru/feats/comments/comments.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export 'moebooru_comment.dart';
export 'moebooru_comment_dto.dart';
export 'moebooru_comment_parser.dart';
export 'moebooru_comment_provider.dart';
export 'moebooru_comment_repository.dart';
30 changes: 30 additions & 0 deletions lib/boorus/moebooru/feats/comments/moebooru_comment.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Package imports:
import 'package:equatable/equatable.dart';

class MoebooruComment extends Equatable {
const MoebooruComment({
required this.id,
required this.createdAt,
required this.postId,
required this.creator,
required this.creatorId,
required this.body,
});

final int id;
final DateTime createdAt;
final int postId;
final String creator;
final int creatorId;
final String body;

@override
List<Object> get props => [
id,
createdAt,
postId,
creator,
creatorId,
body,
];
}
28 changes: 28 additions & 0 deletions lib/boorus/moebooru/feats/comments/moebooru_comment_dto.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class MoebooruCommentDto {
final int? id;
final String? createdAt;
final int? postId;
final String? creator;
final int? creatorId;
final String? body;

MoebooruCommentDto({
this.id,
this.createdAt,
this.postId,
this.creator,
this.creatorId,
this.body,
});

factory MoebooruCommentDto.fromJson(Map<String, dynamic> json) {
return MoebooruCommentDto(
id: json['id'],
createdAt: json['created_at'],
postId: json['post_id'],
creator: json['creator'],
creatorId: json['creator_id'],
body: json['body'],
);
}
}
24 changes: 24 additions & 0 deletions lib/boorus/moebooru/feats/comments/moebooru_comment_parser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Package imports:
import 'package:retrofit/dio.dart';

// Project imports:
import 'package:boorusama/foundation/http/http.dart';
import 'moebooru_comment.dart';
import 'moebooru_comment_dto.dart';

MoebooruComment moebooruCommentDtoToMoebooruComment(MoebooruCommentDto dto) {
return MoebooruComment(
id: dto.id ?? 0,
createdAt: DateTime.tryParse(dto.createdAt ?? '') ?? DateTime.now(),
postId: dto.postId ?? 0,
creator: dto.creator ?? '',
creatorId: dto.creatorId ?? 0,
body: dto.body ?? '',
);
}

List<MoebooruComment> parseMoebooruComments(HttpResponse<dynamic> response) =>
parseResponse(
value: response,
converter: (item) => MoebooruCommentDto.fromJson(item),
).map(moebooruCommentDtoToMoebooruComment).toList();
19 changes: 19 additions & 0 deletions lib/boorus/moebooru/feats/comments/moebooru_comment_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Package imports:
import 'package:flutter_riverpod/flutter_riverpod.dart';

// Project imports:
import 'package:boorusama/boorus/core/feats/boorus/boorus.dart';
import 'package:boorusama/boorus/moebooru/moebooru_provider.dart';
import 'moebooru_comment.dart';
import 'moebooru_comment_repository.dart';

final moebooruCommentRepoProvider = Provider<MoebooruCommentRepository>((ref) {
return MoebooruCommentRepositoryApi(
api: ref.watch(moebooruApiProvider),
booruConfig: ref.watch(currentBooruConfigProvider),
);
});

final moebooruCommentsProvider =
FutureProvider.family<List<MoebooruComment>, int>((ref, postId) =>
ref.watch(moebooruCommentRepoProvider).getComments(postId));
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Project imports:
import 'package:boorusama/api/moebooru/moebooru_api.dart';
import 'package:boorusama/boorus/core/feats/boorus/boorus.dart';
import 'package:boorusama/boorus/moebooru/feats/comments/moebooru_comment_parser.dart';
import 'moebooru_comment.dart';

abstract interface class MoebooruCommentRepository {
Future<List<MoebooruComment>> getComments(int postId);
}

class MoebooruCommentRepositoryApi implements MoebooruCommentRepository {
MoebooruCommentRepositoryApi({
required this.api,
required this.booruConfig,
});

final MoebooruApi api;
final BooruConfig booruConfig;

@override
Future<List<MoebooruComment>> getComments(int postId) => api
.getComments(booruConfig.login, booruConfig.apiKey, postId)
.then(parseMoebooruComments)
.catchError((e) => <MoebooruComment>[]);
}
50 changes: 50 additions & 0 deletions lib/boorus/moebooru/pages/comments/moebooru_comment_item.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Flutter imports:
import 'package:flutter/material.dart';

// Package imports:
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

// Project imports:
import 'package:boorusama/boorus/core/feats/boorus/boorus.dart';
import 'package:boorusama/boorus/core/feats/dtext/html_converter.dart';
import 'package:boorusama/boorus/core/widgets/comment_header.dart';
import 'package:boorusama/boorus/moebooru/feats/comments/comments.dart';
import 'package:boorusama/flutter.dart';

class MoebooruCommentItem extends ConsumerWidget {
const MoebooruCommentItem({
super.key,
required this.comment,
});

final MoebooruComment comment;

@override
Widget build(BuildContext context, WidgetRef ref) {
final booru = ref.watch(currentBooruProvider);

return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CommentHeader(
authorName: comment.creator,
authorTitleColor: context.colorScheme.primary,
createdAt: comment.createdAt,
),
const SizedBox(height: 4),
Html(
style: {
'body': Style(
margin: EdgeInsets.zero,
),
},
data: dtext(
comment.body,
booru: booru,
),
)
],
);
}
}
39 changes: 39 additions & 0 deletions lib/boorus/moebooru/pages/posts/moebooru_post_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import 'package:boorusama/boorus/core/feats/posts/posts.dart';
import 'package:boorusama/boorus/core/provider.dart';
import 'package:boorusama/boorus/core/widgets/general_more_action_button.dart';
import 'package:boorusama/boorus/core/widgets/widgets.dart';
import 'package:boorusama/boorus/moebooru/feats/comments/comments.dart';
import 'package:boorusama/boorus/moebooru/moebooru_provider.dart';
import 'package:boorusama/boorus/moebooru/pages/comments/moebooru_comment_item.dart';
import 'package:boorusama/boorus/moebooru/pages/posts.dart';
import 'package:boorusama/boorus/moebooru/router.dart';
import 'package:boorusama/flutter.dart';
import 'package:boorusama/foundation/i18n.dart';
import 'package:boorusama/foundation/theme/theme_mode.dart';
import 'package:boorusama/widgets/widgets.dart';

Expand Down Expand Up @@ -201,6 +205,41 @@ class _MoebooruPostDetailsPageState
(source) => SourceSection(source: source),
() => const SizedBox.shrink(),
),
ref.watch(moebooruCommentsProvider(post.id)).when(
data: (comments) => comments.isEmpty
? const SizedBox.shrink()
: Padding(
padding: const EdgeInsets.symmetric(
vertical: 8,
horizontal: 12,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Divider(
thickness: 1.5,
),
Text(
'comment.comments'.tr(),
style: context.textTheme.titleLarge!.copyWith(
color: context.theme.hintColor,
fontSize: 16,
),
),
...comments
.map((comment) => Padding(
padding:
const EdgeInsets.symmetric(vertical: 8),
child:
MoebooruCommentItem(comment: comment),
))
.toList()
],
),
),
loading: () => const SizedBox.shrink(),
error: (e, __) => const SizedBox.shrink(),
),
],
];
}
Expand Down

0 comments on commit 717b1f3

Please sign in to comment.