Skip to content

Commit

Permalink
Исправлен баг с неотображением данных в drawer для сотрудников (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
KriseevM authored Oct 9, 2024
1 parent e04ebd0 commit a8a3f6c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 93 deletions.
4 changes: 4 additions & 0 deletions lib/core/viewmodels/factories/profile_view_model_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ class ProfileViewModelFactory
getService<LoggerService>(),
);
}

ProfileViewModel getCurrentUserViewModel() {
return createViewModel(0)..init(loadCurrentUser: true);
}
}
55 changes: 5 additions & 50 deletions lib/core/viewmodels/main_page_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,60 +1,15 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:unn_mobile/core/misc/current_user_sync_storage.dart';
import 'package:unn_mobile/core/models/student_data.dart';
import 'package:unn_mobile/core/services/interfaces/getting_profile_of_current_user_service.dart';
import 'package:unn_mobile/core/services/interfaces/logger_service.dart';
import 'package:unn_mobile/core/viewmodels/base_view_model.dart';
import 'package:unn_mobile/core/viewmodels/profile_view_model.dart';

class MainPageViewModel extends BaseViewModel {
final LoggerService _loggerService;
final GettingProfileOfCurrentUser _gettingCurrentUser;
final CurrentUserSyncStorage _currentUserSyncStorage;
late ProfileViewModel _profileViewModel;

String _userNameAndSurname = '';
String _userGroup = '';
MainPageViewModel();

ImageProvider<Object>? _userAvatar;

String? _avatarUrl;

MainPageViewModel(
this._gettingCurrentUser,
this._currentUserSyncStorage,
this._loggerService,
);

String? get avatarUrl => _avatarUrl;
ImageProvider<Object>? get userAvatar => _userAvatar;
String get userGroup => _userGroup;
String get userNameAndSurname => _userNameAndSurname;
ProfileViewModel get profileViewModel => _profileViewModel;

void init() {
setState(ViewState.busy);
// _feedUpdaterService.updateFeed();
_gettingCurrentUser.getProfileOfCurrentUser().then(
(value) {
value = value ?? _currentUserSyncStorage.currentUserData;
if (value == null) {
setState(ViewState.idle);
return;
}
if (value is StudentData) {
final StudentData studentProfile = value;
_userNameAndSurname =
'${studentProfile.name} ${studentProfile.lastname}';
_userGroup = studentProfile.eduGroup;
_userAvatar = studentProfile.fullUrlPhoto == null
? null
: CachedNetworkImageProvider(studentProfile.fullUrlPhoto!);
}
setState(ViewState.idle);
},
).onError(
(error, stackTrace) {
_loggerService.logError(error, stackTrace);
setState(ViewState.idle);
},
);
_profileViewModel = ProfileViewModel.currentUser();
}
}
19 changes: 17 additions & 2 deletions lib/core/viewmodels/profile_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:injector/injector.dart';
import 'package:unn_mobile/core/misc/user_functions.dart';
import 'package:unn_mobile/core/models/student_data.dart';
import 'package:unn_mobile/core/models/user_data.dart';
import 'package:unn_mobile/core/services/interfaces/getting_profile.dart';
import 'package:unn_mobile/core/services/interfaces/getting_profile_of_current_user_service.dart';
Expand All @@ -18,22 +19,32 @@ class ProfileViewModel extends BaseViewModel {

UserData? _loadedData;

String? _description;

ProfileViewModel(
this._getCurrentUserService,
this._getProfileService,
this._loggerService,
);

factory ProfileViewModel.cached(ProfileCacheKey key) {
return Injector.appInstance
.get<ProfileViewModelFactory>()
.getViewModel(key);
}

factory ProfileViewModel.currentUser() {
return Injector.appInstance
.get<ProfileViewModelFactory>()
.getCurrentUserViewModel();
}
String? get avatarUrl => _loadedData?.fullUrlPhoto;

String get description => _description ?? '';

String get fullname =>
_loadedData?.fullname.toString() ?? //
'Неизвестный пользователь';
'Не удалось загрузить';

bool get hasAvatar => _loadedData?.fullUrlPhoto != null;

bool get hasError => _hasError;
Expand All @@ -56,6 +67,10 @@ class ProfileViewModel extends BaseViewModel {
(loadCurrentUser ? _getCurrentUser() : _getProfile(userId!, loadFromPost))
.then((data) {
_loadedData = data;
_description = switch (data.runtimeType) {
const (StudentData) => (data as StudentData).eduGroup,
_ => '',
};
}).catchError((error, stack) {
_loggerService.logError(error, stack);
_hasError = true;
Expand Down
6 changes: 1 addition & 5 deletions lib/load_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,7 @@ void registerDependencies() {
),
);
injector.registerDependency(
() => MainPageViewModel(
get<GettingProfileOfCurrentUser>(),
get<CurrentUserSyncStorage>(),
get<LoggerService>(),
),
() => MainPageViewModel(),
);
injector.registerDependency(
() => ScheduleScreenViewModel(
Expand Down
78 changes: 43 additions & 35 deletions lib/ui/views/main_page/main_page_drawer.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:injector/injector.dart';
import 'package:unn_mobile/core/misc/current_user_sync_storage.dart';
import 'package:unn_mobile/core/viewmodels/main_page_view_model.dart';
import 'package:unn_mobile/core/viewmodels/profile_view_model.dart';
import 'package:unn_mobile/ui/views/main_page/main_page_navigation_bar.dart';
import 'package:unn_mobile/ui/views/main_page/main_page_routing.dart';

Expand Down Expand Up @@ -66,7 +68,7 @@ class _MainPageDrawerState extends State<MainPageDrawer> {
final theme = Theme.of(context);

final List<Widget> drawerChildren = [
_getDrawerHeader(theme, vm),
_getDrawerHeader(theme, vm.profileViewModel),
for (final route in routes)
NavigationDrawerDestination(
icon: Icon(route.selectedIcon),
Expand All @@ -77,7 +79,7 @@ class _MainPageDrawerState extends State<MainPageDrawer> {
return drawerChildren;
}

Widget _getDrawerHeader(ThemeData theme, MainPageViewModel value) {
Widget _getDrawerHeader(ThemeData theme, ProfileViewModel value) {
return Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 10),
child: SizedBox(
Expand All @@ -94,52 +96,58 @@ class _MainPageDrawerState extends State<MainPageDrawer> {
width: 72,
height: 72,
child: CircleAvatar(
backgroundImage: value.userAvatar,
child: value.userAvatar == null
backgroundImage: value.hasAvatar
? CachedNetworkImageProvider(value.avatarUrl!)
: null,
child: !value.hasAvatar
? Text(
style: theme.textTheme.headlineLarge!.copyWith(
color: theme.colorScheme.onSurface,
),
value.userNameAndSurname
.replaceAll(RegExp('[а-яё ]'), ''),
value.initials,
)
: null,
),
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(
value.userNameAndSurname,
overflow: TextOverflow.fade,
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: theme.colorScheme.onPrimary,
fontFamily: 'Inter',
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(
value.fullname,
overflow: TextOverflow.ellipsis,
maxLines: 2,
softWrap: true,
textWidthBasis: TextWidthBasis.parent,
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: theme.colorScheme.onPrimary,
fontFamily: 'Inter',
),
),
),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(
value.userGroup,
overflow: TextOverflow.fade,
textAlign: TextAlign.left,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: Color(0xFFFFFFFF),
fontFamily: 'Inter',
Padding(
padding: const EdgeInsets.all(4.0),
child: Text(
value.description,
overflow: TextOverflow.fade,
textAlign: TextAlign.left,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: Color(0xFFFFFFFF),
fontFamily: 'Inter',
),
),
),
),
],
],
),
),
],
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: unn_mobile
description: A mobile application for UNN Portal website
publish_to: 'none'

version: 0.2.3+225
version: 0.2.3+226

environment:
sdk: '>=3.1.2 <4.0.0'
Expand Down

0 comments on commit a8a3f6c

Please sign in to comment.