From fd9522200d8e584a993c326278826cb41a10ecd5 Mon Sep 17 00:00:00 2001 From: helderbetiol <37706737+helderbetiol@users.noreply.github.com> Date: Tue, 17 Sep 2024 18:20:54 +0200 Subject: [PATCH] refactor(app) appbar --- APP/lib/common/appbar.dart | 314 +++++++++++++++++++++++-------------- APP/lib/common/csv.dart | 2 +- 2 files changed, 193 insertions(+), 123 deletions(-) diff --git a/APP/lib/common/appbar.dart b/APP/lib/common/appbar.dart index d84f6022..97816336 100644 --- a/APP/lib/common/appbar.dart +++ b/APP/lib/common/appbar.dart @@ -11,57 +11,20 @@ import 'package:ogree_app/widgets/login/change_password_popup.dart'; import 'package:ogree_app/widgets/tenants/popups/create_server_popup.dart'; import 'package:ogree_app/widgets/tools/download_tool_popup.dart'; -AppBar myAppBar(context, userEmail, {isTenantMode = false}) { - final localeMsg = AppLocalizations.of(context)!; - Future logout() => Navigator.of(context).push( - MaterialPageRoute( - builder: (context) => const LoginPage(), - ), - ); - - final List> entries = >[ - PopupMenuItem( - value: "change", - child: Text(AppLocalizations.of(context)!.changePassword), - ), - const PopupMenuItem( - value: "logout", - child: Text("Logout"), - ), - ]; - if (isTenantMode) { - entries.insert( - 0, - PopupMenuItem( - value: "new", - child: Text(backendType == BackendType.kubernetes - ? localeMsg.addKube - : localeMsg.addServer,), - ),); - } else { - entries.insert( - 0, - PopupMenuItem( - value: Tools.unity.name, - child: Text(localeMsg.downloadUnity), - ),); - entries.insert( - 0, - PopupMenuItem( - value: Tools.cli.name, - child: Text(localeMsg.downloadCli), - ),); - if (isTenantAdmin) { - entries.insert( - 0, - PopupMenuItem( - value: "tenant", - child: Text(localeMsg.tenantParameters), - ),); - } - } +enum PopupMenuEntries { + passwordChange, + logout, + createNewServer, + tenantParams, + downloadUnity, + downloadCli +} +AppBar myAppBar(BuildContext context, String userEmail, + {bool isTenantMode = false}) { + final localeMsg = AppLocalizations.of(context)!; final bool isSmallDisplay = MediaQuery.of(context).size.width < 600; + return AppBar( backgroundColor: Colors.grey.shade900, leadingWidth: 160, @@ -73,15 +36,17 @@ AppBar myAppBar(context, userEmail, {isTenantMode = false}) { child: const Text( 'OGrEE', style: TextStyle( - fontSize: 21, - fontWeight: FontWeight.w700, - color: Colors.white,), + fontSize: 21, + fontWeight: FontWeight.w700, + color: Colors.white, + ), ), onPressed: () => Navigator.of(context).push( MaterialPageRoute( builder: (context) => ProjectsPage( - userEmail: isTenantMode ? "admin" : userEmail, - isTenantMode: isTenantMode,), + userEmail: isTenantMode ? "admin" : userEmail, + isTenantMode: isTenantMode, + ), ), ), ), @@ -93,80 +58,185 @@ AppBar myAppBar(context, userEmail, {isTenantMode = false}) { ), ), actions: [ - if (isSmallDisplay) Container() else Padding( - padding: const EdgeInsets.only(right: 20), - child: Row( - children: [ - if (backendType == BackendType.kubernetes) Padding( - padding: const EdgeInsets.only(right: 8), - child: Container( - decoration: BoxDecoration( - borderRadius: - const BorderRadius.all(Radius.circular(8)), - border: Border.all(color: Colors.white), - ), - child: Badge( - backgroundColor: Colors.grey.shade900, - label: const Text("KUBE"), - ), - ), - ) else Container(), - Text(isTenantMode ? apiUrl : tenantName, - style: const TextStyle(color: Colors.white),), - ], - ), - ), + getInfoBadge(isTenantMode, isSmallDisplay), const Padding( padding: EdgeInsets.symmetric(vertical: 15), child: LanguageToggle(), ), const SizedBox(width: 17), - PopupMenuButton( - onSelected: (value) { - if (value == "logout") { - logout(); - } else if (value == "new") { - showCustomPopup( - context, CreateServerPopup(parentCallback: () {}),); - } else if (value == "tenant") { - Navigator.of(context).push(MaterialPageRoute( - builder: (context) => const TenantPage(userEmail: "admin"), - ),); - } else if (value == Tools.unity.name) { - showCustomPopup(context, const DownloadToolPopup(tool: Tools.unity), - isDismissible: true,); - } else if (value == Tools.cli.name) { - showCustomPopup(context, const DownloadToolPopup(tool: Tools.cli), - isDismissible: true,); - } else { - showCustomPopup(context, const ChangePasswordPopup()); - } - }, - itemBuilder: (_) => entries, - child: Row( - children: [ - const Icon( - Icons.account_circle, - color: Colors.white, - ), - const SizedBox(width: 10), - if (isSmallDisplay) Tooltip( - message: isTenantMode - ? (backendType == BackendType.kubernetes - ? "(KUBE) $apiUrl" - : apiUrl) - : tenantName, - triggerMode: TooltipTriggerMode.tap, - child: const Icon( - Icons.info_outline_rounded, - color: Colors.white, - ),) else Text( - isTenantMode ? "admin" : userEmail, - style: const TextStyle(color: Colors.white), - ), - ], - ),), + getPopupMenuButton( + isTenantMode, isSmallDisplay, userEmail, localeMsg, context), const SizedBox(width: 40), ], ); } + +// KUBE + API URL + TenantName +Widget getInfoBadge(bool isTenantMode, bool isSmallDisplay) { + if (isSmallDisplay) { + return Container(); + } else { + return Padding( + padding: const EdgeInsets.only(right: 20), + child: Row( + children: [ + if (backendType == BackendType.kubernetes) + Padding( + padding: const EdgeInsets.only(right: 8), + child: Container( + decoration: BoxDecoration( + borderRadius: const BorderRadius.all(Radius.circular(8)), + border: Border.all(color: Colors.white), + ), + child: Badge( + backgroundColor: Colors.grey.shade900, + label: const Text("KUBE"), + ), + ), + ) + else + Container(), + Text( + isTenantMode ? apiUrl : tenantName, + style: const TextStyle(color: Colors.white), + ), + ], + ), + ); + } +} + +// POPUP MENU +PopupMenuButton getPopupMenuButton( + bool isTenantMode, + bool isSmallDisplay, + String userEmail, + AppLocalizations localeMsg, + BuildContext context) { + return PopupMenuButton( + onSelected: (value) => onMenuEntrySelected(value, context), + itemBuilder: (_) => getPopupMenuEntries(isTenantMode, localeMsg), + child: Row( + children: [ + const Icon( + Icons.account_circle, + color: Colors.white, + ), + const SizedBox(width: 10), + if (isSmallDisplay) + Tooltip( + message: isTenantMode + ? (backendType == BackendType.kubernetes + ? "(KUBE) $apiUrl" + : apiUrl) + : tenantName, + triggerMode: TooltipTriggerMode.tap, + child: const Icon( + Icons.info_outline_rounded, + color: Colors.white, + ), + ) + else + Text( + isTenantMode ? "admin" : userEmail, + style: const TextStyle(color: Colors.white), + ), + ], + ), + ); +} + +List> getPopupMenuEntries( + bool isTenantMode, AppLocalizations localeMsg) { + final List> entries = + >[ + PopupMenuItem( + value: PopupMenuEntries.passwordChange, + child: Text(localeMsg.changePassword), + ), + const PopupMenuItem( + value: PopupMenuEntries.logout, + child: Text("Logout"), + ), + ]; + if (isTenantMode) { + entries.insert( + 0, + PopupMenuItem( + value: PopupMenuEntries.createNewServer, + child: Text( + backendType == BackendType.kubernetes + ? localeMsg.addKube + : localeMsg.addServer, + ), + ), + ); + } else { + entries.insert( + 0, + PopupMenuItem( + value: PopupMenuEntries.downloadUnity, + child: Text(localeMsg.downloadUnity), + ), + ); + entries.insert( + 0, + PopupMenuItem( + value: PopupMenuEntries.downloadCli, + child: Text(localeMsg.downloadCli), + ), + ); + if (isTenantAdmin) { + entries.insert( + 0, + PopupMenuItem( + value: PopupMenuEntries.tenantParams, + child: Text(localeMsg.tenantParameters), + ), + ); + } + } + return entries; +} + +onMenuEntrySelected(PopupMenuEntries selectedEntry, BuildContext context) { + switch (selectedEntry) { + case PopupMenuEntries.logout: + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) => const LoginPage(), + ), + ); + break; + case PopupMenuEntries.tenantParams: + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) => const TenantPage(userEmail: "admin"), + ), + ); + break; + case PopupMenuEntries.downloadUnity: + showCustomPopup( + context, + const DownloadToolPopup(tool: Tools.unity), + isDismissible: true, + ); + break; + case PopupMenuEntries.downloadCli: + showCustomPopup( + context, + const DownloadToolPopup(tool: Tools.cli), + isDismissible: true, + ); + break; + case PopupMenuEntries.passwordChange: + showCustomPopup(context, const ChangePasswordPopup()); + break; + case PopupMenuEntries.createNewServer: + showCustomPopup( + context, + CreateServerPopup(parentCallback: () {}), + ); + break; + } +} diff --git a/APP/lib/common/csv.dart b/APP/lib/common/csv.dart index edfed41e..949d4f9f 100644 --- a/APP/lib/common/csv.dart +++ b/APP/lib/common/csv.dart @@ -18,7 +18,7 @@ saveCSV(String desiredFileName, List> rows, html.AnchorElement( href: 'data:application/octet-stream;base64,${base64Encode(bytes)}', ) - ..setAttribute("download", "report.csv") + ..setAttribute("download", "$desiredFileName.csv") ..click(); } else { // Save to local filesystem