Skip to content
Merged
51 changes: 51 additions & 0 deletions src/Core/src/Handlers/TimePicker/TimePickerHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
using Android.Graphics.Drawables;
using Android.Text.Format;
using DateFormat = Android.Text.Format.DateFormat;
using Android.Views;
using Microsoft.Maui.Devices;

namespace Microsoft.Maui.Handlers
{
public partial class TimePickerHandler : ViewHandler<ITimePicker, MauiTimePicker>
{
MauiTimePicker? _timePicker;
TimePickerDialog? _dialog;
int _currentHour;
int _currentMinute;

protected override MauiTimePicker CreatePlatformView()
{
Expand All @@ -26,6 +30,24 @@ protected override void ConnectHandler(MauiTimePicker platformView)

platformView.ShowPicker = ShowPickerDialog;
platformView.HidePicker = HidePickerDialog;
platformView.ViewAttachedToWindow += OnViewAttachedToWindow;
platformView.ViewDetachedFromWindow += OnViewDetachedFromWindow;

if (platformView.IsAttachedToWindow)
{
OnViewAttachedToWindow();
}
}

void OnViewDetachedFromWindow(object? sender = null, View.ViewDetachedFromWindowEventArgs? e = null)
{
// Called when an activity is destroyed or view is detached
DeviceDisplay.MainDisplayInfoChanged -= OnMainDisplayInfoChanged;
}

void OnViewAttachedToWindow(object? sender = null, View.ViewAttachedToWindowEventArgs? e = null)
{
DeviceDisplay.MainDisplayInfoChanged += OnMainDisplayInfoChanged;
}

protected override void DisconnectHandler(MauiTimePicker platformView)
Expand All @@ -37,17 +59,31 @@ protected override void DisconnectHandler(MauiTimePicker platformView)
_dialog = null;
}

platformView.ViewAttachedToWindow -= OnViewAttachedToWindow;
platformView.ViewDetachedFromWindow -= OnViewDetachedFromWindow;
OnViewDetachedFromWindow();

platformView.ShowPicker = null;
platformView.HidePicker = null;

base.DisconnectHandler(platformView);
}

protected virtual TimePickerDialog CreateTimePickerDialog(int hour, int minute)
{
// Store the current values for orientation change handling
_currentHour = hour;
_currentMinute = minute;

void onTimeSetCallback(object? obj, TimePickerDialog.TimeSetEventArgs args)
{
if (VirtualView == null || PlatformView == null)
return;

// Update stored values when user selects time
_currentHour = args.HourOfDay;
_currentMinute = args.Minute;

VirtualView.Time = new TimeSpan(args.HourOfDay, args.Minute, 0);
VirtualView.IsFocused = false;

Expand Down Expand Up @@ -174,5 +210,20 @@ bool Use24HourView
return IsCustom24HourFormat(VirtualView.Format);
}
}

void OnMainDisplayInfoChanged(object? sender, DisplayInfoChangedEventArgs e)
{
// Only handle orientation changes when dialog is actually showing
if (_dialog is not null && _dialog.IsShowing)
{
// Unsubscribe first to prevent the DismissEvent from hiding the new dialog
_dialog.DismissEvent -= OnDialogDismiss;
_dialog.Dismiss();
_dialog = null;

// Recreate dialog with current values to handle orientation change
ShowPickerDialog(new TimeSpan(_currentHour, _currentMinute, 0));
}
}
}
}
Loading