Skip to content

Commit

Permalink
add home page as app default page
Browse files Browse the repository at this point in the history
  • Loading branch information
smart7even committed Aug 22, 2024
1 parent 7209acd commit 51a0e13
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 63 deletions.
8 changes: 6 additions & 2 deletions lib/common/routing/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:octopus/octopus.dart';
import 'package:uneconly/common/model/short_group_info.dart';
import 'package:uneconly/feature/loading/widget/loading_page.dart';
import 'package:uneconly/feature/schedule/widget/home_page.dart';
import 'package:uneconly/feature/schedule/widget/schedule_page.dart';
import 'package:uneconly/feature/select/widget/select_page.dart';
import 'package:uneconly/feature/settings/widget/settings_page.dart';
Expand All @@ -10,7 +11,8 @@ enum Routes with OctopusRoute {
loading('loading', title: 'Loading'),
schedule('schedule', title: 'Schedule'),
select('select', title: 'Select'),
settings('settings', title: 'Settings');
settings('settings', title: 'Settings'),
home('home', title: 'Home');

const Routes(this.name, {this.title});

Expand All @@ -35,7 +37,7 @@ enum Routes with OctopusRoute {
),
isViewMode: node.arguments['isViewMode'] != null
? node.arguments['isViewMode'] as String == 'true'
: false,
: true,
);
case Routes.select:
return SelectPage(
Expand All @@ -45,6 +47,8 @@ enum Routes with OctopusRoute {
);
case Routes.settings:
return const SettingsPage();
case Routes.home:
return const HomePage();
}
}
}
43 changes: 43 additions & 0 deletions lib/common/routing/schedule_guard.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'dart:async';

import 'package:octopus/octopus.dart';
import 'package:uneconly/common/routing/routes.dart';
import 'package:uneconly/feature/select/model/group.dart';

class ScheduleGuard extends OctopusGuard {
ScheduleGuard({
super.refresh,
});

@override
FutureOr<OctopusState> call(
List<OctopusHistoryEntry> history,
OctopusState$Mutable state,
Map<String, Object?> context,
) async {
if (state.children.isEmpty) {
return OctopusState.fromNodes(
[
Routes.home.node(),
],
);
}

final firstChild = state.children.first;

if (![
Routes.home.name,
Routes.loading.name,
Routes.select.name,
].contains(firstChild.name)) {
return OctopusState.fromNodes(
[
Routes.home.node(),
...state.children,
],
);
}

return state;
}
}
54 changes: 9 additions & 45 deletions lib/feature/loading/widget/loading_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'package:flutter/material.dart';
import 'package:home_widget/home_widget.dart';
import 'package:octopus/octopus.dart';
import 'package:uneconly/common/model/dependencies.dart';
import 'package:uneconly/common/routing/routes.dart';
import 'package:uneconly/feature/settings/data/settings_repository.dart';

/// {@template loading_page}
/// LoadingPage widget
Expand Down Expand Up @@ -46,53 +43,20 @@ class _LoadingPageState extends State<LoadingPage> {
}

Future<void> onOpen() async {
ISettingsRepository settingsRepository =
Dependencies.of(context).settingsRepository;

final group = await settingsRepository.getGroup();

if (!mounted) {
return;
}

if (group == null) {
Octopus.of(context).setState((state) {
return state
..removeByName(Routes.loading.name)
..add(
Routes.select.node(),
);
});
} else {
HomeWidget.saveWidgetData<int>(
'groupId',
group.id,
).then((value) {
HomeWidget.updateWidget(
name: 'UWidget',
iOSName: 'UWidget',
Octopus.of(context).setState((state) {
return state
..removeWhere((state) => true)
..add(
Routes.home.node(),
);
});

Octopus.of(context).setState((state) {
return state
..removeByName(Routes.loading.name)
..add(
Routes.schedule.node(
arguments: <String, String>{
'groupId': group.id.toString(),
'groupName': group.name,
},
),
);
});
}
});
}

@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
return Scaffold(
appBar: AppBar(),
body: const Center(
child: CircularProgressIndicator(),
),
);
Expand Down
110 changes: 110 additions & 0 deletions lib/feature/schedule/widget/home_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import 'package:flutter/material.dart';
import 'package:home_widget/home_widget.dart';
import 'package:octopus/octopus.dart';
import 'package:uneconly/common/model/dependencies.dart';
import 'package:uneconly/common/model/short_group_info.dart';
import 'package:uneconly/common/routing/routes.dart';
import 'package:uneconly/feature/schedule/widget/schedule_page.dart';
import 'package:uneconly/feature/select/model/group.dart';
import 'package:uneconly/feature/settings/data/settings_repository.dart';

/// {@template home_page}
/// HomePage widget
/// {@endtemplate}
class HomePage extends StatefulWidget {
/// {@macro home_page}
const HomePage({super.key});

@override
State<HomePage> createState() => _HomePageState();
} // HomePage

/// State for widget HomePage
class _HomePageState extends State<HomePage> {
Group? myGroup;

/* #region Lifecycle */
@override
void initState() {
super.initState();
// Initial state initialization
onOpen();
}

@override
void didUpdateWidget(HomePage oldWidget) {
super.didUpdateWidget(oldWidget);
// Widget configuration changed
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
// The configuration of InheritedWidgets has changed
// Also called after initState but before build
}

@override
void dispose() {
// Permanent removal of a tree stent
super.dispose();
}
/* #endregion */

Future<void> onOpen() async {
ISettingsRepository settingsRepository =
Dependencies.of(context).settingsRepository;

final group = await settingsRepository.getGroup();

if (!mounted) {
return;
}

if (group == null) {
Octopus.of(context).setState((state) {
return state
..removeWhere((node) => true)
..add(
Routes.select.node(),
);
});
} else {
HomeWidget.saveWidgetData<int>(
'groupId',
group.id,
).then((value) {
HomeWidget.updateWidget(
name: 'UWidget',
iOSName: 'UWidget',
);
});

setState(() {
myGroup = group;
});
}
}

@override
Widget build(BuildContext context) {
final currentMyGroup = myGroup;

if (currentMyGroup == null) {
return Scaffold(
appBar: AppBar(),
body: const Center(
child: CircularProgressIndicator(),
),
);
}

return SchedulePage(
shortGroupInfo: ShortGroupInfo(
groupId: currentMyGroup.id,
groupName: currentMyGroup.name,
),
isViewMode: false,
);
}
} // _HomePageState
42 changes: 35 additions & 7 deletions lib/feature/schedule/widget/schedule_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class _SchedulePageState extends State<SchedulePage>
initializeDateFormatting('en');
// Initial state initialization

print(Octopus.of(context).state.uri.toString());

scheduleBLoC = _initBloc(context);

WidgetsBinding.instance.addObserver(this);
Expand Down Expand Up @@ -213,17 +215,15 @@ class _SchedulePageState extends State<SchedulePage>
);
}

Future<void> waitReturnToSchedule(
Future<void> waitReturnToHomeSchedule(
Octopus octopus,
) async {
await waitRouteChange(
octopus,
shouldStopListen: () {
final lastNode = octopus.observer.value.children.last;

return lastNode.name == Routes.schedule.name &&
lastNode.arguments['groupId'] ==
widget.shortGroupInfo.groupId.toString();
return lastNode.name == Routes.home.name;
},
);
}
Expand Down Expand Up @@ -299,7 +299,7 @@ class _SchedulePageState extends State<SchedulePage>
},
);

await waitReturnToSchedule(
await waitReturnToHomeSchedule(
octopus,
);

Expand Down Expand Up @@ -355,7 +355,7 @@ class _SchedulePageState extends State<SchedulePage>
},
);

await waitReturnToSchedule(octopus);
await waitReturnToHomeSchedule(octopus);

final value = await dependenciesScope.settingsRepository
.getFavoriteGroups();
Expand Down Expand Up @@ -389,7 +389,7 @@ class _SchedulePageState extends State<SchedulePage>
},
);

await waitReturnToSchedule(octopus);
await waitReturnToHomeSchedule(octopus);

final value = await dependenciesScope.settingsRepository
.getFavoriteGroups();
Expand Down Expand Up @@ -491,6 +491,34 @@ class _SchedulePageState extends State<SchedulePage>
(content) async {
await Share.share(content);

// final groupInfo = state.shortGroupInfo;

// if (groupInfo == null) {
// return;
// }

// final groupName = groupInfo.groupName;

// if (groupName == null) {
// return;
// }

// final navigationState = OctopusState.fromNodes([
// Routes.home.node(),
// Routes.schedule.node(
// arguments: <String, String>{
// 'groupId': groupInfo.groupId.toString(),
// 'groupName': groupName,
// },
// ),
// ]);

// print(navigationState.location);

// await Share.shareUri(Uri.parse(
// 'https://roadmapik.com${navigationState.location}',
// ));

// await showDialog(
// context: context,
// builder: (context) {
Expand Down
7 changes: 1 addition & 6 deletions lib/feature/select/widget/select_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,7 @@ class _SelectPageState extends State<SelectPage> {
return state
..removeWhere((node) => true)
..add(
Routes.schedule.node(
arguments: {
'groupId': group.id.toString(),
'groupName': group.name,
},
),
Routes.loading.node(),
);
},
);
Expand Down
9 changes: 6 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:uneconly/common/app_scroll_configuration.dart';
import 'package:uneconly/common/logging/logging_repository.dart';
import 'package:uneconly/common/model/dependencies.dart';
import 'package:uneconly/common/routing/routes.dart';
import 'package:uneconly/common/routing/schedule_guard.dart';
import 'package:uneconly/common/util/error_util.dart';
import 'package:uneconly/common/utils/colors_utils.dart';
import 'package:uneconly/common/widget/app_error.dart';
Expand Down Expand Up @@ -142,8 +143,10 @@ class _MyAppState extends State<MyApp> {
// Create router.
router = Octopus(
routes: Routes.values,
defaultRoute: Routes.loading,
guards: <IOctopusGuard>[],
defaultRoute: Routes.home,
guards: <IOctopusGuard>[
ScheduleGuard(),
],
onError: (error, stackTrace) => log(
error.toString(),
error: error,
Expand Down Expand Up @@ -184,7 +187,7 @@ class _MyAppState extends State<MyApp> {
textScaler: TextScaler.noScaling,
),
child: OctopusTools(
enable: false,
enable: true,
octopus: router,
child: child ?? const SizedBox.shrink(),
),
Expand Down

0 comments on commit 51a0e13

Please sign in to comment.