Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 71 additions & 2 deletions src/Core/src/Handlers/TimePicker/TimePickerHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
using Android.Content.Res;
using Android.Graphics.Drawables;
using Android.Text.Format;
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 @@ -22,22 +26,59 @@ protected override MauiTimePicker CreatePlatformView()
return _timePicker;
}

protected override void ConnectHandler(MauiTimePicker platformView)
{
base.ConnectHandler(platformView);
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)
{
if (_dialog != null)
{
_dialog.Hide();
_dialog = null;
}

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

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 @@ -87,8 +128,15 @@ public static void MapTextColor(ITimePickerHandler handler, ITimePicker timePick

void ShowPickerDialog()
{
if (VirtualView == null)
if (VirtualView is null)
Copy link

Copilot AI Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent null checking pattern. The existing code uses VirtualView == null on line 75, but this change uses VirtualView is null. For consistency, maintain the same null checking pattern throughout the file.

Suggested change
if (VirtualView is null)
if (VirtualView == null)

Copilot uses AI. Check for mistakes.

{
return;
}

if (_dialog is not null && _dialog.IsShowing)
{
return;
}

var time = VirtualView.Time;
ShowPickerDialog(time.Hours, time.Minutes);
Expand All @@ -99,7 +147,17 @@ void ShowPickerDialog()
// to be lost). Not useful until we have orientation changed events.
void ShowPickerDialog(int hour, int minute)
{
_dialog = CreateTimePickerDialog(hour, minute);
if (_dialog is null)
{
_dialog = CreateTimePickerDialog(hour, minute);
}
else
{
// Update the dialog with new values (for orientation changes)
_dialog.Dispose();
_dialog = CreateTimePickerDialog(hour, minute);
}

Comment on lines +150 to +160
Copy link

Copilot AI Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic in both branches creates a new dialog. This can be simplified by disposing the existing dialog first (if it exists) and then creating a new one, eliminating code duplication.

Suggested change
if (_dialog is null)
{
_dialog = CreateTimePickerDialog(hour, minute);
}
else
{
// Update the dialog with new values (for orientation changes)
_dialog.Dispose();
_dialog = CreateTimePickerDialog(hour, minute);
}
// Always dispose the existing dialog before creating a new one
_dialog?.Dispose();
_dialog = CreateTimePickerDialog(hour, minute);

Copilot uses AI. Check for mistakes.

_dialog.Show();
}

Expand All @@ -112,5 +170,16 @@ void HidePickerDialog()

bool Use24HourView => VirtualView != null && (DateFormat.Is24HourFormat(PlatformView?.Context)
&& VirtualView.Format == "t" || VirtualView.Format == "HH:mm");

void OnMainDisplayInfoChanged(object? sender, DisplayInfoChangedEventArgs e)
{
if (_dialog is not null && _dialog.IsShowing)
{
_dialog.Dismiss();

// Recreate dialog with stored current values to handle orientation change
ShowPickerDialog(_currentHour, _currentMinute);
}
}
}
}
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*REMOVED*Microsoft.Maui.Platform.MauiPicker.ShowPopupOnFocus.set -> void
*REMOVED*override Microsoft.Maui.Platform.MauiPicker.OnFocusChanged(bool gainFocus, Android.Views.FocusSearchDirection direction, Android.Graphics.Rect? previouslyFocusedRect) -> void
*REMOVED*override Microsoft.Maui.Platform.MauiPicker.OnTouchEvent(Android.Views.MotionEvent? e) -> bool
override Microsoft.Maui.Handlers.TimePickerHandler.ConnectHandler(Microsoft.Maui.Platform.MauiTimePicker! platformView) -> void
override Microsoft.Maui.Platform.MauiDatePicker.DefaultMovementMethod.get -> Android.Text.Method.IMovementMethod?
override Microsoft.Maui.Platform.MauiPickerBase.DefaultMovementMethod.get -> Android.Text.Method.IMovementMethod?
override Microsoft.Maui.Platform.MauiTimePicker.DefaultMovementMethod.get -> Android.Text.Method.IMovementMethod?
Expand Down