Skip to content

Commit

Permalink
Переделанная навигация (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
KriseevM authored Sep 14, 2024
1 parent cd72189 commit 1e82b76
Show file tree
Hide file tree
Showing 21 changed files with 458 additions and 434 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,5 @@ android/app/google-services.json
ios/firebase_app_id_file.json
lib/firebase_options.dart
devtools_options.yaml

firebase.json
11 changes: 6 additions & 5 deletions lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import 'package:flutter/material.dart';
import 'package:unn_mobile/core/models/subject.dart';
import 'package:unn_mobile/ui/router.dart' as router;
import 'package:unn_mobile/ui/router.dart';
import 'package:unn_mobile/ui/unn_mobile_colors.dart';
import 'package:unn_mobile/ui/views/loading_page/loading_page.dart';

class UnnMobile extends StatelessWidget {
const UnnMobile({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: const LoadingPage(),
onGenerateRoute: router.Router.generateRoute,
return MaterialApp.router(
routerConfig: mainRouter,
theme: ThemeData(
appBarTheme: const AppBarTheme(
elevation: 0,
),
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xAA1A63B7),
Expand Down
2 changes: 1 addition & 1 deletion lib/core/viewmodels/feed_screen_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FeedScreenViewModel extends BaseViewModel {

bool isNewPost(DateTime dateTimePublish) {
_lastViewedPostDateTime ??= _feedStreamUpdater.lastViewedPostDateTime;
return _lastViewedPostDateTime!.isBefore(dateTimePublish);
return _lastViewedPostDateTime?.isBefore(dateTimePublish) ?? true;
}

void loadNextPage() {
Expand Down
15 changes: 8 additions & 7 deletions lib/core/viewmodels/loading_page_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:go_router/go_router.dart';
import 'package:unn_mobile/core/misc/app_open_tracker.dart';
import 'package:unn_mobile/core/misc/app_settings.dart';
import 'package:unn_mobile/core/misc/loading_pages.dart';
Expand Down Expand Up @@ -39,7 +40,7 @@ class LoadingPageViewModel extends BaseViewModel {

LoadingPageModel get loadingPageData => actualLoadingPage;

void disateRoute(context) {
void decideRoute(context) {
_init().then((value) => _goToScreen(context, value));
}

Expand Down Expand Up @@ -69,12 +70,12 @@ class LoadingPageViewModel extends BaseViewModel {
return typeScreen;
}

void _goToScreen(context, _TypeScreen typeScreen) {
final routes = switch (typeScreen) {
_TypeScreen.authScreen => Routes.authPage,
_TypeScreen.mainScreen => Routes.mainPagePrefix,
void _goToScreen(BuildContext context, _TypeScreen typeScreen) {
final route = switch (typeScreen) {
_TypeScreen.authScreen => authPageRoute,
_TypeScreen.mainScreen => mainPageRoute,
};
Navigator.of(context!).pushNamedAndRemoveUntil(routes, (route) => false);
GoRouter.of(context).go(route);
}

Future<void> _initUserData() async {
Expand Down
50 changes: 8 additions & 42 deletions lib/core/viewmodels/main_page_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ 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/feed_stream_updater_service.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';

class MainPageViewModel extends BaseViewModel {
final LoggerService _loggerService;
final GettingProfileOfCurrentUser _gettingCurrentUser;
final CurrentUserSyncStorage _currentUserSyncStorage;
final FeedUpdaterService _feedUpdaterService;
int _selectedDrawerItem = 0;
int _selectedBarItem = 1;
bool _isDrawerItemSelected = false;
String _userNameAndSurname = '';
String _userGroup = '';

Expand All @@ -22,49 +21,11 @@ class MainPageViewModel extends BaseViewModel {
this._gettingCurrentUser,
this._currentUserSyncStorage,
this._feedUpdaterService,
this._loggerService,
);

String? get avatarUrl => _avatarUrl;
String? _avatarUrl;

int get selectedDrawerItem => _selectedDrawerItem;
set selectedDrawerItem(int value) {
_selectedDrawerItem = value;
notifyListeners();
}

int get selectedBarItem => _selectedBarItem;
set selectedBarItem(value) {
_selectedBarItem = value;
notifyListeners();
}

bool get isDrawerItemSelected => _isDrawerItemSelected;
set isDrawerItemSelected(bool value) {
_isDrawerItemSelected = value;
notifyListeners();
}

final List<String> _barScreenNames = [
'Лента',
'Расписание',
'Карта',
'Материалы',
];
final List<String> _drawerScreenNames = [
'Зачётная книжка',
'Настройки',
'О нас',
];
String get selectedScreenName {
return isDrawerItemSelected
? _drawerScreenNames[_selectedDrawerItem]
: _barScreenNames[_selectedBarItem];
}

List<String> get drawerScreenNames => _drawerScreenNames;
List<String> get barScreenNames => _barScreenNames;

ImageProvider<Object>? get userAvatar => _userAvatar;
String get userNameAndSurname => _userNameAndSurname;
String get userGroup => _userGroup;
Expand All @@ -90,6 +51,11 @@ class MainPageViewModel extends BaseViewModel {
}
setState(ViewState.idle);
},
).onError(
(error, stackTrace) {
_loggerService.logError(error, stackTrace);
setState(ViewState.idle);
},
);
}
}
1 change: 1 addition & 0 deletions lib/load_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ void registerDependencies() {
get<GettingProfileOfCurrentUser>(),
get<CurrentUserSyncStorage>(),
get<FeedUpdaterService>(),
get<LoggerService>(),
),
);
injector.registerDependency(
Expand Down
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void main() async {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
);

await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
Expand Down
90 changes: 51 additions & 39 deletions lib/ui/router.dart
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:unn_mobile/ui/views/auth_page/auth_page.dart';
import 'package:unn_mobile/ui/views/loading_page/loading_page.dart';
import 'package:unn_mobile/ui/views/main_page/main_page.dart';
import 'package:unn_mobile/ui/views/main_page/main_page_routing.dart';

class Routes {
static const String mainPagePrefix = 'main/';
static const String authPage = 'auth';
static const String loadingPage = 'loading';
}
const loadingPageRoute = '/loading';
const mainPageRoute = '/';
const authPageRoute = '/auth';

class Router {
static Route<dynamic> generateRoute(RouteSettings settings) {
if (settings.name == Routes.authPage) {
return createCustomRoute(const AuthPage());
} else if (settings.name == Routes.loadingPage) {
return createCustomRoute(const LoadingPage());
} else if (settings.name!.startsWith(Routes.mainPagePrefix)) {
return createCustomRoute(
MainPage(
subroute: settings.name!.substring(Routes.mainPagePrefix.length),
),
);
} else {
return createCustomRoute(
Scaffold(
body: Center(
child: Text('No route defined for ${settings.name}'),
final mainRouter = GoRouter(
initialLocation: loadingPageRoute,
routes: [
ShellRoute(
builder: (context, state, child) => MainPage(
child: child,
),
routes: [
for (final route in MainPageRouting.navbarRoutes)
GoRoute(
path: route.pageRoute,
name: route.pageTitle,
pageBuilder: (context, state) =>
NoTransitionPage(child: route.builder(context)),
routes: [
for (final subroute in MainPageRouting.drawerRoutes)
GoRoute(
path: subroute.pageRoute,
builder: (context, state) => subroute.builder(context),
),
for (final subroute in route.subroutes)
GoRoute(
path: subroute.pageRoute,
builder: (context, state) => subroute.builder(context),
),
],
),
),
);
],
),
GoRoute(
path: authPageRoute,
name: 'auth',
builder: (context, state) => const AuthPage(),
),
GoRoute(
path: loadingPageRoute,
pageBuilder: (context, state) => const NoTransitionPage(
child: LoadingPage(),
),
),
],
redirect: (context, state) {
if (state.uri.path == mainPageRoute) {
return MainPageRouting.navbarRoutes.first.pageRoute;
}
}

static Route<dynamic> createCustomRoute(
page, {
duration = const Duration(milliseconds: 0),
}) {
return PageRouteBuilder(
transitionDuration: duration,
pageBuilder: (context, animation, secondaryAnimation) => page,
transitionsBuilder: (context, animation, secondaryAnimation, child) =>
child,
);
}
}
return null;
},
);
3 changes: 2 additions & 1 deletion lib/ui/views/auth_page/auth_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:go_router/go_router.dart';
import 'package:injector/injector.dart';
import 'package:unn_mobile/core/misc/app_open_tracker.dart';
import 'package:unn_mobile/core/viewmodels/auth_page_view_model.dart';
Expand Down Expand Up @@ -359,7 +360,7 @@ class AuthPageWithState extends State<AuthPage> {
.then((isLoginSuccess) {
if (isLoginSuccess) {
if (context.mounted) {
Navigator.pushReplacementNamed(context, Routes.loadingPage);
GoRouter.of(context).go(loadingPageRoute);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/views/loading_page/loading_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LoadingPage extends StatelessWidget {
),
),
),
onModelReady: (model) => model.disateRoute(context),
onModelReady: (model) => model.decideRoute(context),
);
}

Expand Down
41 changes: 22 additions & 19 deletions lib/ui/views/main_page/about/about.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@ class AboutScreenView extends StatelessWidget {

@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(top: 15, bottom: 100),
child: Column(
children: [
Column(
children: _authors
.map((authorProfile) => _AuthorProfileWidget(authorProfile))
.toList(),
),
const Text(
'По всем вопросам можно обращаться: unnmobile@mail.ru',
textAlign: TextAlign.center,
style: TextStyle(
color: Color(0xFF717A84),
fontFamily: 'Inter',
fontSize: 13,
return Scaffold(
appBar: AppBar(title: const Text('О нас')),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(top: 15, bottom: 100),
child: Column(
children: [
Column(
children: _authors
.map((authorProfile) => _AuthorProfileWidget(authorProfile))
.toList(),
),
),
],
const Text(
'По всем вопросам можно обращаться: unnmobile@mail.ru',
textAlign: TextAlign.center,
style: TextStyle(
color: Color(0xFF717A84),
fontFamily: 'Inter',
fontSize: 13,
),
),
],
),
),
),
);
Expand Down
Loading

0 comments on commit 1e82b76

Please sign in to comment.