1
0

routes.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import 'dart:developer';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:go_router/go_router.dart';
  5. import 'package:hive_flutter/hive_flutter.dart';
  6. import 'package:jiffy/jiffy.dart';
  7. import 'package:tp5/authpages/auth_magiclink.dart';
  8. import 'package:tp5/authpages/login_otp.dart';
  9. import 'package:tp5/core/basic_page.dart';
  10. import 'package:tp5/core/utils.dart';
  11. import 'package:tp5/flightslist/flightslist_page.dart';
  12. import 'package:tp5/fltinfo/view/dutyinfo_page.dart';
  13. import 'package:tp5/fltinfo/view/fltinfo_page.dart';
  14. import 'package:tp5/ftl/view/ftl_page.dart';
  15. import 'package:tp5/lido/view/lido_form.dart';
  16. import 'package:tp5/lido/view/lidolist_page.dart';
  17. import 'package:tp5/lido/view/lidoofp_page.dart';
  18. import 'package:tp5/onboarding/onboarding.dart';
  19. import 'package:tp5/pdf/pdf_page.dart';
  20. import 'package:tp5/roster/view/crewlink_form.dart';
  21. import 'package:tp5/roster/view/crewlist_page.dart';
  22. import 'package:tp5/roster/view/roster_page.dart';
  23. import 'package:tp5/rosters/rosters_page.dart';
  24. import 'package:supabase_flutter/supabase_flutter.dart';
  25. final routeProvider = Provider<RouterConfig<Object>?>((ref) {
  26. return _routes;
  27. });
  28. final _routes = GoRouter(
  29. debugLogDiagnostics: false,
  30. redirect: (context, state) {
  31. log("${state.fullPath} | ${state.extra.runtimeType.toString()}",
  32. name: "routes");
  33. // Check app expiration
  34. if (DateTime.now().isAfter(DateTime(2024, 12, 31))) {
  35. return "/expired";
  36. }
  37. // Get current auth state
  38. final session = Supabase.instance.client.auth.currentSession;
  39. final isLoggedIn = session != null;
  40. // List of paths that don't require authentication
  41. final publicPaths = ['/auth/login-otp', '/magic-link', '/auth/signup'];
  42. final isPublicPath = publicPaths.contains(state.fullPath);
  43. // Redirect to login if not authenticated and trying to access protected route
  44. if (!isLoggedIn && !isPublicPath) {
  45. return '/auth/login-otp';
  46. }
  47. // Allow access to public paths even when logged out
  48. if (isPublicPath) {
  49. return null;
  50. }
  51. return null;
  52. },
  53. // initialLocation: '/test',
  54. // initialLocation: '/auth/magiclink',
  55. initialLocation: '/home',
  56. routes: [
  57. GoRoute(
  58. path: '/magic-link',
  59. builder: (BuildContext context, GoRouterState state) {
  60. // Access query parameters here
  61. final token = state.uri.queryParameters['token'];
  62. return AuthMagiclink(token: token);
  63. }),
  64. GoRoute(
  65. path: '/test',
  66. builder: (context, state) => BasicPage(
  67. appBar: AppBar(
  68. automaticallyImplyLeading: false,
  69. title: const Text("TAR Pilot v5"),
  70. ),
  71. drawer: ListView(),
  72. body: Column(
  73. children: [
  74. Text("${Jiffy.parseFromList([
  75. 2024,
  76. 9,
  77. 1,
  78. 20,
  79. 0
  80. ], isUtc: true).setTz(ap: "TUN").toUtc().dateTime}"),
  81. Text(DTInterval.fromHm(
  82. apartir:
  83. Jiffy.parseFromList([2024, 9, 1, 1, 1], isUtc: true)
  84. .setTz(ap: "TUN"),
  85. h: 2,
  86. m: 0,
  87. duration: const Duration(hours: 2, minutes: 59),
  88. ap: "TUN")
  89. .start
  90. .dateTime
  91. .toIso8601String())
  92. ],
  93. ),
  94. ),
  95. ),
  96. GoRoute(
  97. path: "/auth",
  98. redirect: (_, state) {
  99. // if
  100. // (state.fullPath != "/crewlink/settings" &&
  101. // (((Hive.box("profile").get("crewlink_user") ?? "") == "") ||
  102. // ((Hive.box("profile").get("crewlink_pass") ?? "") ==
  103. // ""))) {
  104. // return "/crewlink/settings";
  105. // }
  106. return null;
  107. },
  108. routes: [
  109. GoRoute(
  110. path: '/signup',
  111. builder: (BuildContext context, GoRouterState state) {
  112. // Access query parameters here
  113. return Text("signup");
  114. }),
  115. GoRoute(
  116. path: '/login-otp',
  117. builder: (BuildContext context, GoRouterState state) {
  118. // Access query parameters here
  119. final token = state.uri.queryParameters['token'];
  120. return LoginOtp();
  121. }),
  122. ]),
  123. GoRoute(
  124. path: '/expired',
  125. builder: (context, state) => BasicPage(
  126. appBar: AppBar(
  127. automaticallyImplyLeading: false,
  128. title: const Text("TAR Pilot v5"),
  129. ),
  130. drawer: ListView(),
  131. body: const Text("App is Expired please Update")),
  132. ),
  133. GoRoute(
  134. path: '/home',
  135. redirect: (context, state) => '/crewlink/roster',
  136. ),
  137. GoRoute(
  138. path: '/pdf',
  139. builder: (context, state) {
  140. final params = state.extra as PdfPageParams;
  141. return PdfPage(params: params);
  142. },
  143. ),
  144. GoRoute(
  145. path: '/rosters',
  146. builder: (context, state) {
  147. return const RostersPage();
  148. },
  149. ),
  150. GoRoute(
  151. path: '/flightslist',
  152. builder: (context, state) {
  153. return const FlightslistPage();
  154. },
  155. ),
  156. GoRoute(
  157. path: '/ftl',
  158. builder: (context, state) {
  159. final params = (state.extra as FtlPageParams?) ?? const FtlPageParams();
  160. return FtlPage(params: params);
  161. },
  162. ),
  163. GoRoute(
  164. path: '/fltinfo',
  165. builder: (context, state) {
  166. final params = (state.extra as FltinfoParams?) ?? const FltinfoParams();
  167. return FltinfoPage(params: params);
  168. },
  169. ),
  170. GoRoute(
  171. path: '/dutyinfo',
  172. builder: (context, state) {
  173. final params =
  174. (state.extra as DutyinfoParams?) ?? const DutyinfoParams();
  175. return DutyinfoPage(params: params);
  176. },
  177. ),
  178. GoRoute(
  179. path: "/lido",
  180. redirect: (_, state) {
  181. // log(state.fullPath.toString());
  182. if
  183. // (["/crewlink/roster"].contains(state.uri.toString())&&
  184. (state.fullPath != "/lido/settings" &&
  185. (((Hive.box("profile").get("lido_user") ?? "") == "") ||
  186. ((Hive.box("profile").get("lido_pass") ?? "") == ""))) {
  187. return "/lido/settings";
  188. }
  189. return null;
  190. },
  191. routes: [
  192. GoRoute(
  193. name: "lido.list",
  194. path: 'list',
  195. builder: (context, state) {
  196. final params = (state.extra as LidolistPageParams?) ??
  197. const LidolistPageParams();
  198. return LidolistPage(params: params);
  199. }),
  200. GoRoute(
  201. name: "lido.ofp",
  202. path: 'ofp',
  203. builder: (context, state) {
  204. final params = (state.extra as LidoofpPageParams?) ??
  205. const LidoofpPageParams();
  206. return LidoofpPage(params: params);
  207. }),
  208. GoRoute(
  209. path: 'settings', builder: (context, state) => const LidoForm()),
  210. ]),
  211. GoRoute(
  212. path: "/crewlink",
  213. redirect: (_, state) {
  214. // log(state.fullPath.toString());
  215. if
  216. // (["/crewlink/roster"].contains(state.uri.toString())&&
  217. (state.fullPath != "/crewlink/settings" &&
  218. (((Hive.box("profile").get("crewlink_user") ?? "") == "") ||
  219. ((Hive.box("profile").get("crewlink_pass") ?? "") ==
  220. ""))) {
  221. return "/crewlink/settings";
  222. }
  223. return null;
  224. },
  225. routes: [
  226. GoRoute(
  227. name: "crewlink.roster",
  228. path: 'roster',
  229. builder: (context, state) {
  230. final params = (state.extra as RosterPageParams?) ??
  231. const RosterPageParams();
  232. return RosterPage(
  233. params: RosterPageParams(
  234. datestart: params.datestart ??
  235. Jiffy.now()
  236. .toUtc()
  237. .startOf(Unit.month)
  238. // .subtract(days: 30)
  239. .format(pattern: "ddMMMyy"),
  240. dateend: params.dateend ??
  241. Jiffy.now()
  242. .toUtc()
  243. .endOf(Unit.month)
  244. .format(pattern: "ddMMMyy"),
  245. crewlinkuser: params.crewlinkuser ??
  246. Hive.box("profile").get("crewlink_user") ??
  247. "",
  248. crewlinkpass: params.crewlinkpass ??
  249. Hive.box("profile").get("crewlink_pass") ??
  250. ""),
  251. );
  252. }),
  253. GoRoute(
  254. name: "crewlink.crewlist",
  255. path: 'crewlist',
  256. builder: (context, state) {
  257. final params = (state.extra as CrewlistPageParams?) ??
  258. const CrewlistPageParams();
  259. print(
  260. "${params.datestart} ${params.dateend} ${params.al} ${params.fnum} ${params.dep} ${params.des}");
  261. return CrewlistPage(
  262. params: CrewlistPageParams(
  263. datestart: params.datestart ?? //"04Dec23",
  264. Jiffy.now()
  265. .toUtc()
  266. //.subtract(days: 1)
  267. .format(pattern: "ddMMMyy"),
  268. dateend: params.dateend ?? //"05Dec23",
  269. params.datestart ??
  270. Jiffy.now().toUtc().format(pattern: "ddMMMyy"),
  271. al: params.al ?? "",
  272. fnum: params.fnum ?? "",
  273. dep: params.dep ?? "",
  274. des: params.des ?? "",
  275. crewlinkuser: params.crewlinkuser ??
  276. Hive.box("profile").get("crewlink_user") ??
  277. "",
  278. crewlinkpass: params.crewlinkpass ??
  279. Hive.box("profile").get("crewlink_pass") ??
  280. ""),
  281. );
  282. }),
  283. GoRoute(
  284. path: 'settings',
  285. builder: (context, state) => const CrewlinkForm()),
  286. ])
  287. ],
  288. );