Skip to content

Commit

Permalink
feat(*): Try parse unread info status
Browse files Browse the repository at this point in the history
* Try parse unread info status in homepage and profile page.
* Show badge if there is any unread info.
  • Loading branch information
realth000 committed Feb 3, 2024
1 parent f3d3933 commit 278b5f9
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed

- 不再显示被设置为不显示的html节点。
- 尝试在首页和我的页面内提醒有未读的消息。

## [0.5.1] - 2024-01-31

Expand Down
8 changes: 8 additions & 0 deletions lib/features/homepage/bloc/homepage_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ class HomepageBloc extends Bloc<HomepageEvent, HomepageState> {
// Style 2: With welcome text
document.querySelector('div#chart > script');

final hasUnreadNotice =
document.querySelector('a#myprompt')?.classes.contains('new') ?? false;

final hasUnreadMessage =
document.querySelector('a#pm_ntc')?.classes.contains('new') ?? false;

final picUrlList =
_buildKahrpbaPicUrlList(styleNode).whereType<String>().toList();
final picHrefList =
Expand Down Expand Up @@ -282,6 +288,8 @@ class HomepageBloc extends Bloc<HomepageEvent, HomepageState> {
loggedUserInfo: loggedUserInfo,
pinnedThreadGroupList: pinnedThreadGroupList,
swiperUrlList: swiperUrlList,
hasUnreadNotice: hasUnreadNotice,
hasUnreadMessage: hasUnreadMessage,
);
}

Expand Down
13 changes: 13 additions & 0 deletions lib/features/homepage/bloc/homepage_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ final class HomepageState extends Equatable {
this.loggedUserInfo,
this.pinnedThreadGroupList = const [],
this.swiperUrlList = const [],
this.hasUnreadNotice = false,
this.hasUnreadMessage = false,
});

/// Loading status.
Expand All @@ -63,6 +65,12 @@ final class HomepageState extends Equatable {
/// Swiper urls in the homepage.
final List<SwiperUrl> swiperUrlList;

/// Flag indicating has unread notices or not.
final bool hasUnreadNotice;

/// Flag indicating has unread messages or not.
final bool hasUnreadMessage;

/// Copy with
HomepageState copyWith({
HomepageStatus? status,
Expand All @@ -71,6 +79,8 @@ final class HomepageState extends Equatable {
List<PinnedThreadGroup>? pinnedThreadGroupList,
List<SwiperUrl>? swiperUrlList,
int? documentHashCode,
bool? hasUnreadNotice,
bool? hasUnreadMessage,
}) {
return HomepageState(
status: status ?? this.status,
Expand All @@ -79,6 +89,8 @@ final class HomepageState extends Equatable {
pinnedThreadGroupList:
pinnedThreadGroupList ?? this.pinnedThreadGroupList,
swiperUrlList: swiperUrlList ?? this.swiperUrlList,
hasUnreadNotice: hasUnreadNotice ?? this.hasUnreadNotice,
hasUnreadMessage: hasUnreadMessage ?? this.hasUnreadMessage,
);
}

Expand All @@ -89,5 +101,6 @@ final class HomepageState extends Equatable {
loggedUserInfo,
pinnedThreadGroupList,
swiperUrlList,
hasUnreadNotice,
];
}
11 changes: 10 additions & 1 deletion lib/features/homepage/widgets/welcome_section.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:tsdm_client/constants/url.dart';
import 'package:tsdm_client/extensions/string.dart';
import 'package:tsdm_client/features/authentication/repository/authentication_repository.dart';
import 'package:tsdm_client/features/home/cubit/home_cubit.dart';
import 'package:tsdm_client/features/homepage/bloc/homepage_bloc.dart';
import 'package:tsdm_client/features/homepage/models/models.dart';
import 'package:tsdm_client/routes/screen_paths.dart';
import 'package:tsdm_client/utils/debug.dart';
Expand Down Expand Up @@ -154,6 +155,10 @@ class WelcomeSection extends StatelessWidget {
final needExpand = ResponsiveBreakpoints.of(context)
.largerOrEqualTo('homepage_welcome_expand');

final homePageState = context.read<HomepageBloc>().state;
final hasUnreadInfo =
homePageState.hasUnreadMessage || homePageState.hasUnreadNotice;

return ConstrainedBox(
constraints: BoxConstraints(
maxHeight: needExpand ? _kahrpbaPicHeight : _kahrpbaPicHeight * 2 + 20,
Expand Down Expand Up @@ -203,7 +208,11 @@ class WelcomeSection extends StatelessWidget {
),
Expanded(child: Container()),
IconButton(
icon: const Icon(Icons.notifications_outlined),
icon: hasUnreadInfo
? const Badge(
child: Icon(Icons.notifications_outlined),
)
: const Icon(Icons.notifications_outlined),
onPressed: () async {
await context.pushNamed(ScreenPaths.notice);
},
Expand Down
24 changes: 24 additions & 0 deletions lib/features/profile/bloc/profile_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
event.uid == null &&
_profileRepository.hasCache()) {
final userProfile = _buildProfile(_profileRepository.getCache()!);
final (hasUnreadNotice, hasUnreadMessage) =
_buildUnreadInfoStatus(_profileRepository.getCache()!);
emit(
state.copyWith(
status: ProfileStatus.success,
userProfile: userProfile,
hasUnreadNotice: hasUnreadNotice,
hasUnreadMessage: hasUnreadMessage,
),
);
return;
Expand All @@ -69,10 +73,14 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
emit(state.copyWith(status: ProfileStatus.failed));
return;
}
final (hasUnreadNotice, hasUnreadMessage) =
_buildUnreadInfoStatus(document);
emit(
state.copyWith(
status: ProfileStatus.success,
userProfile: userProfile,
hasUnreadNotice: hasUnreadNotice,
hasUnreadMessage: hasUnreadMessage,
),
);
} on HttpRequestFailedException catch (e) {
Expand All @@ -98,10 +106,14 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
emit(state.copyWith(status: ProfileStatus.failed));
return;
}
final (hasUnreadNotice, hasUnreadMessage) =
_buildUnreadInfoStatus(document);
emit(
state.copyWith(
status: ProfileStatus.success,
userProfile: userProfile,
hasUnreadNotice: hasUnreadNotice,
hasUnreadMessage: hasUnreadMessage,
),
);
} on HttpRequestFailedException catch (e) {
Expand Down Expand Up @@ -222,4 +234,16 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
activityInfoList: activityInfoList,
);
}

(bool hasUnreadNotice, bool hasUnreadMessage) _buildUnreadInfoStatus(
uh.Document document,
) {
// Check notice status.
final hasUnreadNotice =
document.querySelector('a#myprompt')?.classes.contains('new') ?? false;
final hasUnreadMessage =
document.querySelector('a#pm_ntc')?.classes.contains('new') ?? false;

return (hasUnreadNotice, hasUnreadMessage);
}
}
18 changes: 16 additions & 2 deletions lib/features/profile/bloc/profile_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class ProfileState extends Equatable {
this.uid,
this.userProfile,
this.failedToLogoutReason,
this.hasUnreadNotice = false,
this.hasUnreadMessage = false,
});

/// Status.
Expand All @@ -50,21 +52,31 @@ class ProfileState extends Equatable {
/// page content is as same as [ProfileStatus.success].
final Exception? failedToLogoutReason;

/// Flag indicating has unread notices or not.
final bool hasUnreadNotice;

/// Flag indicating has unread messages or not.
final bool hasUnreadMessage;

/// Copy with.
ProfileState copyWith({
ProfileStatus? status,
String? username,
String? uid,
UserProfile? userProfile,
Exception? failedToLogoutReason,
bool? hasUnreadNotice,
bool? hasUnreadMessage,
}) {
return ProfileState(
status: status ?? this.status,
username: username ?? this.username,
uid: uid ?? this.uid,
userProfile: userProfile ?? this.userProfile,
failedToLogoutReason:
failedToLogoutReason, // This argument should be cleaned if not set.
// This argument should be cleaned if not set.
failedToLogoutReason: failedToLogoutReason,
hasUnreadNotice: hasUnreadNotice ?? this.hasUnreadNotice,
hasUnreadMessage: hasUnreadMessage ?? this.hasUnreadMessage,
);
}

Expand All @@ -75,5 +87,7 @@ class ProfileState extends Equatable {
uid,
userProfile,
failedToLogoutReason,
hasUnreadNotice,
hasUnreadMessage,
];
}
10 changes: 9 additions & 1 deletion lib/features/profile/view/profile_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,20 @@ class _ProfilePageState extends State<ProfilePage> {
);
}

final profileState = context.read<ProfileBloc>().state;
final hasUnreadInfo =
profileState.hasUnreadNotice || profileState.hasUnreadMessage;

late final List<Widget> actions;
if (widget.username == null && widget.uid == null) {
// Current is current logged user's profile page.
actions = [
IconButton(
icon: const Icon(Icons.notifications_outlined),
icon: hasUnreadInfo
? const Badge(
child: Icon(Icons.notifications_outlined),
)
: const Icon(Icons.notifications_outlined),
onPressed: () async {
await context.pushNamed(ScreenPaths.notice);
},
Expand Down

0 comments on commit 278b5f9

Please sign in to comment.