-
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
Open
HarishwaranVijayakumar
wants to merge
6
commits into
dotnet:main
Choose a base branch
from
HarishwaranVijayakumar:fix-31658
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−2
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
67d0c77
Fix for TimePicker
HarishwaranVijayakumar bc82c16
Refactor
HarishwaranVijayakumar b73caf6
Remove unwanted code
HarishwaranVijayakumar c4bfcf1
Added braces
HarishwaranVijayakumar f8251ea
Modified the changes
HarishwaranVijayakumar eddcc4d
Dispose call removed
HarishwaranVijayakumar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,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; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -87,8 +128,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 +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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||
_dialog.Show(); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -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); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.