Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
quantz-dev committed Apr 15, 2022
2 parents e47d180 + 67607ad commit b22ab79
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 41 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Fixed MAL syncing bug.
- Improve loading speed in splash screen.
- Fixed issue with new feed not properly caching.
- Improved user experience.
Expand Down
14 changes: 12 additions & 2 deletions lib/src/components/animelist/animelist.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:timeago/timeago.dart' as timeago;

import '../../data/index.dart';
import '../../data/mal-user.animelist.dart';
import '../../notification/index.dart';
import '../../services/interface/api.interface.dart';
import '../../services/interface/mal.interface.dart';
import '../../widgets/index.dart';
Expand Down Expand Up @@ -39,11 +40,12 @@ class AnimelistController extends MomentumController<AnimelistModel> {
MalInterface get mal => service<MalInterface>(runtimeType: false);

@override
void onReady() {
void onReady() async {
await waitForFirebaseInit();
_messaging = FirebaseMessaging.instance;
}

bootstrap() {
bootstrap() async {
loadList();
}

Expand Down Expand Up @@ -86,6 +88,14 @@ class AnimelistController extends MomentumController<AnimelistModel> {
model.update(loadingUserAnimeDetails: false);
}

AnimeEntry getAnimeItem(int malId) {
try {
return model.list.firstWhere((x) => x.malId == malId);
} on Exception {
return AnimeEntry();
}
}

Future<MalUserAnimeListStatus?> updateUserAnimeDetails(AnimeEntry anime, int episodeWatched) async {
model.update(loadingUserAnimeDetails: true);
MalUserAnimeListStatus? status;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/components/notification/notification.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/services.dart';
import 'package:momentum/momentum.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../notification/index.dart';
import '../../services/index.dart';
import '../../widgets/index.dart';
import 'index.dart';
Expand All @@ -22,6 +23,7 @@ class NotificationController extends MomentumController<NotificationModel> {

@override
void onReady() async {
await waitForFirebaseInit();
_messaging = FirebaseMessaging.instance;
var message = await _messaging!.getInitialMessage();
if (message != null) {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/components/sources/sources.controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:momentum/momentum.dart';

import '../../misc/index.dart';
import '../../notification/index.dart';
import 'index.dart';

class SourcesController extends MomentumController<SourcesModel> {
Expand All @@ -24,6 +25,7 @@ class SourcesController extends MomentumController<SourcesModel> {

@override
void onReady() async {
await waitForFirebaseInit();
_messaging = FirebaseMessaging.instance;
if (!model.initialized) {
model.update(loading: true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:async';

import 'package:firebase_messaging/firebase_messaging.dart';
// import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/widgets.dart';
import 'package:in_app_purchase/in_app_purchase.dart';
import 'package:momentum/momentum.dart';
Expand Down Expand Up @@ -38,7 +38,7 @@ class SupporterSubscriptionController extends MomentumController<SupporterSubscr
// ignore: cancel_subscriptions
StreamSubscription<List<PurchaseDetails>>? _subscription;

FirebaseMessaging get messaging => FirebaseMessaging.instance;
// FirebaseMessaging get messaging => FirebaseMessaging.instance;

Future<void> initialize() async {
await checkStore();
Expand Down Expand Up @@ -135,11 +135,11 @@ class SupporterSubscriptionController extends MomentumController<SupporterSubscr

Future<void> activateReward() async {
print(['QUANTZ', 'activateReward()']);
await messaging.subscribeToTopic('dev_supporter');
// await messaging.subscribeToTopic('dev_supporter'); // NO longer part of subscriber benefits.
}

Future<void> deactivateReward() async {
print(['QUANTZ', 'deactivateReward()']);
await messaging.unsubscribeFromTopic('dev_supporter');
// await messaging.unsubscribeFromTopic('dev_supporter'); // NO longer part of subscriber benefits.
}
}
3 changes: 2 additions & 1 deletion lib/src/core/initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import 'index.dart';
import 'version.dart';

Future<void> initializer() async {
initFirebaseNotification();

await Future.wait([
initSharedPreferences(),
initFirebaseNotification(),
initLocalNotification(),
checkAppVersion(),
]);
Expand Down
12 changes: 12 additions & 0 deletions lib/src/notification/notification.firebase.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import 'dart:async';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

Completer firebaseInitializer = Completer();
Future<void> waitForFirebaseInit() async {
await firebaseInitializer.future;
}

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
Expand All @@ -17,6 +24,9 @@ Future<void> initFirebaseNotification() async {

FirebaseMessaging messaging = FirebaseMessaging.instance;

final token = await messaging.getToken();
print(['firebase_token', token]);

NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
Expand All @@ -40,6 +50,8 @@ Future<void> initFirebaseNotification() async {
badge: true,
sound: true,
);

if (!firebaseInitializer.isCompleted) firebaseInitializer.complete();
} catch (e) {
print(e);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/services/concretes/google-api.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import '../../core/api_key.dart';
import '../../core/config.dart';
import '../../core/in-app-purchase.dart';
import '../../data/firebase.topics.dart';
import '../../notification/index.dart';
import '../../widgets/toast.dart';
import '../interface/google-api.interface.dart';

Expand Down Expand Up @@ -38,6 +39,7 @@ class GoogleApiService extends GoogleApiInterface {
}

Future<FirebaseSubscription> getFirebaseSubscription() async {
await waitForFirebaseInit();
var token = await FirebaseMessaging.instance.getToken();
final path = '$api/topics/$token';
try {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/widgets/listing/anime.item-integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AnimeItemIntegrationAction extends StatelessWidget {
),
onPressed: () {
Momentum.controller<AnimelistController>(context).getUserAnimeDetails(item);
showMalUpdater(context, item);
showMalUpdater(context, item.malId);
},
),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/widgets/listing/anime.item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class AnimeItem extends StatelessWidget {
SizedBox(width: 12),
Expanded(child: _AnimeEntryDetails(item: item, showType: showType)),
_AnimeItemOrderLabel(item: item),
item.malStatus != null
item.malStatus != null && !topicLoading
? AnimeItemIntegrationAction(
item: item,
fallbackWidget: switchWidget,
Expand Down
56 changes: 24 additions & 32 deletions lib/src/widgets/syncing/mal-updater.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import '../../data/mal-user.animelist.dart';
import '../../data/response.all_anime.dart';
import '../button.dart';

showMalUpdater(BuildContext context, AnimeEntry anime) {
showMalUpdater(BuildContext context, int malId) {
showDialog(
barrierDismissible: false,
context: context,
builder: (context) {
return WillPopScope(
onWillPop: () async => false,
child: _MalUpdater(anime: anime),
child: _MalUpdater(malId: malId),
);
},
);
Expand All @@ -23,35 +23,35 @@ showMalUpdater(BuildContext context, AnimeEntry anime) {
class _MalUpdater extends StatefulWidget {
const _MalUpdater({
Key? key,
required this.anime,
required this.malId,
}) : super(key: key);

final AnimeEntry anime;
final int malId;

@override
__MalUpdaterState createState() => __MalUpdaterState();
}

class __MalUpdaterState extends State<_MalUpdater> {
MalUserAnimeListStatus? _status;

MalUserAnimeListStatus get status => _status!;

String statusText = '';
MalUserAnimeListStatus get status => anime.malStatus ?? MalUserAnimeListStatus();

AnimelistController? _animelistController;

AnimelistController get animelistController => _animelistController!;

AnimeEntry get anime {
return animelistController.getAnimeItem(widget.malId);
}

String get latestEpisode {
if (widget.anime.latestEpisode > 0) {
return '${widget.anime.latestEpisode}';
if (anime.latestEpisode > 0) {
return '${anime.latestEpisode}';
}
return '?';
}

int get behindEpisodes {
final latest = widget.anime.latestEpisode;
final latest = anime.latestEpisode;
if (latest > 0 && latest > status.numEpisodesWatched) {
return (latest - status.numEpisodesWatched).toInt();
}
Expand All @@ -61,9 +61,7 @@ class __MalUpdaterState extends State<_MalUpdater> {
@override
void didChangeDependencies() {
super.didChangeDependencies();
_status = widget.anime.malStatus;
_animelistController = Momentum.controller<AnimelistController>(context);
updateStatusText(_status);
}

@override
Expand Down Expand Up @@ -92,7 +90,7 @@ class __MalUpdaterState extends State<_MalUpdater> {
mainAxisSize: MainAxisSize.min,
children: [
Text(
widget.anime.displayTitle,
anime.displayTitle,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
Expand Down Expand Up @@ -150,7 +148,7 @@ class __MalUpdaterState extends State<_MalUpdater> {
color: Colors.green,
)
: Text(
statusText,
getStatusText(status),
style: TextStyle(
color: Colors.white.withOpacity(0.5),
),
Expand All @@ -165,12 +163,12 @@ class __MalUpdaterState extends State<_MalUpdater> {
Expanded(
child: CustomButton(
textColor: Colors.blueAccent,
text: widget.anime.following ? 'Unfollow' : 'Follow',
text: anime.following ? 'Unfollow' : 'Follow',
color: Colors.transparent,
onPressed: () {
Momentum.controller<AnimelistController>(context).toggleTopic(
widget.anime,
status: !widget.anime.following,
anime,
status: !anime.following,
flagEntry: true,
);
Navigator.pop(context);
Expand All @@ -196,32 +194,26 @@ class __MalUpdaterState extends State<_MalUpdater> {
}

Future<void> updateEpisode(int episodeWatched) async {
final updated = await animelistController.updateUserAnimeDetails(widget.anime, episodeWatched);
final updated = await animelistController.updateUserAnimeDetails(anime, episodeWatched);
if (updated != null) {
_status = updated;
updateStatusText(_status);
if (mounted) {
setState(() {});
}
}
}

void updateStatusText(MalUserAnimeListStatus? statusDetails) {
String getStatusText(MalUserAnimeListStatus? statusDetails) {
switch (statusDetails?.status) {
case "plan_to_watch":
statusText = "You\'re still planning to watch this.";
break;
return "You\'re still planning to watch this.";
case "completed":
statusText = "You already finished watching this.";
break;
return "You already finished watching this.";
case "dropped":
statusText = "You've stopped watching this.";
break;
return "You've stopped watching this.";
case null:
statusText = "There was an error getting details.";
break;
return "There was an error getting details.";
default:
statusText = 'You\'re behind $behindEpisodes episode${behindEpisodes <= 1 ? "" : "s"}';
return 'You\'re behind $behindEpisodes episode${behindEpisodes <= 1 ? "" : "s"}';
}
}
}

0 comments on commit b22ab79

Please sign in to comment.