Skip to content

Commit

Permalink
clicking on forums sort of works
Browse files Browse the repository at this point in the history
  • Loading branch information
EricApostal committed Dec 16, 2024
1 parent 44b7c3f commit fed0b9c
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 92 deletions.
127 changes: 68 additions & 59 deletions bonfire/lib/features/forum/views/components/card/card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import 'package:bonfire/theme/theme.dart';
import 'package:firebridge/firebridge.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

class ThreadCard extends ConsumerStatefulWidget {
final Snowflake threadId;
final Snowflake channelId;
final ScrollController scrollController;

const ThreadCard({
super.key,
required this.threadId,
required this.channelId,
required this.scrollController,
});

@override
Expand All @@ -23,82 +24,90 @@ class _ThreadCardState extends ConsumerState<ThreadCard> {
@override
void initState() {
super.initState();
widget.scrollController.addListener(_onScroll);
// widget.scrollController.addListener(_onScroll);
}

void _onScroll() {
if (widget.scrollController.position.pixels >=
widget.scrollController.position.maxScrollExtent - 200) {
final forumPosts =
ref.read(forumPostsProvider(widget.channelId).notifier);
if (forumPosts.hasMore) {
forumPosts.loadMore();
}
}
}
// void _onScroll() {
// if (widget.scrollController.position.pixels >=
// widget.scrollController.position.maxScrollExtent - 200) {
// final forumPosts = ref.read(forumPostsProvider(widget.threadId).notifier);
// if (forumPosts.hasMore) {
// forumPosts.loadMore();
// }
// }
// }

@override
Widget build(BuildContext context) {
// a bit of a hack, this can probably be done using other thread types so that should be handled
PublicThread thread =
ref.watch(threadChannelProvider(widget.channelId)) as PublicThread;
ref.watch(threadChannelProvider(widget.threadId)) as PublicThread;

Message? previewMessage = ref.watch(firstMessageProvider(widget.channelId));
Message? previewMessage = ref.watch(firstMessageProvider(widget.threadId));

return Container(
decoration: BoxDecoration(
color: Theme.of(context).custom.colorTheme.foreground,
borderRadius: BorderRadius.circular(8),
),
child: InkWell(
// hoverColor: Theme.of(context).custom.colorTheme.foreground,
splashColor: Colors.white,
borderRadius: BorderRadius.circular(8),
onTap: () {
print("tapped forum");
// Navigator.of(context).pushNamed(
// '/forum/thread',
// arguments: widget.postId,
// );
print("should navigate");
print("${widget.channelId}/${thread.id}");
// print(thread.);
// context.go("/channels/${thread.guildId}/${thread.id}/");
context.go(
"/channels/${thread.guildId}/${widget.channelId}/threads/${thread.id}/");
},
borderRadius: BorderRadius.circular(8),
child: Padding(
padding: const EdgeInsets.all(12),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
thread.name,
style: Theme.of(context).custom.textTheme.titleSmall,
),
if (previewMessage != null)
ConstrainedBox(
constraints:
const BoxConstraints(maxHeight: 50, minHeight: 0),
child: Text(
previewMessage.content,
style: Theme.of(context).custom.textTheme.bodyText2,
overflow: TextOverflow.ellipsis,
maxLines: 2,
),
),
],
),
child: Container(
decoration: BoxDecoration(
// color: Theme.of(context).custom.colorTheme.foreground,
// borderRadius: BorderRadius.circular(8),
),
if (previewMessage?.attachments.isNotEmpty == true &&
previewMessage!.attachments.first.contentType
?.split("/")[0] ==
"image")
ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
previewMessage.attachments.first.url.toString(),
width: 80,
height: 80,
fit: BoxFit.cover,
child: Padding(
padding: const EdgeInsets.all(12),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
thread.name,
style: Theme.of(context).custom.textTheme.titleSmall,
),
if (previewMessage != null)
ConstrainedBox(
constraints:
const BoxConstraints(maxHeight: 50, minHeight: 0),
child: Text(
previewMessage.content,
style: Theme.of(context).custom.textTheme.bodyText2,
overflow: TextOverflow.ellipsis,
maxLines: 2,
),
),
],
),
),
],
if (previewMessage?.attachments.isNotEmpty == true &&
previewMessage!.attachments.first.contentType
?.split("/")[0] ==
"image")
ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.network(
previewMessage.attachments.first.url.toString(),
width: 80,
height: 80,
fit: BoxFit.cover,
),
),
],
),
),
),
),
Expand All @@ -107,7 +116,7 @@ class _ThreadCardState extends ConsumerState<ThreadCard> {

@override
void dispose() {
widget.scrollController.removeListener(_onScroll);
// widget.scrollController.removeListener(_onScroll);
super.dispose();
}
}
39 changes: 24 additions & 15 deletions bonfire/lib/features/forum/views/forum.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:bonfire/features/forum/repositories/forum_posts.dart';
import 'package:bonfire/features/forum/repositories/forums.dart';
import 'package:bonfire/features/forum/views/components/card/card.dart';
import 'package:bonfire/features/messaging/views/components/box/channel_header.dart';
import 'package:firebridge/firebridge.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand Down Expand Up @@ -66,21 +67,29 @@ class _ForumViewState extends ConsumerState<ForumView> {
return (threadList?.threads ?? []) as List<Channel>;
}).toList();

return ListView.builder(
controller: _scrollController,
itemCount: threads.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: ThreadCard(
channelId: threads[index].id,
scrollController: _scrollController,
return Stack(
children: [
ListView.builder(
controller: _scrollController,
itemCount: threads.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: ThreadCard(
threadId: threads[index].id,
channelId: channel.id,
),
);
},
padding: EdgeInsets.only(
top: MediaQuery.of(context).padding.top + 60,
bottom: MediaQuery.of(context).padding.bottom,
),
);
},
padding: const EdgeInsets.only(
top: 8,
),
),
ChannelHeader(
channelName: channel.name,
),
],
);
},
loading: () => threads.isEmpty
Expand All @@ -93,7 +102,7 @@ class _ForumViewState extends ConsumerState<ForumView> {
padding: const EdgeInsets.only(bottom: 8.0),
child: ThreadCard(
channelId: threads[index].id,
scrollController: _scrollController,
threadId: threads[index].id,
),
);
},
Expand Down
23 changes: 18 additions & 5 deletions bonfire/lib/features/me/views/components/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import 'package:google_fonts/google_fonts.dart';
class MessageView extends ConsumerStatefulWidget {
final Snowflake guildId;
final Snowflake channelId;
const MessageView(
{super.key, required this.guildId, required this.channelId});
final Snowflake? threadId;
const MessageView({
super.key,
required this.guildId,
required this.channelId,
this.threadId,
});

@override
ConsumerState<ConsumerStatefulWidget> createState() => _MessageViewState();
Expand All @@ -19,13 +24,21 @@ class MessageView extends ConsumerStatefulWidget {
class _MessageViewState extends ConsumerState<MessageView> {
@override
Widget build(BuildContext context) {
var channelId = widget.channelId;
if (widget.threadId != null) {
channelId = widget.threadId!;
}
Channel? channel =
ref.watch(channelControllerProvider(widget.channelId)).valueOrNull;
ref.watch(channelControllerProvider(channelId)).valueOrNull;

if (channel is TextChannel) {
return MessageList(guildId: widget.guildId, channelId: widget.channelId);
return MessageList(
guildId: widget.guildId,
channelId: channelId,
threadId: widget.threadId,
);
} else if (channel is ForumChannel) {
return ForumView(guildId: widget.guildId, channelId: widget.channelId);
return ForumView(guildId: widget.guildId, channelId: channelId);
} else if (channel == null) {
return const Center(child: CircularProgressIndicator());
}
Expand Down
12 changes: 9 additions & 3 deletions bonfire/lib/features/messaging/repositories/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Messages extends _$Messages {

Future<List<Message>?> getMessages({
Snowflake? before,
Snowflake? around,
int? count,
bool disableAck = false,
}) async {
Expand Down Expand Up @@ -93,9 +94,9 @@ class Messages extends _$Messages {

var messages = await (channel as TextChannel)
.messages
.fetchMany(limit: count ?? 50, before: before);
.fetchMany(limit: count ?? 50, before: before, around: around);

if (before == null) {
if (before == null && around == null) {
loadedMessages = messages.toList();
} else {
loadedMessages.addAll(messages.toList());
Expand All @@ -119,14 +120,19 @@ class Messages extends _$Messages {
}
}

Future<List<Message>> fetchMessages({Message? before, int? limit}) async {
Future<List<Message>> fetchMessages({
Message? before,
int? limit,
Snowflake? around,
}) async {
Channel channel =
ref.watch(channelControllerProvider(channelId)).valueOrNull!;
List<Message> messages = [];

messages.addAll(loadedMessages);
messages.addAll(await getMessages(
before: before?.id,
around: around,
count: limit,
) ??
[]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class _ChannelHeaderState extends ConsumerState<ChannelHeader> {
style: GoogleFonts.publicSans(
fontSize: 16,
letterSpacing: 0.4,
fontWeight: FontWeight.w500,
fontWeight: FontWeight.w600,
color: Colors.white,
),
),
Expand Down
16 changes: 13 additions & 3 deletions bonfire/lib/features/messaging/views/components/message_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ import 'package:bonfire/shared/utils/platform.dart';
class MessageList extends ConsumerStatefulWidget {
final Snowflake guildId;
final Snowflake channelId;
const MessageList(
{super.key, required this.guildId, required this.channelId});
final Snowflake? threadId;
const MessageList({
super.key,
required this.guildId,
required this.channelId,
this.threadId,
});

@override
ConsumerState<MessageList> createState() => _MessageViewState();
Expand All @@ -40,6 +45,7 @@ class _MessageViewState extends ConsumerState<MessageList>
@override
void initState() {
super.initState();

_scrollController.addListener(_scrollListener);

_fadeController = AnimationController(
Expand Down Expand Up @@ -81,7 +87,11 @@ class _MessageViewState extends ConsumerState<MessageList>
lastScrollMessage = lastScrollMessage ?? firstBatchLastMessage!;
List<Message>? recents = await ref
.read(messagesProvider(widget.channelId).notifier)
.fetchMessages(before: lastScrollMessage!, limit: 50);
.fetchMessages(
before: lastScrollMessage!,
limit: 50,
around: widget.threadId,
);

if (recents.isNotEmpty) {
if (lastScrollMessage?.id != recents.last.id) {
Expand Down
12 changes: 10 additions & 2 deletions bonfire/lib/features/overview/views/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
class GuildMessagingOverview extends ConsumerStatefulWidget {
final Snowflake guildId;
final Snowflake channelId;
const GuildMessagingOverview(
{super.key, required this.guildId, required this.channelId});
final Snowflake? threadId;
const GuildMessagingOverview({
super.key,
required this.guildId,
required this.channelId,
this.threadId,
});

@override
ConsumerState<GuildMessagingOverview> createState() => _HomeState();
Expand All @@ -29,14 +34,17 @@ class _HomeState extends ConsumerState<GuildMessagingOverview> {

@override
Widget build(BuildContext context) {
print("GuildMessagingOverview.build");
return (shouldUseMobileLayout(context))
? HomeMobile(
guildId: widget.guildId,
channelId: widget.channelId,
threadId: widget.threadId,
)
: HomeDesktop(
guildId: widget.guildId,
channelId: widget.channelId,
threadId: widget.threadId,
);
}
}
Loading

0 comments on commit fed0b9c

Please sign in to comment.