using System; using Android.App; using Android.Views; using Google.Android.Material.BottomNavigation; using Microsoft.Maui; using Microsoft.Maui.Platform; namespace Xamarin.Forms.Clinical6.Views { public partial class DashboardTabPage { partial void RegisterPlatformBadgeHandler() { DashboardBadgeEvent -= Android_UpdateBadge; DashboardBadgeEvent += Android_UpdateBadge; } private void Android_UpdateBadge(object sender, int count) { try { var activity = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity; if (activity == null) return; activity.RunOnUiThread(() => { try { var decor = activity.Window?.DecorView; if (decor == null) return; var root = decor as ViewGroup; var bottomNav = FindBottomNavigationView(root); if (bottomNav == null) { int bottomNavId = activity.Resources.GetIdentifier("maui_bottom_navigation_view", "id", activity.PackageName); if (bottomNavId == 0) bottomNavId = activity.Resources.GetIdentifier("bottom_navigation_view", "id", activity.PackageName); if (bottomNavId != 0) bottomNav = activity.FindViewById(bottomNavId); } if (bottomNav == null) return; if (_alertTabIndex < 0 || _alertTabIndex >= bottomNav.Menu.Size()) return; var menuItem = bottomNav.Menu.GetItem(_alertTabIndex); if (menuItem == null) return; var itemId = menuItem.ItemId; if (count <= 0) { bottomNav.RemoveBadge(itemId); return; } // Cap at 99 for display int displayCount = Math.Min(count, 99); // Create/update badge var badge = bottomNav.GetOrCreateBadge(itemId); badge.Number = displayCount; // ---- POSITIONING LOGIC ---- try { var density = activity.Resources.DisplayMetrics.Density; badge.HorizontalOffset = (int)(25 * density); // adjust right/left badge.VerticalOffset = (int)(10 * density); // adjust up/down } catch { } // -------------------------------- // ---- COLOR ---- badge.BackgroundColor = global::Android.Graphics.Color.ParseColor("#B00020"); // ---- TEXT COLOR ---- try { var setTextColorMethod = badge.GetType().GetMethod("SetBadgeTextColor"); if (setTextColorMethod != null) { setTextColorMethod.Invoke(badge, new object[] { global::Android.Graphics.Color.White }); } else { var prop = badge.GetType().GetProperty("BadgeTextColor"); if (prop != null && prop.CanWrite) prop.SetValue(badge, global::Android.Graphics.Color.White); } } catch { } } catch (Exception exInner) { System.Diagnostics.Debug.WriteLine($"Android_UpdateBadge UI exception: {exInner}"); } }); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"Android_UpdateBadge exception: {ex}"); } } private BottomNavigationView FindBottomNavigationView(ViewGroup root) { if (root == null) return null; for (int i = 0; i < root.ChildCount; i++) { var child = root.GetChildAt(i); if (child is BottomNavigationView bnv) return bnv; if (child is ViewGroup vg) { var found = FindBottomNavigationView(vg); if (found != null) return found; } } return null; } } }