Skip to content

Commit

Permalink
feat: regard thread order type when dispatch string urls
Browse files Browse the repository at this point in the history
  • Loading branch information
realth000 committed Jan 14, 2025
1 parent 3fd9a40 commit f21d4c3
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/extensions/string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ extension ParseUrl on String {
}

if (mod == 'viewthread' && queryParameters.containsKey('tid')) {
final order = queryParameters['ordertype']?.parseToInt();

// When anchor is used, means need to scroll to a target post by its pid.
//
// In this situation, do NOT override page order because the page number
Expand All @@ -69,6 +71,7 @@ extension ParseUrl on String {
if (queryParameters.containsKey('page'))
'pageNumber': "${queryParameters['page']}",
if (anchor != null) 'overrideReverseOrder': 'false',
if (order != null) 'overrideWithExactOrder': '$order',
if (anchor != null) 'pid': anchor,
if (queryParameters.containsKey('authorid'))
'onlyVisibleUid': "${queryParameters['authorid']}",
Expand Down
9 changes: 9 additions & 0 deletions lib/features/thread/bloc/thread_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ThreadBloc extends Bloc<ThreadEvent, ThreadState> with LoggerMixin {
required String? pid,
required String? onlyVisibleUid,
required bool? reverseOrder,
required int? exactOrder,
required ThreadRepository threadRepository,
}) : _threadRepository = threadRepository,
super(
Expand All @@ -31,6 +32,7 @@ class ThreadBloc extends Bloc<ThreadEvent, ThreadState> with LoggerMixin {
pid: pid,
onlyVisibleUid: onlyVisibleUid,
reverseOrder: reverseOrder,
exactOrder: exactOrder,
),
) {
on<ThreadLoadMoreRequested>(_onThreadLoadMoreRequested);
Expand Down Expand Up @@ -59,6 +61,7 @@ class ThreadBloc extends Bloc<ThreadEvent, ThreadState> with LoggerMixin {
pageNumber: event.pageNumber,
onlyVisibleUid: state.onlyVisibleUid,
reverseOrder: state.reverseOrder,
exactOrder: state.exactOrder,
)
.match(
(e) {
Expand All @@ -85,6 +88,7 @@ class ThreadBloc extends Bloc<ThreadEvent, ThreadState> with LoggerMixin {
pid: state.pid,
onlyVisibleUid: state.onlyVisibleUid,
reverseOrder: state.reverseOrder,
exactOrder: state.exactOrder,
)
.match(
(e) {
Expand Down Expand Up @@ -112,6 +116,7 @@ class ThreadBloc extends Bloc<ThreadEvent, ThreadState> with LoggerMixin {
pageNumber: event.pageNumber,
onlyVisibleUid: state.onlyVisibleUid,
reverseOrder: state.reverseOrder,
exactOrder: state.exactOrder,
)
.map(
(v) =>
Expand Down Expand Up @@ -147,6 +152,7 @@ class ThreadBloc extends Bloc<ThreadEvent, ThreadState> with LoggerMixin {
pageNumber: state.currentPage,
onlyVisibleUid: event.uid,
reverseOrder: state.reverseOrder,
exactOrder: state.exactOrder,
)
.match(
(e) {
Expand Down Expand Up @@ -186,6 +192,7 @@ class ThreadBloc extends Bloc<ThreadEvent, ThreadState> with LoggerMixin {
pid: state.pid,
pageNumber: state.currentPage,
reverseOrder: state.reverseOrder,
exactOrder: state.exactOrder,
)
.match(
(e) {
Expand Down Expand Up @@ -230,6 +237,7 @@ class ThreadBloc extends Bloc<ThreadEvent, ThreadState> with LoggerMixin {
pageNumber: state.currentPage,
onlyVisibleUid: state.onlyVisibleUid,
reverseOrder: state.reverseOrder,
exactOrder: state.exactOrder,
)
.match(
(e) {
Expand Down Expand Up @@ -375,6 +383,7 @@ class ThreadBloc extends Bloc<ThreadEvent, ThreadState> with LoggerMixin {
onlyVisibleUid:
(clearOnlyVisibleUid ?? false) ? null : state.onlyVisibleUid,
reverseOrder: state.reverseOrder,
exactOrder: state.exactOrder,
isDraft: isDraft ?? false,
latestModAct: latestModAct,
);
Expand Down
6 changes: 6 additions & 0 deletions lib/features/thread/bloc/thread_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ThreadState with ThreadStateMappable {
this.threadType,
this.onlyVisibleUid,
this.reverseOrder,
this.exactOrder,
this.isDraft = false,
this.latestModAct,
});
Expand Down Expand Up @@ -124,6 +125,11 @@ class ThreadState with ThreadStateMappable {
/// * Not force order if `null`.
final bool? reverseOrder;

/// The exact thread order in state.
///
/// Specify this field if want to override with the app one.
final int? exactOrder;

/// Indicating current thread is a draft or not.
final bool isDraft;

Expand Down
10 changes: 6 additions & 4 deletions lib/features/thread/repository/thread_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ThreadRepository {
int pageNumber = 1,
String? onlyVisibleUid,
bool? reverseOrder,
int? exactOrder,
}) =>
AsyncEither(() async {
assert(
Expand All @@ -56,10 +57,11 @@ class ThreadRepository {
// to find a post in a certain page number, in this use case a manually
// other override may going into different page that does NOT contain
// the target post.
final orderType = switch (reverseOrder) {
true => '&ordertype=1',
false => '&ordertype=2',
null => '',
final orderType = switch ((exactOrder, reverseOrder)) {
(final int i, _) => '&ordertype=$i',
(null, true) => '&ordertype=1',
(null, false) => '&ordertype=2',
(null, null) => '',
};

_pageNumber = pageNumber;
Expand Down
22 changes: 22 additions & 0 deletions lib/features/thread/view/thread_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ThreadPage extends StatefulWidget {
required this.findPostID,
required this.pageNumber,
required this.overrideReverseOrder,
required this.overrideWithExactOrder,
this.title,
this.threadType,
this.onlyVisibleUid,
Expand Down Expand Up @@ -80,8 +81,28 @@ class ThreadPage extends StatefulWidget {
/// This flag is used in situation that user is heading to a certain page
/// contains a target post. If set to `true`, override order may cause going
/// to a wrong page.
///
/// Additionally, the effect has less priority compared to
/// [overrideWithExactOrder] where the latter one is specifying the exact
/// order type and current field only determines using the order specified by
/// app if has.
final bool overrideReverseOrder;

/// Carries the exact order required by external reasons.
///
/// If value is not null, the final thread order is override by the value no
/// matter [overrideReverseOrder] is true or false.
///
/// Actually this field is a patch on is the following situation:
///
/// ```console
/// ${HOST}/...&ordertype=N
/// ```
///
/// where order type is directly specified in url before dispatching, and
/// should override any order in app settings.
final int? overrideWithExactOrder;

/// Thread type.
///
/// Sometimes we do not know the thread type before we load it, redirect from
Expand Down Expand Up @@ -220,6 +241,7 @@ class _ThreadPageState extends State<ThreadPage>
threadRepository: context.repo(),
reverseOrder:
widget.overrideReverseOrder ? threadReverseOrder : null,
exactOrder: widget.overrideWithExactOrder,
)..add(ThreadLoadMoreRequested(int.tryParse(widget.pageNumber) ?? 1)),
),
BlocProvider(
Expand Down
3 changes: 3 additions & 0 deletions lib/routes/app_routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ final router = GoRouter(
} else {
overrideReverseOrder = true;
}
final overrideWithExactOrder =
state.uri.queryParameters['overrideWithExactOrder']?.parseToInt();

assert(
tid != null || pid != null,
Expand All @@ -158,6 +160,7 @@ final router = GoRouter(
findPostID: pid,
pageNumber: pageNumber ?? '1',
overrideReverseOrder: overrideReverseOrder,
overrideWithExactOrder: overrideWithExactOrder,
onlyVisibleUid: onlyVisibleUid,
);
},
Expand Down
3 changes: 3 additions & 0 deletions lib/routes/screen_paths.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class ScreenPaths {
///
/// `overrideReverseOrder`: set to "false" keep the original post order in
/// thread, for some use case user heading to a page contains a certain post.
///
/// `overrideWithExactOrder`: set the exact thread order type, this value
/// is usually specified by the original source (in url).
static const String thread = '/thread';

/// Notice page.
Expand Down

0 comments on commit f21d4c3

Please sign in to comment.