459 lines
15 KiB
Dart
459 lines
15 KiB
Dart
import 'package:didvan/models/notification_message.dart';
|
|
import 'package:didvan/services/app_initalizer.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:home_widget/home_widget.dart';
|
|
import 'package:persian_number_utility/persian_number_utility.dart';
|
|
import 'package:url_launcher/url_launcher_string.dart';
|
|
|
|
import '../../main.dart';
|
|
import '../../models/requests/infography.dart';
|
|
import '../../models/requests/news.dart';
|
|
import '../../models/requests/radar.dart';
|
|
import '../../models/widget_response.dart';
|
|
import '../../routes/routes.dart';
|
|
import '../network/request.dart';
|
|
import '../network/request_helper.dart';
|
|
|
|
class HomeWidgetRepository {
|
|
static Future<void> fetchWidget() async {
|
|
// RequestService.token = await StorageService.getValue(key: 'token');
|
|
final service = RequestService(
|
|
RequestHelper.widgetNews(),
|
|
);
|
|
await service.httpGet();
|
|
if (service.isSuccess) {
|
|
final favourites = service.data('content');
|
|
HomeWidget.saveWidgetData("token", RequestService.token.toString());
|
|
for (var i = 0; i < favourites.length; i++) {
|
|
HomeWidget.saveWidgetData(
|
|
"id${i + 1}", WidgetResponse.fromJson(favourites[i]).id.toString());
|
|
HomeWidget.saveWidgetData("title${i + 1}",
|
|
WidgetResponse.fromJson(favourites[i]).title.toString());
|
|
HomeWidget.saveWidgetData(
|
|
"createdAt${i + 1}",
|
|
DateTime.parse(
|
|
WidgetResponse.fromJson(favourites[i]).createdAt.toString())
|
|
.toPersianDateStr());
|
|
HomeWidget.saveWidgetData("type${i + 1}",
|
|
WidgetResponse.fromJson(favourites[i]).type.toString());
|
|
HomeWidget.saveWidgetData("link${i + 1}",
|
|
WidgetResponse.fromJson(favourites[i]).link.toString());
|
|
HomeWidget.saveWidgetData("category${i + 1}",
|
|
WidgetResponse.fromJson(favourites[i]).category.toString());
|
|
HomeWidget.saveWidgetData("image${i + 1}",
|
|
WidgetResponse.fromJson(favourites[i]).image.toString());
|
|
}
|
|
|
|
HomeWidget.updateWidget(
|
|
androidName: "FavWidget",
|
|
);
|
|
}
|
|
}
|
|
|
|
static Future<void> decideWhereToGo() async {
|
|
String? uri =
|
|
await HomeWidget.getWidgetData<String>('uri', defaultValue: "");
|
|
if (uri!.isNotEmpty) {
|
|
int row = 0;
|
|
String route = "";
|
|
dynamic args;
|
|
switch (uri) {
|
|
case 'setting':
|
|
route = Routes.favouritesStep;
|
|
args = {"toTimer": false};
|
|
break;
|
|
case 'rowfirst':
|
|
row = 1;
|
|
break;
|
|
case 'rowmiddle':
|
|
row = 2;
|
|
break;
|
|
case 'rowlast':
|
|
row = 3;
|
|
break;
|
|
}
|
|
|
|
if (row != 0) {
|
|
String? id =
|
|
await HomeWidget.getWidgetData<String>("id$row", defaultValue: "");
|
|
WidgetResponse data = WidgetResponse(
|
|
id: int.parse(id!),
|
|
title: await HomeWidget.getWidgetData("title$row", defaultValue: ""),
|
|
createdAt:
|
|
await HomeWidget.getWidgetData("createdAt$row", defaultValue: ""),
|
|
type: await HomeWidget.getWidgetData("type$row", defaultValue: ""),
|
|
link: await HomeWidget.getWidgetData("link$row", defaultValue: ""),
|
|
category:
|
|
await HomeWidget.getWidgetData("category$row", defaultValue: ""),
|
|
image: await HomeWidget.getWidgetData("image$row", defaultValue: ""),
|
|
);
|
|
|
|
if (data.link!.startsWith('http')) {
|
|
AppInitializer.openWebLink(
|
|
navigatorKey.currentContext!,
|
|
'${data.link}',
|
|
mode: LaunchMode.inAppWebView,
|
|
);
|
|
} else {
|
|
switch (data.type!) {
|
|
case "infography":
|
|
route = Routes.infography;
|
|
args = {
|
|
'id': data.id,
|
|
'args': const InfographyRequestArgs(page: 0),
|
|
'hasUnmarkConfirmation': false,
|
|
};
|
|
break;
|
|
case "news":
|
|
route = Routes.newsDetails;
|
|
args = {
|
|
'id': data.id,
|
|
'args': const NewsRequestArgs(page: 0),
|
|
'hasUnmarkConfirmation': false,
|
|
};
|
|
break;
|
|
case "radar":
|
|
route = Routes.radarDetails;
|
|
args = {
|
|
'id': data.id,
|
|
'args': const RadarRequestArgs(page: 0),
|
|
'hasUnmarkConfirmation': false,
|
|
};
|
|
break;
|
|
case "video":
|
|
route = Routes.studioDetails;
|
|
args = {
|
|
'type': 'podcast',
|
|
'id': data.id,
|
|
};
|
|
break;
|
|
case "podcast":
|
|
route = Routes.podcasts;
|
|
args = {
|
|
'type': 'podcast',
|
|
'id': data.id,
|
|
};
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (route.isNotEmpty) {
|
|
Future.delayed(
|
|
const Duration(milliseconds: 500),
|
|
() => Navigator.of(navigatorKey.currentContext!)
|
|
.pushNamed(route.toString(), arguments: args));
|
|
}
|
|
}
|
|
await HomeWidget.saveWidgetData("uri", "");
|
|
|
|
return;
|
|
}
|
|
|
|
static NotificationMessage? data;
|
|
static Future<void> decideWhereToGoNotif() async {
|
|
NotificationMessage? localData = HomeWidgetRepository.data;
|
|
|
|
if (localData == null) {
|
|
if (kDebugMode) {
|
|
print("=== NAVIGATION ABORTED ===");
|
|
print("Reason: Notification data is null");
|
|
print("===========================");
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (RequestService.token == null ||
|
|
RequestService.token.toString().isEmpty) {
|
|
if (kDebugMode) {
|
|
print("⏳ Token not loaded yet. Deferring navigation to Home/MainPage.");
|
|
}
|
|
return;
|
|
}
|
|
|
|
HomeWidgetRepository.data = null;
|
|
|
|
if (kDebugMode) {
|
|
print("=== NAVIGATION DECISION ===");
|
|
print("Notification Data: ${localData.toJson()}");
|
|
print("Type: ${localData.type}");
|
|
print("ID: ${localData.id}");
|
|
print("Link: ${localData.link}");
|
|
print("Notification Type: ${localData.notificationType}");
|
|
}
|
|
|
|
String route = "";
|
|
dynamic args;
|
|
bool openComments = localData.notificationType.toString() == "2";
|
|
|
|
if (localData.link.toString().isEmpty ||
|
|
localData.link.toString() == "null") {
|
|
if (localData.type == null || localData.type!.isEmpty) {
|
|
if (kDebugMode) {
|
|
print("=== NAVIGATION ABORTED ===");
|
|
print("Reason: Notification type is null or empty");
|
|
print("Defaulting to home route");
|
|
print("===========================");
|
|
}
|
|
route = Routes.home;
|
|
} else {
|
|
switch (localData.type!) {
|
|
case "infography":
|
|
if (localData.id == null || localData.id.toString().isEmpty) {
|
|
if (kDebugMode) {
|
|
print(
|
|
"WARNING: Infography notification without ID - navigating to home");
|
|
}
|
|
route = Routes.home;
|
|
} else {
|
|
route = Routes.infography;
|
|
args = {
|
|
'id': int.parse(localData.id.toString()),
|
|
'args': const InfographyRequestArgs(page: 0),
|
|
'hasUnmarkConfirmation': false,
|
|
'goToComment': openComments
|
|
};
|
|
}
|
|
break;
|
|
case "news":
|
|
if (localData.id == null || localData.id.toString().isEmpty) {
|
|
if (kDebugMode) {
|
|
print(
|
|
"WARNING: News notification without ID - navigating to home");
|
|
}
|
|
route = Routes.home;
|
|
} else {
|
|
route = Routes.newsDetails;
|
|
args = {
|
|
'id': int.parse(localData.id.toString()),
|
|
'args': const NewsRequestArgs(page: 0),
|
|
'hasUnmarkConfirmation': false,
|
|
'goToComment': openComments
|
|
};
|
|
if (kDebugMode) {
|
|
print("News navigation - ID: ${localData.id}");
|
|
}
|
|
}
|
|
break;
|
|
case "radar":
|
|
if (localData.id == null || localData.id.toString().isEmpty) {
|
|
if (kDebugMode) {
|
|
print(
|
|
"WARNING: Radar notification without ID - navigating to home");
|
|
}
|
|
route = Routes.home;
|
|
} else {
|
|
route = Routes.radarDetails;
|
|
args = {
|
|
'id': int.parse(localData.id.toString()),
|
|
'args': const RadarRequestArgs(page: 0),
|
|
'hasUnmarkConfirmation': false,
|
|
'goToComment': openComments
|
|
};
|
|
}
|
|
break;
|
|
case "studio":
|
|
if (localData.id == null || localData.id.toString().isEmpty) {
|
|
if (kDebugMode) {
|
|
print(
|
|
"WARNING: Studio notification without ID - navigating to home");
|
|
}
|
|
route = Routes.home;
|
|
} else {
|
|
route = Routes.studioDetails;
|
|
args = {
|
|
'type': 'podcast',
|
|
'id': int.parse(localData.id.toString()),
|
|
'goToComment': openComments
|
|
};
|
|
}
|
|
break;
|
|
case "video":
|
|
if (localData.id == null || localData.id.toString().isEmpty) {
|
|
if (kDebugMode) {
|
|
print(
|
|
"WARNING: Video notification without ID - navigating to home");
|
|
}
|
|
route = Routes.home;
|
|
} else {
|
|
route = Routes.studioDetails;
|
|
args = {
|
|
'type': 'podcast',
|
|
'id': int.parse(localData.id.toString()),
|
|
'goToComment': openComments
|
|
};
|
|
}
|
|
break;
|
|
case "podcast":
|
|
if (localData.id == null || localData.id.toString().isEmpty) {
|
|
if (kDebugMode) {
|
|
print(
|
|
"WARNING: Podcast notification without ID - navigating to home");
|
|
}
|
|
route = Routes.home;
|
|
} else {
|
|
route = Routes.podcasts;
|
|
args = {
|
|
'type': 'podcast',
|
|
'id': int.parse(localData.id.toString()),
|
|
'goToComment': openComments
|
|
};
|
|
}
|
|
break;
|
|
|
|
case "monthly":
|
|
route = Routes.monthlyList;
|
|
break;
|
|
|
|
case "didvanplus":
|
|
case "didvan_plus":
|
|
route = Routes.didvanPlusList;
|
|
args = [];
|
|
break;
|
|
|
|
case "didvanvoice":
|
|
case "didvan_voice":
|
|
route = Routes.didvanVoiceList;
|
|
args = [];
|
|
break;
|
|
case "startup":
|
|
case "technology":
|
|
case "trend":
|
|
if (localData.link != null &&
|
|
localData.link!.isNotEmpty &&
|
|
localData.link! != "null") {
|
|
if (kDebugMode) {
|
|
print(
|
|
"Opening external link for ${localData.type}: ${localData.link}");
|
|
}
|
|
AppInitializer.openWebLink(
|
|
navigatorKey.currentContext!,
|
|
localData.link!,
|
|
mode: LaunchMode.inAppWebView,
|
|
);
|
|
} else if (localData.id != null &&
|
|
localData.id.toString().isNotEmpty) {
|
|
String url = "";
|
|
String title = localData.title?.split(" ").join("-") ?? "";
|
|
|
|
switch (localData.type) {
|
|
case "startup":
|
|
url =
|
|
"https://startup.didvan.app/startup/${localData.id}?accessToken=${RequestService.token}";
|
|
break;
|
|
case "technology":
|
|
url =
|
|
"https://tech.didvan.app/technology/${localData.id}/$title?accessToken=${RequestService.token}";
|
|
break;
|
|
case "trend":
|
|
url =
|
|
"https://trend.didvan.app/trend/${localData.id}/$title?accessToken=${RequestService.token}";
|
|
break;
|
|
}
|
|
|
|
if (url.isNotEmpty) {
|
|
if (kDebugMode) {
|
|
print("Opening constructed URL for ${localData.type}: $url");
|
|
}
|
|
AppInitializer.openWebLink(
|
|
navigatorKey.currentContext!,
|
|
url,
|
|
mode: LaunchMode.inAppWebView,
|
|
);
|
|
} else {
|
|
route = Routes.home;
|
|
if (kDebugMode) {
|
|
print(
|
|
"Unable to construct URL for ${localData.type} - navigating to home");
|
|
}
|
|
}
|
|
} else {
|
|
route = Routes.home;
|
|
if (kDebugMode) {
|
|
print(
|
|
"No ID or link available for ${localData.type} - navigating to home");
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
route = Routes.home;
|
|
if (kDebugMode) {
|
|
print(
|
|
"Unknown notification type: ${localData.type} - navigating to home");
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
if (kDebugMode) {
|
|
print("External link detected: ${localData.link}");
|
|
}
|
|
if (localData.type == 'monthly') {
|
|
route = Routes.pdfViewer;
|
|
args = {
|
|
'pdfUrl': localData.link,
|
|
'title': localData.title ?? 'ماهنامه',
|
|
};
|
|
} else if (localData.link!.startsWith('http')) {
|
|
String linkWithToken = localData.link!;
|
|
if (RequestService.token != null && RequestService.token!.isNotEmpty) {
|
|
String separator = localData.link!.contains('?') ? '&' : '?';
|
|
linkWithToken =
|
|
"${localData.link}${separator}accessToken=${RequestService.token}";
|
|
}
|
|
|
|
if (kDebugMode) {
|
|
print("Opening external link with token: $linkWithToken");
|
|
}
|
|
|
|
AppInitializer.openWebLink(
|
|
navigatorKey.currentContext!,
|
|
linkWithToken,
|
|
mode: LaunchMode.inAppWebView,
|
|
);
|
|
}
|
|
}
|
|
|
|
if (kDebugMode) {
|
|
print("Final navigation decision:");
|
|
print("Route: $route");
|
|
print("Args: $args");
|
|
print("===========================");
|
|
}
|
|
|
|
if (route.isNotEmpty) {
|
|
await Future.delayed(const Duration(milliseconds: 1000));
|
|
|
|
if (kDebugMode) {
|
|
print("Attempting navigation after delay...");
|
|
print("Navigator ready: ${navigatorKey.currentState != null}");
|
|
}
|
|
|
|
int retryCount = 0;
|
|
while (navigatorKey.currentState == null && retryCount < 10) {
|
|
if (kDebugMode) {
|
|
print(
|
|
"Navigator not ready, waiting... (attempt ${retryCount + 1}/10)");
|
|
}
|
|
await Future.delayed(const Duration(milliseconds: 500));
|
|
retryCount++;
|
|
}
|
|
|
|
if (navigatorKey.currentState != null) {
|
|
if (kDebugMode) {
|
|
print("Navigator is ready, performing navigation to: $route");
|
|
}
|
|
navigatorKey.currentState!.pushNamed(route, arguments: args);
|
|
} else {
|
|
if (kDebugMode) {
|
|
print(
|
|
"ERROR: Navigator still not ready after waiting. Navigation aborted.");
|
|
}
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|