Skip to content

Commit

Permalink
support daily notification
Browse files Browse the repository at this point in the history
Signed-off-by: phuoc <[email protected]>
  • Loading branch information
phuocbitmark committed Nov 5, 2024
1 parent 1ebc970 commit 15e915a
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 31 deletions.
34 changes: 31 additions & 3 deletions lib/model/additional_data/additional_data.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:autonomy_flutter/model/additional_data/cs_view_thread.dart';
import 'package:autonomy_flutter/model/additional_data/daily_notification_data.dart';
import 'package:autonomy_flutter/model/additional_data/jg_crystalline_work_generated.dart';
import 'package:autonomy_flutter/model/additional_data/navigate_additional_data.dart';
import 'package:autonomy_flutter/model/additional_data/view_collection.dart'
Expand All @@ -14,10 +15,12 @@ import 'package:flutter/cupertino.dart';
class AdditionalData {
final NotificationType notificationType;
final String? announcementContentId;
final String? linkText;

AdditionalData({
required this.notificationType,
this.announcementContentId,
this.linkText,
});

bool get isTappable => false;
Expand All @@ -27,10 +30,13 @@ class AdditionalData {
try {
final notificationType =
NotificationType.fromString(type ?? json['notification_type']);
final String? linkText = json['link_text'];

final defaultAdditionalData = AdditionalData(
notificationType: notificationType,
announcementContentId: announcementContentId);
notificationType: notificationType,
announcementContentId: announcementContentId,
linkText: linkText,
);

switch (notificationType) {
case NotificationType.customerSupportNewMessage:
Expand All @@ -44,13 +50,15 @@ class AdditionalData {
issueId: issueId,
notificationType: notificationType,
announcementContentId: announcementContentId,
linkText: linkText,
);
case NotificationType.artworkCreated:
case NotificationType.artworkReceived:
case NotificationType.galleryNewNft:
return view_collection_handler.ViewCollection(
notificationType: notificationType,
announcementContentId: announcementContentId,
linkText: linkText,
);
case NotificationType.newMessage:
final groupId = json['group_id'];
Expand All @@ -62,6 +70,7 @@ class AdditionalData {
groupId: groupId,
notificationType: notificationType,
announcementContentId: announcementContentId,
linkText: linkText,
);
case NotificationType.newPostcardTrip:
case NotificationType.postcardShareExpired:
Expand All @@ -74,13 +83,15 @@ class AdditionalData {
indexID: indexID,
notificationType: notificationType,
announcementContentId: announcementContentId,
linkText: linkText,
);
case NotificationType.jgCrystallineWorkHasArrived:
final jgExhibitionId = JohnGerrardHelper.exhibitionID;
return ViewExhibitionData(
exhibitionId: jgExhibitionId ?? '',
notificationType: notificationType,
announcementContentId: announcementContentId,
linkText: linkText,
);
case NotificationType.jgCrystallineWorkGenerated:
final tokenId = json['token_id'];
Expand All @@ -92,6 +103,7 @@ class AdditionalData {
tokenId: tokenId,
notificationType: notificationType,
announcementContentId: announcementContentId,
linkText: linkText,
);
case NotificationType.exhibitionViewingOpening:
case NotificationType.exhibitionSalesOpening:
Expand All @@ -105,6 +117,7 @@ class AdditionalData {
exhibitionId: exhibitionId,
notificationType: notificationType,
announcementContentId: announcementContentId,
linkText: linkText,
);
case NotificationType.navigate:
final navigationRoute = json['navigation_route'];
Expand All @@ -114,7 +127,22 @@ class AdditionalData {
notificationType: notificationType,
announcementContentId: announcementContentId,
homeIndex: homeIndex,
linkText: linkText,
);
case NotificationType.daily:
final subType = json['notification_sub_type'];
final dailyType = DailyNotificationType.fromString(subType?? '');
if (dailyType == null) {
log.warning('AdditionalData: dailyType is null');
return defaultAdditionalData;
}
return DailyNotificationData(
dailyNotificationType: dailyType,
notificationType: notificationType,
announcementContentId: announcementContentId,
linkText: linkText,
);

default:
return defaultAdditionalData;
}
Expand All @@ -130,5 +158,5 @@ class AdditionalData {
log.info('AdditionalData: handle tap: $notificationType');
}

bool prepareAndDidSuccess() => true;
Future<bool> prepareAndDidSuccess() => Future.value(true);
}
3 changes: 2 additions & 1 deletion lib/model/additional_data/cs_view_thread.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CsViewThread extends AdditionalData {
required this.issueId,
required super.notificationType,
super.announcementContentId,
super.linkText,
});

final CustomerSupportService _customerSupportService =
Expand All @@ -37,7 +38,7 @@ class CsViewThread extends AdditionalData {
}

@override
bool prepareAndDidSuccess() {
Future<bool> prepareAndDidSuccess() async {
_customerSupportService.triggerReloadMessages.value += 1;
unawaited(_customerSupportService.getChatThreads());
if (issueId == memoryValues.viewingSupportThreadIssueID) {
Expand Down
151 changes: 151 additions & 0 deletions lib/model/additional_data/daily_notification_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import 'dart:async';

import 'package:autonomy_flutter/common/injector.dart';
import 'package:autonomy_flutter/model/additional_data/additional_data.dart';
import 'package:autonomy_flutter/model/dailies.dart';
import 'package:autonomy_flutter/screen/app_router.dart';
import 'package:autonomy_flutter/screen/exhibition_details/exhibition_detail_page.dart';
import 'package:autonomy_flutter/screen/feralfile_series/feralfile_series_page.dart';
import 'package:autonomy_flutter/service/feralfile_service.dart';
import 'package:autonomy_flutter/service/navigation_service.dart';
import 'package:autonomy_flutter/util/log.dart';
import 'package:flutter/material.dart';
import 'package:sentry_flutter/sentry_flutter.dart';

enum DailyNotificationType {
viewDaily,
revisitDaily,
viewDailySeries,
viewDailyExhibition,
meetDailyArtist,
displayDailyOnTV,
;

static DailyNotificationType? fromString(String value) {
switch (value) {
case 'view_daily':
return DailyNotificationType.viewDaily;
case 'revisit_daily':
return DailyNotificationType.revisitDaily;
case 'view_daily_series':
return DailyNotificationType.viewDailySeries;
case 'view_daily_exhibition':
return DailyNotificationType.viewDailyExhibition;
case 'meet_daily_artist':
return DailyNotificationType.meetDailyArtist;
case 'display_daily_on_tv':
return DailyNotificationType.displayDailyOnTV;
default:
log.info('DailyNotificationType: fromString: unknown value: $value');
return null;
}
}
}

class DailyNotificationData extends AdditionalData {
final DailyNotificationType dailyNotificationType;

final _navigationService = injector<NavigationService>();
final _feralFileService = injector<FeralFileService>();
DailyToken? _dailyToken;

DailyNotificationData({
required this.dailyNotificationType,
required super.notificationType,
super.announcementContentId,
super.linkText,
});

@override
bool get isTappable => true;

@override
Future<void> handleTap(BuildContext context) async {
log.info('DailyNotificationData: handle tap');
bool isDailyTokenAvailable = await prepareAndDidSuccess();
if (!isDailyTokenAvailable) {
log.warning('Tapped at daily notification: dailyToken is null');
unawaited(Sentry.captureMessage(
'Tapped at daily notification: dailyToken is null'));
return;
}
switch (dailyNotificationType) {
case DailyNotificationType.viewDaily:
case DailyNotificationType.revisitDaily:
await _navigationService.navigatePath(AppRouter.dailyWorkPage);
case DailyNotificationType.viewDailySeries:
final artwork = _dailyToken!.artwork;
if (artwork == null) {
_logAndSendSentryForNullData('artwork');
return;
}
final series = await _feralFileService.getSeries(artwork.seriesID);
if (!context.mounted) {
return;
}
await Navigator.of(context).pushNamed(
AppRouter.feralFileSeriesPage,
arguments: FeralFileSeriesPagePayload(
seriesId: artwork.seriesID,
exhibitionId: series.exhibitionID,
),
);
case DailyNotificationType.viewDailyExhibition:
final artwork = _dailyToken!.artwork;
if (artwork == null) {
_logAndSendSentryForNullData('artwork');
return;
}
final series = await _feralFileService.getSeries(artwork.seriesID);
final exhibition = series.exhibition;
if (exhibition == null) {
_logAndSendSentryForNullData('exhibition');
return;
}
if (!context.mounted) {
return;
}
await Navigator.of(context).pushNamed(
AppRouter.exhibitionDetailPage,
arguments:
ExhibitionDetailPayload(exhibitions: [exhibition], index: 0),
);
case DailyNotificationType.meetDailyArtist:
final artwork = _dailyToken!.artwork;
if (artwork == null) {
_logAndSendSentryForNullData('artwork');
return;
}
final series = await _feralFileService.getSeries(artwork.seriesID);
final alumniID = series.artistAlumniAccountID;
await injector<NavigationService>().openFeralFileArtistPage(alumniID);
case DailyNotificationType.displayDailyOnTV:
await _navigationService.navigatePath(AppRouter.dailyWorkPage);
await Future.delayed(const Duration(milliseconds: 300), () {
dailyWorkKey.currentState?.openDisplayDialog();
});

default:
}
}

void _logAndSendSentryForNullData(String data) {
log.warning('DailyNotificationData: $data is null');
unawaited(Sentry.captureMessage('DailyNotificationData: $data is null'));
}

@override
Future<bool> prepareAndDidSuccess() async {
if (_dailyToken != null) {
return true;
}
_dailyToken = await _feralFileService.getCurrentDailiesToken();
if (_dailyToken == null) {
log.warning('DailyNotificationData: dailyToken is null');
unawaited(
Sentry.captureMessage('DailyNotificationData: dailyToken is null'));
return false;
}
return true;
}
}
3 changes: 2 additions & 1 deletion lib/model/additional_data/jg_crystalline_work_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class JgCrystallineWorkGenerated extends AdditionalData {
required this.tokenId,
required super.notificationType,
super.announcementContentId,
super.linkText,
});

@override
Expand All @@ -29,7 +30,7 @@ class JgCrystallineWorkGenerated extends AdditionalData {
}

@override
bool prepareAndDidSuccess() {
Future<bool> prepareAndDidSuccess() async {
unawaited(injector<ClientTokenService>().refreshTokens());
return true;
}
Expand Down
1 change: 1 addition & 0 deletions lib/model/additional_data/navigate_additional_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class NavigateAdditionalData extends AdditionalData {
required this.navigationRoute,
required super.notificationType,
super.announcementContentId,
super.linkText,
this.homeIndex,
});

Expand Down
3 changes: 2 additions & 1 deletion lib/model/additional_data/view_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ViewCollection extends AdditionalData {
ViewCollection({
required super.notificationType,
super.announcementContentId,
super.linkText,
});

@override
Expand All @@ -29,7 +30,7 @@ class ViewCollection extends AdditionalData {
}

@override
bool prepareAndDidSuccess() {
Future<bool> prepareAndDidSuccess() async {
unawaited(injector<ClientTokenService>().refreshTokens());
return true;
}
Expand Down
1 change: 1 addition & 0 deletions lib/model/additional_data/view_exhibition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ViewExhibitionData extends AdditionalData {
required this.exhibitionId,
required super.notificationType,
super.announcementContentId,
super.linkText,
});

@override
Expand Down
3 changes: 2 additions & 1 deletion lib/model/additional_data/view_new_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ViewNewMessage extends AdditionalData {
required this.groupId,
required super.notificationType,
super.announcementContentId,
super.linkText,
});

final RemoteConfigService _remoteConfigService =
Expand Down Expand Up @@ -66,7 +67,7 @@ class ViewNewMessage extends AdditionalData {
}

@override
bool prepareAndDidSuccess() {
Future<bool> prepareAndDidSuccess() async {
if (!_remoteConfigService.getBool(ConfigGroup.viewDetail, ConfigKey.chat)) {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/model/additional_data/view_postcard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ViewPostcard extends AdditionalData {
required this.indexID,
required super.notificationType,
super.announcementContentId,
super.linkText,
});

@override
Expand Down Expand Up @@ -49,7 +50,7 @@ class ViewPostcard extends AdditionalData {
}

@override
bool prepareAndDidSuccess() {
Future<bool> prepareAndDidSuccess() async {
unawaited(injector<ClientTokenService>().refreshTokens());
return true;
}
Expand Down
1 change: 1 addition & 0 deletions lib/screen/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ import 'package:page_transition/page_transition.dart';
GlobalKey<HomeNavigationPageState> homePageKey = GlobalKey();
GlobalKey<HomeNavigationPageState> homePageNoTransactionKey = GlobalKey();
GlobalKey<FeralfileHomePageState> feralFileHomeKey = GlobalKey();
final GlobalKey<DailyWorkPageState> dailyWorkKey = GlobalKey();

class AppRouter {
static const createPlayListPage = 'create_playlist_page';
Expand Down
Loading

0 comments on commit 15e915a

Please sign in to comment.