Skip to content

Commit

Permalink
feat(thread): Show snack bar when send post succeed
Browse files Browse the repository at this point in the history
  • Loading branch information
realth000 committed Feb 21, 2024
1 parent 8a18acd commit 79ce55a
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 109 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- 支持筛选积分变更历史。
- 帖子内按页数显示分组。
- 现在在安卓和IOS上尝试使用和当前语言的应用标题。
- 现在发送回复后会提示发送成功。

### Fixed

Expand Down
224 changes: 115 additions & 109 deletions lib/features/thread/view/thread_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,122 +190,128 @@ class _ThreadPageState extends State<ThreadPage>
create: (context) => JumpPageCubit(),
),
],
child: BlocListener<ThreadBloc, ThreadState>(
listener: (context, state) {
// Update reply parameters to reply bar.
context
.read<ReplyBloc>()
.add(ReplyParametersUpdated(state.replyParameters));

// Update thread closed state to reply bar.
if (state.threadClosed) {
context
.read<ReplyBloc>()
.add(const ReplyThreadClosed(closed: true));
} else {
context
.read<ReplyBloc>()
.add(const ReplyThreadClosed(closed: false));
}
},
child: BlocListener<ThreadBloc, ThreadState>(
listener: (context, state) {
if (state.status == ThreadStatus.failed) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(context.t.general.failedToLoad)),
);
}
},
child: BlocBuilder<ThreadBloc, ThreadState>(
builder: (context, state) {
// Update jump page state.
context.read<JumpPageCubit>().setPageInfo(
currentPage: state.currentPage,
totalPages: state.totalPages,
);
child: MultiBlocListener(
listeners: [
BlocListener<ThreadBloc, ThreadState>(
listener: (context, state) {
// Update reply parameters to reply bar.
context
.read<ReplyBloc>()
.add(ReplyParametersUpdated(state.replyParameters));

String? title;

// Reset jump page state when every build.
if (state.status == ThreadStatus.loading ||
state.status == ThreadStatus.initial) {
context.read<JumpPageCubit>().markLoading();
title = widget.title;
// Update thread closed state to reply bar.
if (state.threadClosed) {
context
.read<ReplyBloc>()
.add(const ReplyThreadClosed(closed: true));
} else {
context.read<JumpPageCubit>().markSuccess();
context
.read<ReplyBloc>()
.add(const ReplyThreadClosed(closed: false));
}

var threadUrl =
RepositoryProvider.of<ThreadRepository>(context).threadUrl;
if (widget.threadID != null) {
threadUrl ??= '$baseUrl/forum.php?mod=viewthread&'
'tid=${widget.threadID}&extra=page%3D1';
} else {
// Here we don;t have threadID, thus the findPostID is
// definitely not null.
threadUrl ??= '$baseUrl/forum.php?mode=redirect&goto=findpost&'
'pid=${widget.findPostID}';
if (state.status == ThreadStatus.failed) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(context.t.general.failedToLoad)),
);
}
},
),
BlocListener<ReplyBloc, ReplyState>(
listener: (context, state) {
if (state.status == ReplyStatus.success) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(context.t.threadPage.replySuccess)),
);
}
},
),
],
child: BlocBuilder<ThreadBloc, ThreadState>(
builder: (context, state) {
// Update jump page state.
context.read<JumpPageCubit>().setPageInfo(
currentPage: state.currentPage,
totalPages: state.totalPages,
);

String? title;

// Reset jump page state when every build.
if (state.status == ThreadStatus.loading ||
state.status == ThreadStatus.initial) {
context.read<JumpPageCubit>().markLoading();
title = widget.title;
} else {
context.read<JumpPageCubit>().markSuccess();
}

return Scaffold(
appBar: ListAppBar(
title: title,
showReverseOrderAction: true,
onSearch: () async {
await context.pushNamed(ScreenPaths.search);
},
onJumpPage: (pageNumber) async {
if (!mounted) {
return;
}
// Mark loading here.
// Mark state will be removed when loading finishes
// in next build.
context.read<JumpPageCubit>().markLoading();
context
.read<ThreadBloc>()
.add(ThreadJumpPageRequested(pageNumber));
},
onSelected: (value) async {
switch (value) {
case MenuActions.refresh:
context
.read<ThreadBloc>()
.add(ThreadRefreshRequested());
var threadUrl =
RepositoryProvider.of<ThreadRepository>(context).threadUrl;
if (widget.threadID != null) {
threadUrl ??= '$baseUrl/forum.php?mod=viewthread&'
'tid=${widget.threadID}&extra=page%3D1';
} else {
// Here we don;t have threadID, thus the findPostID is
// definitely not null.
threadUrl ??= '$baseUrl/forum.php?mode=redirect&goto=findpost&'
'pid=${widget.findPostID}';
}

case MenuActions.copyUrl:
await Clipboard.setData(
ClipboardData(text: threadUrl!),
);
if (!context.mounted) {
return;
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
context.t.aboutPage.copiedToClipboard,
),
return Scaffold(
appBar: ListAppBar(
title: title,
showReverseOrderAction: true,
onSearch: () async {
await context.pushNamed(ScreenPaths.search);
},
onJumpPage: (pageNumber) async {
if (!mounted) {
return;
}
// Mark loading here.
// Mark state will be removed when loading finishes
// in next build.
context.read<JumpPageCubit>().markLoading();
context
.read<ThreadBloc>()
.add(ThreadJumpPageRequested(pageNumber));
},
onSelected: (value) async {
switch (value) {
case MenuActions.refresh:
context.read<ThreadBloc>().add(ThreadRefreshRequested());
case MenuActions.copyUrl:
await Clipboard.setData(
ClipboardData(text: threadUrl!),
);
if (!context.mounted) {
return;
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
context.t.aboutPage.copiedToClipboard,
),
);
case MenuActions.openInBrowser:
await context.dispatchAsUrl(threadUrl!, external: true);
case MenuActions.backToTop:
await _listScrollController.animateTo(
0,
curve: Curves.ease,
duration: const Duration(milliseconds: 500),
);
case MenuActions.reverseOrder:
context
.readOrNull<ThreadBloc>()
?.add(const ThreadChangeViewOrderRequested());
}
},
),
body: _buildBody(context, state),
);
},
),
),
);
case MenuActions.openInBrowser:
await context.dispatchAsUrl(threadUrl!, external: true);
case MenuActions.backToTop:
await _listScrollController.animateTo(
0,
curve: Curves.ease,
duration: const Duration(milliseconds: 500),
);
case MenuActions.reverseOrder:
context
.readOrNull<ThreadBloc>()
?.add(const ThreadChangeViewOrderRequested());
}
},
),
body: _buildBody(context, state),
);
},
),
),
);
Expand Down

0 comments on commit 79ce55a

Please sign in to comment.