111 lines
3.0 KiB
Dart
111 lines
3.0 KiB
Dart
import 'package:didvan/models/enums.dart';
|
|
import 'package:didvan/models/home_page_content/home_page_content.dart';
|
|
import 'package:didvan/models/requests/infography.dart';
|
|
import 'package:didvan/models/requests/news.dart';
|
|
import 'package:didvan/models/requests/radar.dart';
|
|
import 'package:didvan/providers/core.dart';
|
|
import 'package:didvan/routes/routes.dart';
|
|
import 'package:didvan/services/app_initalizer.dart';
|
|
import 'package:didvan/services/network/request.dart';
|
|
import 'package:didvan/services/network/request_helper.dart';
|
|
import 'package:didvan/utils/action_sheet.dart';
|
|
import 'package:flutter/material.dart';
|
|
// import 'package:url_launcher/url_launcher_string.dart';
|
|
|
|
class MainPageState extends CoreProvier {
|
|
late MainPageContent content;
|
|
int unread = 0;
|
|
|
|
Future<void> _getMainPageContent() async {
|
|
final service = RequestService(RequestHelper.mainPageContent);
|
|
|
|
await service.httpGet();
|
|
if (service.isSuccess) {
|
|
content = MainPageContent.fromJson(service.result);
|
|
unread = service.result['unread'];
|
|
appState = AppState.idle;
|
|
return;
|
|
}
|
|
appState = AppState.failed;
|
|
}
|
|
|
|
void init() {
|
|
Future.delayed(Duration.zero, () {
|
|
_getMainPageContent();
|
|
});
|
|
}
|
|
|
|
void markChangeHandler(String type, int id, bool value) {
|
|
content.lists
|
|
.firstWhere((element) => element.type == type)
|
|
.contents
|
|
.firstWhere((element) => element.id == id)
|
|
.marked = value;
|
|
notifyListeners();
|
|
}
|
|
|
|
void navigationHandler(
|
|
String type,
|
|
int id,
|
|
String? link,
|
|
) {
|
|
link = link ?? '';
|
|
dynamic args;
|
|
switch (type) {
|
|
case 'infography':
|
|
{
|
|
link = Routes.infography;
|
|
args = {
|
|
'onMarkChanged': (id, value) => markChangeHandler(type, id, value),
|
|
'id': id,
|
|
'args': const InfographyRequestArgs(page: 0),
|
|
'hasUnmarkConfirmation': false,
|
|
};
|
|
break;
|
|
}
|
|
case 'news':
|
|
{
|
|
link = Routes.newsDetails;
|
|
args = {
|
|
'onMarkChanged': (id, value) => markChangeHandler(type, id, value),
|
|
'id': id,
|
|
'args': const NewsRequestArgs(page: 0),
|
|
'hasUnmarkConfirmation': false,
|
|
};
|
|
break;
|
|
}
|
|
case 'radar':
|
|
{
|
|
link = Routes.radarDetails;
|
|
args = {
|
|
'onMarkChanged': (id, value) => markChangeHandler(type, id, value),
|
|
'id': id,
|
|
'args': const RadarRequestArgs(page: 0),
|
|
'hasUnmarkConfirmation': false,
|
|
};
|
|
break;
|
|
}
|
|
case 'video':
|
|
{
|
|
link = Routes.studioDetails;
|
|
args = {
|
|
'type': 'podcast',
|
|
'id': id,
|
|
};
|
|
break;
|
|
}
|
|
}
|
|
if (link == '') {
|
|
return;
|
|
}
|
|
if (link.startsWith('http')) {
|
|
launchUrlString(
|
|
'$link?accessToken=${RequestService.token}',
|
|
mode: LaunchMode.inAppWebView,
|
|
);
|
|
return;
|
|
}
|
|
Navigator.of(ActionSheetUtils.context).pushNamed(link, arguments: args);
|
|
}
|
|
}
|