-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android] Fix for TimePicker Dialog doesn't update the layout when rotating the device with dialog open #31910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
67d0c77
bc82c16
b73caf6
c4bfcf1
f8251ea
eddcc4d
9c7fb70
904c232
8048897
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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() | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
|
@@ -22,22 +26,60 @@ 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.Dispose(); | ||||||||||||||||||||||||||||
_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; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -87,8 +129,15 @@ public static void MapTextColor(ITimePickerHandler handler, ITimePicker timePick | |||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
void ShowPickerDialog() | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
if (VirtualView == null) | ||||||||||||||||||||||||||||
if (VirtualView is null) | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if (_dialog is not null && _dialog.IsShowing) | ||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
var time = VirtualView.Time; | ||||||||||||||||||||||||||||
ShowPickerDialog(time.Hours, time.Minutes); | ||||||||||||||||||||||||||||
|
@@ -99,7 +148,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); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
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.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could include a null-check for _dialog
before attempting to use it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsuarezruiz, I have made the suggested change.
There was a problem hiding this comment.
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 usesVirtualView is null
. For consistency, maintain the same null checking pattern throughout the file.Copilot uses AI. Check for mistakes.