Skip to content

Commit

Permalink
Merge pull request #18991 from unoplatform/dev/ks/native-timepicker
Browse files Browse the repository at this point in the history
chore: native timepicker- set -initial time
  • Loading branch information
kazo0 authored Dec 11, 2024
2 parents 06ad82c + e6443a8 commit 13630a7
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Microsoft.UI.Xaml.Controls;

partial class TimePicker
{
internal const long DEFAULT_TIME_TICKS = -1;
#region FlyoutPlacement DependencyProperty

[UnoOnly]
Expand Down

0 comments on commit 13630a7

Please sign in to comment.