180 lines
6.1 KiB
Dart
180 lines
6.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:tm_app/data/repositories/auth_repository.dart';
|
|
import 'package:tm_app/data/services/model/company_profile_data/company_profile_data.dart';
|
|
import 'package:tm_app/data/services/model/company_profile_response/company_profile_response.dart';
|
|
import 'package:tm_app/data/services/model/profile_data/profile_data.dart';
|
|
|
|
import '../core/utils/result.dart';
|
|
import '../data/repositories/profile_repository.dart';
|
|
import '../data/services/model/logout_response/logout_response.dart';
|
|
import '../data/services/model/profile_response/profile_response.dart';
|
|
import 'auth_view_model.dart';
|
|
|
|
class ProfileViewModel with ChangeNotifier {
|
|
final ProfileRepository _profileRepository;
|
|
final AuthRepository _authRepository;
|
|
final AuthViewModel _authViewModel;
|
|
|
|
ProfileViewModel({
|
|
required ProfileRepository profileRepository,
|
|
required AuthRepository authRepository,
|
|
required AuthViewModel authViewModel,
|
|
}) : _profileRepository = profileRepository,
|
|
_authRepository = authRepository,
|
|
_authViewModel = authViewModel;
|
|
|
|
bool _isLoading = false;
|
|
String? _errorMessage;
|
|
ProfileData? _profileData;
|
|
CompanyProfileData? _companyProfileData;
|
|
bool _loggedOut = false;
|
|
|
|
// Notification info saved from FCM message in SharedPreferences
|
|
String? _notifData;
|
|
String? _notifFrom;
|
|
String? _notifType;
|
|
String? _notifKeys;
|
|
String? _notifTitle;
|
|
String? _notifBody;
|
|
String? _notifWebLink;
|
|
String? _notifAndroidLink;
|
|
String? _notifWebImage;
|
|
String? _notifAndroidImage;
|
|
String? _notifWebClickAction;
|
|
|
|
bool get isLoading => _isLoading;
|
|
bool get loggedOut => _loggedOut;
|
|
String? get errorMessage => _errorMessage;
|
|
ProfileData? get profileData => _profileData;
|
|
CompanyProfileData? get companyProfileData => _companyProfileData;
|
|
|
|
// Expose notification info
|
|
String? get notifData => _notifData;
|
|
String? get notifFrom => _notifFrom;
|
|
String? get notifType => _notifType;
|
|
String? get notifKeys => _notifKeys;
|
|
String? get notifTitle => _notifTitle;
|
|
String? get notifBody => _notifBody;
|
|
String? get notifWebLink => _notifWebLink;
|
|
String? get notifAndroidLink => _notifAndroidLink;
|
|
String? get notifWebImage => _notifWebImage;
|
|
String? get notifAndroidImage => _notifAndroidImage;
|
|
String? get notifWebClickAction => _notifWebClickAction;
|
|
|
|
String get notifSummaryText {
|
|
final parts = <String>[];
|
|
if (_notifTitle != null && _notifTitle!.isNotEmpty)
|
|
parts.add('Title: ${_notifTitle!}');
|
|
if (_notifBody != null && _notifBody!.isNotEmpty)
|
|
parts.add('Body: ${_notifBody!}');
|
|
if (_notifType != null && _notifType!.isNotEmpty)
|
|
parts.add('Type: ${_notifType!}');
|
|
if (_notifFrom != null && _notifFrom!.isNotEmpty)
|
|
parts.add('From: ${_notifFrom!}');
|
|
if (_notifKeys != null && _notifKeys!.isNotEmpty)
|
|
parts.add('Keys: ${_notifKeys!}');
|
|
if (_notifWebLink != null && _notifWebLink!.isNotEmpty)
|
|
parts.add('Web: ${_notifWebLink!}');
|
|
if (_notifAndroidLink != null && _notifAndroidLink!.isNotEmpty)
|
|
parts.add('Android: ${_notifAndroidLink!}');
|
|
if (_notifWebImage != null && _notifWebImage!.isNotEmpty)
|
|
parts.add('WebImg: ${_notifWebImage!}');
|
|
if (_notifAndroidImage != null && _notifAndroidImage!.isNotEmpty)
|
|
parts.add('AndroidImg: ${_notifAndroidImage!}');
|
|
if (_notifWebClickAction != null && _notifWebClickAction!.isNotEmpty)
|
|
parts.add('Click: ${_notifWebClickAction!}');
|
|
if (_notifData != null && _notifData!.isNotEmpty)
|
|
parts.add('Data: ${_notifData!}');
|
|
return parts.isEmpty ? '-' : parts.join(' | ');
|
|
}
|
|
|
|
Future<void> getProfile() async {
|
|
_isLoading = true;
|
|
_errorMessage = null;
|
|
notifyListeners();
|
|
|
|
final result = await _profileRepository.getProfile();
|
|
|
|
switch (result) {
|
|
case Ok<ProfileResponse>():
|
|
_profileData = result.value.data;
|
|
break;
|
|
case Error<ProfileResponse>():
|
|
_errorMessage = result.error.toString();
|
|
break;
|
|
}
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
_errorMessage = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> getCompanyProfile() async {
|
|
_isLoading = true;
|
|
_errorMessage = null;
|
|
notifyListeners();
|
|
|
|
final result = await _profileRepository.getCompanyProfile();
|
|
|
|
switch (result) {
|
|
case Ok<CompanyProfileResponse>():
|
|
_companyProfileData = result.value.data;
|
|
break;
|
|
case Error<CompanyProfileResponse>():
|
|
_errorMessage = result.error.toString();
|
|
break;
|
|
}
|
|
_isLoading = false;
|
|
notifyListeners();
|
|
_errorMessage = null;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> logout() async {
|
|
_errorMessage = null;
|
|
notifyListeners();
|
|
|
|
final result = await _authRepository.logout();
|
|
|
|
switch (result) {
|
|
case Ok<LogoutResponse>():
|
|
await _authViewModel.localLogout();
|
|
_loggedOut = true;
|
|
break;
|
|
case Error<LogoutResponse>():
|
|
_errorMessage = result.error.toString();
|
|
break;
|
|
}
|
|
|
|
notifyListeners();
|
|
_errorMessage = null;
|
|
_loggedOut = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> loadNotificationInfoFromPrefs() async {
|
|
// Do not toggle global loading; this is a lightweight local load
|
|
try {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
_notifData = prefs.getString('notif_data');
|
|
_notifFrom = prefs.getString('notif_from');
|
|
_notifType = prefs.getString('notif_type');
|
|
_notifKeys = prefs.getString('notif_keys');
|
|
_notifTitle = prefs.getString('notif_title');
|
|
_notifBody = prefs.getString('notif_body');
|
|
_notifWebLink = prefs.getString('notif_web_link');
|
|
_notifAndroidLink = prefs.getString('notif_android_link');
|
|
_notifWebImage = prefs.getString('notif_web_image');
|
|
_notifAndroidImage = prefs.getString('notif_android_image');
|
|
_notifWebClickAction = prefs.getString('notif_web_click_action');
|
|
} catch (e) {
|
|
// Non-fatal; surface as errorMessage so UI can toast if listening
|
|
_errorMessage = e.toString();
|
|
}
|
|
notifyListeners();
|
|
_errorMessage = null;
|
|
notifyListeners();
|
|
}
|
|
}
|