Skip to content

Commit

Permalink
Implement ProgressColor property in ProgressBarHandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz committed Mar 26, 2021
1 parent 2a442cb commit 946586b
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/Core/src/Core/IProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ public interface IProgress : IView
/// Progress values less than 0 will be clamped to 0, values greater than 1 will be clamped to 1.
/// </summary>
double Progress { get; }

/// <summary>
/// Get the color of the progress bar.
/// </summary>
Color ProgressColor { get; }
}
}
6 changes: 6 additions & 0 deletions src/Core/src/Handlers/ProgressBar/ProgressBarHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ public partial class ProgressBarHandler
public static PropertyMapper<IProgress, ProgressBarHandler> ProgressMapper = new PropertyMapper<IProgress, ProgressBarHandler>(ViewHandler.ViewMapper)
{
[nameof(IProgress.Progress)] = MapProgress,
[nameof(IProgress.ProgressColor)] = MapProgressColor
};

public static void MapProgress(ProgressBarHandler handler, IProgress progress)
{
handler.TypedNativeView?.UpdateProgress(progress);
}

public static void MapProgressColor(ProgressBarHandler handler, IProgress progress)
{
handler.TypedNativeView?.UpdateProgressColor(progress);
}

public ProgressBarHandler() : base(ProgressMapper)
{

Expand Down
23 changes: 21 additions & 2 deletions src/Core/src/Platform/Android/ProgressBarExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Android.Content.Res;
using Android.OS;
using AProgressBar = Android.Widget.ProgressBar;

namespace Microsoft.Maui
Expand All @@ -12,5 +11,25 @@ public static void UpdateProgress(this AProgressBar nativeProgressBar, IProgress
{
nativeProgressBar.Progress = (int)(progress.Progress * Maximum);
}
}

public static void UpdateProgressColor(this AProgressBar nativeProgressBar, IProgress progress)
{
Color color = progress.ProgressColor;

if (color.IsDefault)
{
(nativeProgressBar.Indeterminate ? nativeProgressBar.IndeterminateDrawable :
nativeProgressBar.ProgressDrawable)?.ClearColorFilter();
}
else
{
var tintList = ColorStateList.ValueOf(color.ToNative());

if (nativeProgressBar.Indeterminate)
nativeProgressBar.IndeterminateTintList = tintList;
else
nativeProgressBar.ProgressTintList = tintList;
}
}
}
}
6 changes: 2 additions & 4 deletions src/Core/src/Platform/Standard/ProgressBarExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
{
public static class ProgressBarExtensions
{
public static void UpdateProgress(this object nothing, IProgress progress)
{

}
public static void UpdateProgress(this object nothing, IProgress progress) { }
public static void UpdateProgressColor(this object nothing, IProgress progress) { }
}
}
5 changes: 5 additions & 0 deletions src/Core/src/Platform/iOS/ProgressBarExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ public static void UpdateProgress(this UIProgressView nativeProgressBar, IProgre
{
nativeProgressBar.Progress = (float)progress.Progress;
}

public static void UpdateProgressColor(this UIProgressView nativeProgressBar, IProgress progress)
{
nativeProgressBar.ProgressTintColor = progress.ProgressColor == Color.Default ? null : progress.ProgressColor.ToNative();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Maui.Handlers;
using System;
using System.Threading.Tasks;
using Microsoft.Maui.Handlers;
using AProgressBar = Android.Widget.ProgressBar;

namespace Microsoft.Maui.DeviceTests
Expand All @@ -10,5 +12,18 @@ AProgressBar GetNativeProgressBar(ProgressBarHandler progressBarHandler) =>

double GetNativeProgress(ProgressBarHandler progressBarHandler) =>
(double)GetNativeProgressBar(progressBarHandler).Progress / ProgressBarExtensions.Maximum;
}

Task ValidateNativeProgressColor(IProgress progressBar, Color color, Action action = null) =>
ValidateHasColor(progressBar, color, action);

Task ValidateHasColor(IProgress progressBar, Color color, Action action = null)
{
return InvokeOnMainThreadAsync(() =>
{
var nativeProgressBar = GetNativeProgressBar(CreateHandler(progressBar));
action?.Invoke();
nativeProgressBar.AssertContainsColor(color);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
[Category("ProgressBarHandler")]
[Category(TestCategory.ProgressBar)]
public partial class ProgressBarHandlerTests : HandlerTestBase<ProgressBarHandler, ProgressBarStub>
{
[Theory(DisplayName = "Progress Initializes Correctly")]
Expand All @@ -25,5 +24,22 @@ public async Task ProgressInitializesCorrectly(double progress)

await ValidatePropertyInitValue(progressBar, () => progressBar.Progress, GetNativeProgress, progressBar.Progress);
}

[Theory(DisplayName = "Progress Color Initializes Correctly")]
[InlineData("#FF0000")]
[InlineData("#00FF00")]
[InlineData("#0000FF")]
public async Task ProgressColorInitializesCorrectly(string colorHex)
{
Color progressColor = Color.FromHex(colorHex);

var progressBar = new ProgressBarStub()
{
Progress = 0.9,
ProgressColor = progressColor
};

await ValidateNativeProgressColor(progressBar, progressColor);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using Microsoft.Maui.Handlers;
using System;
using System.Threading.Tasks;
using Microsoft.Maui.Handlers;
using UIKit;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
Expand All @@ -10,5 +13,16 @@ UIProgressView GetNativeProgressBar(ProgressBarHandler progressBarHandler) =>

double GetNativeProgress(ProgressBarHandler progressBarHandler) =>
GetNativeProgressBar(progressBarHandler).Progress;
}

async Task ValidateNativeProgressColor(IProgress progressBar, Color color, Action action = null)
{
var expected = await GetValueAsync(progressBar, handler =>
{
var native = GetNativeProgressBar(handler);
action?.Invoke();
return native.ProgressTintColor.ToColor();
});
Assert.Equal(expected, color);
}
}
}
1 change: 1 addition & 0 deletions src/Core/tests/DeviceTests/TestCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static class TestCategory
public const string Label = "Label";
public const string Layout = "Layout";
public const string Picker = "Picker";
public const string ProgressBar = "ProgressBar";
public const string SearchBar = "SearchBar";
public const string Slider = "Slider";
public const string Stepper = "Stepper";
Expand Down

0 comments on commit 946586b

Please sign in to comment.