Skip to content

Commit

Permalink
chore: resolve PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunal22shah committed Dec 2, 2024
1 parent cd4c72e commit 3eabc86
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using MUXControlsTestApp.Utilities;
using Private.Infrastructure;
using SamplesApp.UITests;
using Uno.UI.Extensions;
using Uno.UI.RuntimeTests.Helpers;
using Uno.UI.RuntimeTests.MUX.Helpers;

Expand Down Expand Up @@ -308,31 +309,68 @@ public async Task When_Time_Uninitialized_Should_Display_Current_Time()

#if __ANDROID__
var nativeFlyout = (NativeTimePickerFlyout)associatedFlyout;
var displayedTime = nativeFlyout._initialTime;

Assert.AreEqual(expectedCurrentTime.Hours, displayedTime.Hours, "Hours should match the current time.");
Assert.AreEqual(expectedCurrentTime.Minutes, displayedTime.Minutes,
"Minutes should match the current time.");
var dialogField = typeof(NativeTimePickerFlyout).GetField("_dialog", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var dialog = dialogField?.GetValue(nativeFlyout) as UnoTimePickerDialog;
var decorView = dialog.Window?.DecorView;

var timePickerView = FindTimePicker(decorView);

var displayedHour = timePickerView.GetHourCompat();
var displayedMinute = timePickerView.GetMinuteCompat();

Assert.AreEqual(expectedCurrentTime.Hours, displayedHour, "Hours should match the current time.");
Assert.AreEqual(expectedCurrentTime.Minutes, displayedMinute, "Minutes should match the current time.");
#elif __IOS__
var nativeFlyout = (NativeTimePickerFlyout)associatedFlyout;
var displayedTime = nativeFlyout._timeSelector?.Time ?? TimeSpan.Zero;
var nativeFlyout = (NativeTimePickerFlyout)associatedFlyout;

Assert.AreEqual(expectedCurrentTime.Hours, displayedTime.Hours, "Hours should match the current time.");
Assert.AreEqual(expectedCurrentTime.Minutes, displayedTime.Minutes, "Minutes should match the current time.");
var selectorField = typeof(NativeTimePickerFlyout).GetField("_timeSelector", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var timeSelector = selectorField?.GetValue(nativeFlyout) as TimePickerSelector;

var displayedTime = timeSelector.Time;

Assert.AreEqual(expectedCurrentTime.Hours, displayedTime.Hours, "Hours should match the current time.");
Assert.AreEqual(expectedCurrentTime.Minutes, displayedTime.Minutes, "Minutes should match the current time.");
#endif
}
#endif
private TimeSpan GetCurrentTime()
{
var calendar = new global::Windows.Globalization.Calendar();
calendar.SetToNow();
var now = calendar.GetDateTime();
return new TimeSpan(now.Hour, now.Minute, now.Second);
}
}
#endif
#if __ANDROID__
private Android.Widget.TimePicker FindTimePicker(Android.Views.View root)
{
if (root is Android.Widget.TimePicker picker)
{
return picker;
}

class MyContext
{
public object StartTime => null;
if (root is not Android.Views.ViewGroup viewGroup)
{
return null;
}

for (var i = 0; i < viewGroup.ChildCount; i++)
{
var child = viewGroup.GetChildAt(i);
var result = FindTimePicker(child);
if (result != null)
{
return result;
}
}

return null;
}
#endif

class MyContext
{
public object StartTime => null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,25 @@ partial class NativeTimePickerFlyout
{
private bool _programmaticallyDismissed;
private UnoTimePickerDialog _dialog;
public TimeSpan _initialTime;
private TimeSpan _initialTime;

internal bool IsNativeDialogOpen => _dialog?.IsShowing ?? false;

internal protected override void Open()
{
if (Time.Ticks == -1)
{
Time = GetCurrentTime();
}

SaveInitialTime();

ShowTimePicker();

AddToOpenFlyouts();
}

private void SaveInitialTime()
{

if (Time.Ticks == -1)
{
var calendar = new global::Windows.Globalization.Calendar();
calendar.SetToNow();
var currentDateTime = calendar.GetDateTime();
_initialTime = new TimeSpan(currentDateTime.Hour, currentDateTime.Minute, currentDateTime.Second);
}
else
{
_initialTime = Time;
}

}
private void SaveInitialTime() => _initialTime = Time;


private void SaveTime(TimeSpan time)
Expand Down
11 changes: 10 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/TimePicker/NativeTimePickerFlyout.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
using Microsoft.UI.Xaml.Controls.Primitives;
using System;
using Windows.Globalization;
using Microsoft.UI.Xaml.Controls.Primitives;

namespace Microsoft.UI.Xaml.Controls;

internal partial class NativeTimePickerFlyout : TimePickerFlyout
{
protected TimeSpan GetCurrentTime()
{
var calendar = new Calendar();
calendar.SetToNow();
var currentDateTime = calendar.GetDateTime();
return new TimeSpan(currentDateTime.Hour, currentDateTime.Minute, currentDateTime.Second);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ private void InitializeContent()
return;
}

if (Time.Ticks == -1)
{
var calendar = new global::Windows.Globalization.Calendar();
calendar.SetToNow();
var currentDateTime = calendar.GetDateTime();
Time = new TimeSpan(currentDateTime.Hour, currentDateTime.Minute, currentDateTime.Second);
}

_isInitialized = true;

_timeSelector = new TimePickerSelector()
Expand Down Expand Up @@ -152,8 +144,12 @@ void onUnload(object sender, RoutedEventArgs e)

protected internal override void Open()
{
InitializeContent();
if (Time.Ticks == -1)
{
Time = GetCurrentTime();
}

InitializeContent();

_timeSelector?.Initialize();

Expand Down

0 comments on commit 3eabc86

Please sign in to comment.