Yimaru-Mobile/lib/ui/views/home/home_viewmodel.dart
2026-02-19 10:43:31 +03:00

140 lines
4.4 KiB
Dart

import 'package:yimaru_app/app/app.bottomsheets.dart';
import 'package:yimaru_app/app/app.dialogs.dart';
import 'package:yimaru_app/app/app.locator.dart';
import 'package:yimaru_app/app/app.router.dart';
import 'package:yimaru_app/models/user_model.dart';
import 'package:yimaru_app/services/status_checker_service.dart';
import 'package:yimaru_app/ui/common/app_strings.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';
import 'package:yimaru_app/ui/views/failure/failure_view.dart';
import '../../../services/api_service.dart';
import '../../../services/authentication_service.dart';
import '../../../services/image_downloader_service.dart';
import '../../common/enmus.dart';
class HomeViewModel extends ReactiveViewModel {
final _apiService = locator<ApiService>();
final _dialogService = locator<DialogService>();
final _statusChecker = locator<StatusCheckerService>();
final _navigationService = locator<NavigationService>();
final _bottomSheetService = locator<BottomSheetService>();
final _authenticationService = locator<AuthenticationService>();
final _imageDownloaderService = locator<ImageDownloaderService>();
@override
List<ListenableServiceMixin> get listenableServices =>
[_authenticationService];
// Current user
UserModel? get _user => _authenticationService.user;
UserModel? get user => _user;
// Bottom navigation
int _currentIndex = 0;
int get currentIndex => _currentIndex;
// Bottom navigation
void setCurrentIndex(int index) {
_currentIndex = index;
rebuildUi();
}
void showDialog() {
_dialogService.showCustomDialog(
title: 'Stacked Rocks!',
variant: DialogType.infoAlert,
description: 'Give stacked stars on Github',
);
}
void showBottomSheet() {
_bottomSheetService.showCustomSheet(
title: ksHomeBottomSheetTitle,
variant: BottomSheetType.notice,
description: ksHomeBottomSheetDescription,
);
}
Future<void> saveProfileStatus(bool value) async =>
await _authenticationService.saveProfileStatus(value);
// Navigation
Future<void> replaceWithFailure() async =>
await _navigationService.clearStackAndShow(Routes.failureView,
arguments: 'Check your internet connection to proceed');
Future<void> replaceWithOnboarding() async =>
await _navigationService.replaceWithOnboardingView();
// Remote api calls
// Initialize user data
Future<void> initialize() async =>
await runBusyFuture(_initialize(), busyObject: StateObjects.homeView);
Future<void> _initialize() async {
await _getProfileStatus();
await _getProfileData();
}
// Profile status
Future<void> _getProfileStatus() async {
Map<String, dynamic> response = {};
if (_user?.profileCompleted == null) {
if (await _statusChecker.checkConnection()) {
print('BREAK-POINT 1');
response = await _apiService.getProfileStatus(_user);
} else {
print('BREAK-POINT 2');
await replaceWithFailure();
}
} else if (!(_user?.profileCompleted ?? false)) {
print('BREAK-POINT 3');
response = {'data': false, 'status': ResponseStatus.success};
} else {
print('BREAK-POINT 4');
response = {'data': true, 'status': ResponseStatus.success};
}
if (response['status'] == ResponseStatus.success && !response['data']) {
print('BREAK-POINT 5');
await replaceWithOnboarding();
} else if (response['status'] == ResponseStatus.success &&
response['data']) {
print('BREAK-POINT 6');
await saveProfileStatus(response['data']);
}
}
// Profile data
Future<void> _getProfileData() async {
if (!(_user?.userInfoLoaded ?? false)) {
Map<String, dynamic> response = {};
print('BREAK-POINT: Profile-Completed ${_user?.profileCompleted}');
if (_user?.profileCompleted != null &&
(_user?.profileCompleted ?? false)) {
if (await _statusChecker.checkConnection()) {
response = await _apiService.getProfileData(_user?.userId);
if (response['status'] == ResponseStatus.success) {
print('BREAK-POINT 8');
UserModel user = response['data'] as UserModel;
String image =
await _imageDownloaderService.downloader(user.profilePicture);
await _authenticationService.saveUserData(image: image, data: user);
}
}
}
}
}
}