Skip to content
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

chore: native timepicker- set -initial time #18991

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@ -288,10 +289,86 @@ private async Task When_Native_Flyout_Theme(UIKit.UIUserInterfaceStyle expectedS
Assert.AreEqual(expectedStyle, nativeTimePicker.OverrideUserInterfaceStyle);
}
#endif
}
#if __IOS__ || __ANDROID__
[TestMethod]
public async Task When_Time_Uninitialized_Should_Display_Current_Time()
{
var timePicker = new Microsoft.UI.Xaml.Controls.TimePicker();
timePicker.Time = new TimeSpan(TimePicker.DEFAULT_TIME_TICKS);

class MyContext
{
public object StartTime => null;
var expectedCurrentTime = GetCurrentTime();

TestServices.WindowHelper.WindowContent = timePicker;
await TestServices.WindowHelper.WaitForLoaded(timePicker);

await DateTimePickerHelper.OpenDateTimePicker(timePicker);

var openFlyouts = FlyoutBase.OpenFlyouts;
var associatedFlyout = openFlyouts[0];
Assert.IsInstanceOfType(associatedFlyout, typeof(NativeTimePickerFlyout));

#if __ANDROID__
var nativeFlyout = (NativeTimePickerFlyout)associatedFlyout;

var dialog = nativeFlyout.GetNativeDialog();

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 timeSelector = nativeFlyout.GetTimeSelector();
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
}

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);
}
#if __ANDROID__
private Android.Widget.TimePicker FindTimePicker(Android.Views.View root)
{
if (root is Android.Widget.TimePicker picker)
{
return picker;
}

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
#endif

class MyContext
{
public object StartTime => null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ partial class NativeTimePickerFlyout
private TimeSpan _initialTime;

internal bool IsNativeDialogOpen => _dialog?.IsShowing ?? false;
internal UnoTimePickerDialog GetNativeDialog() => _dialog;


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

SaveInitialTime();

ShowTimePicker();
Expand All @@ -28,6 +35,7 @@ internal protected override void Open()

private void SaveInitialTime() => _initialTime = Time;


private void SaveTime(TimeSpan time)
{
if (Time != time)
Expand Down Expand Up @@ -58,7 +66,7 @@ private void AdjustAndSaveTime(int hourOfDay, int minutes)

private void ShowTimePicker()
{
var time = Time.RoundToNextMinuteInterval(MinuteIncrement);
var time = _initialTime.RoundToNextMinuteInterval(MinuteIncrement);
var listener = new OnSetTimeListener((view, hourOfDay, minute) => AdjustAndSaveTime(hourOfDay, minute));

var themeResourceId = CoreApplication.RequestedTheme == Uno.Helpers.Theming.SystemTheme.Light ?
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 @@ -21,6 +21,8 @@ partial class NativeTimePickerFlyout
private FlyoutPresenter? _timePickerPresenter;
private bool _isInitialized;

internal TimePickerSelector? GetTimeSelector() => _timeSelector;

protected override void InitializePopupPanel()
{
_popup.PopupPanel = new PickerFlyoutPopupPanel(this)
Expand Down Expand Up @@ -144,6 +146,11 @@ void onUnload(object sender, RoutedEventArgs e)

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

InitializeContent();

_timeSelector?.Initialize();
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TimePicker/TimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ namespace Microsoft.UI.Xaml.Controls;
[ContentProperty(Name = nameof(Header))]
public partial class TimePicker : Control
{
internal const long DEFAULT_TIME_TICKS = -1;
Kunal22shah marked this conversation as resolved.
Show resolved Hide resolved

}
Loading