Yimaru-Mobile/lib/ui/views/learn_course/learn_course_viewmodel.dart

58 lines
1.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
import 'package:yimaru_app/app/app.router.dart';
import '../../../app/app.locator.dart';
import '../../../models/learn_course.dart';
import '../../../services/api_service.dart';
import '../../../services/status_checker_service.dart';
import '../../common/enmus.dart';
class LearnCourseViewModel extends BaseViewModel {
// Dependency injection
final _apiService = locator<ApiService>();
final _statusChecker = locator<StatusCheckerService>();
final _navigationService = locator<NavigationService>();
// Learn courses
List<LearnCourse> _learnCourses = [];
List<LearnCourse> get learnCourses => _learnCourses;
// Navigation
void pop() => _navigationService.back();
Future<void> navigateToLearnModule(LearnCourse course) async =>
_navigationService.navigateToLearnModuleView(course: course);
Future<void> navigateToLearnPractice(
{required int id, required String level}) async =>
await _navigationService.navigateToLearnPracticeView(
id: id,
level: level,
label: 'Begin Level Practice',
practice: LearnPractices.course,
title: 'Lets Practice Course $level',
subtitle: 'Lets quickly review what youve learned in this level!',
);
// Remote api call
// Learn courses
Future<void> getLearnCourses(int id) async =>
await runBusyFuture(_getLearnCourses(id),
busyObject: StateObjects.learnCourses);
Future<void> _getLearnCourses(int id) async {
if (_learnCourses.isEmpty) {
if (await _statusChecker.checkConnection()) {
_learnCourses = await _apiService.getLearnCourse(id);
_learnCourses
.sort((a, b) => (a.sortOrder ?? 0).compareTo(b.sortOrder ?? 0));
}
}
}
}