Yimaru-Mobile/lib/ui/views/learn_lesson/learn_lesson_viewmodel.dart

109 lines
3.4 KiB
Dart

import 'package:easy_localization/easy_localization.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
import 'package:yimaru_app/app/app.router.dart';
import 'package:yimaru_app/models/learn_lesson.dart';
import 'package:yimaru_app/ui/common/enmus.dart';
import 'package:yimaru_app/ui/common/translations/locale_keys.g.dart';
import '../../../app/app.locator.dart';
import '../../../models/learn_module.dart';
import '../../../models/user.dart';
import '../../../services/authentication_service.dart';
import '../../../services/learn_service.dart';
import '../../../services/status_checker_service.dart';
import '../../common/helper_functions.dart';
class LearnLessonViewModel extends ReactiveViewModel {
// Dependency injection
final _learnService = locator<LearnService>();
final _statusChecker = locator<StatusCheckerService>();
final _navigationService = locator<NavigationService>();
final _authenticationService = locator<AuthenticationService>();
@override
List<ListenableServiceMixin> get listenableServices =>
[_learnService, _authenticationService];
// Current user
User? get _user => _authenticationService.user;
User? get user => _user;
// Learn lessons
final Map<int, String> _refreshedThumbnails = {};
Map<int, String> get refreshedThumbnails => _refreshedThumbnails;
List<LearnLesson> get _lessons => _learnService.lessons;
List<LearnLesson> get lessons => _lessons;
// Navigation
void pop() => _navigationService.back();
Future<void> navigateToLearnPractice(int id) async =>
await _navigationService.navigateToLearnPracticeView(
id: id,
practice: LearnPractices.lesson,
label: LocaleKeys.start_practice.tr(),
title: LocaleKeys.lets_practice_module.tr(),
subtitle: LocaleKeys.ask_you_few_actions.tr(),
);
Future<void> navigateToLearnSubscription() async =>
await _navigationService.navigateToLearnSubscriptionView();
Future<void> navigateToLearnLessonDetail(
{
required int index,
required bool hasPractice,
required LearnLesson lesson,
required LearnModule module}) async =>
await _navigationService.navigateToLearnLessonDetailView(
index: index,
lesson: lesson, module: module, hasPractice: hasPractice);
// Remote api call
// Learn lessons
Future<void> getLessons(int id) async => await runBusyFuture(_getLessons(id),
busyObject: StateObjects.learnLessons);
Future<void> _getLessons(int id) async {
if (await _statusChecker.checkConnection()) {
await _learnService.getLearnLessons(id);
// await refreshLessonImages(_lessons);
}
}
// Get module
LearnModule? getUpdatedLearnModule(int id) {
return _learnService.getLearnModuleById(id);
}
//Refresh image
Future<void> refreshLessonImages(List<LearnLesson> lessons) async {
for (final lesson in lessons) {
final thumbnail = lesson.thumbnail;
if (lesson.id == null || thumbnail == null || thumbnail.isEmpty) {
continue;
}
final String? refreshedUrl = await _learnService.refreshObject(thumbnail);
if (refreshedUrl != null) {
_refreshedThumbnails[lesson.id!] = refreshedUrl;
}
}
}
String getLessonImage(LearnLesson lesson) =>
getReadableUrl(_refreshedThumbnails[lesson.id] ?? '') ?? '';
}