Skip to content

Commit 3a373e0

Browse files
authored
Fix statusBar color changes on modal pages (#2413)
1 parent d694f5f commit 3a373e0

File tree

8 files changed

+381
-29
lines changed

8 files changed

+381
-29
lines changed

CONTRIBUTING.md

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Having that said, if you are a first-timer and you could use some help please re
88

99
Furthermore, for anyone, we would like you to take into consideration the following guidelines.
1010

11+
## Kindness
12+
1113
### Make an effort to be nice
1214

1315
If you disagree, that's fine. We don't think about everything the same way, be respectful and at some point decide to agree to disagree. If a decision needs to be made, try to involve at least one other person without continuing an endless discussion.
@@ -20,13 +22,13 @@ During a code review try to make a habit out of it to say at least one nice thin
2022

2123
Remember English is not everyones native language. Written communication always lacks non-verbal communication. With written communication in a language that is not your native tongue it is even harder to express certain emotions.
2224

23-
Always assume that people mean to do right. Try to read a sentence a couple of times over and take things more literal. Try to place yourself in their shoes and see the message beyond the actual words.
25+
Always assume that people mean to do right. Try to read a sentence a couple of times over and take things more literal. Try to place yourself in their shoes and see the message beyond the actual words.
2426

2527
Things might come across different than they were intended, please keep that in mind and always check to see how someone meant it. If you're not sure, pull someone offline in a private channel on Twitter or email and chat about it for a bit. Maybe even jump on a call to collaborate. We're living in the 21st century, all the tools are there, why not use them to get to know each other and be friends?!
2628

2729
Besides language, we understand that contributing to open-source mostly happens in your spare time. Remember that priorities might change and we can only spend our time once. This works as a two-way street: don't expect things to be solved instantly, but also please let us know if you do not have the capacity to finish work you have in progress. There is no shame in that. That way it's clear to other people that they can step in and take over.
2830

29-
### THANK YOU!
31+
### THANK YOU
3032

3133
Lastly, a big thank you for spending your precious time on our project. We appreciate any effort you make to help us with this project.
3234

@@ -45,7 +47,7 @@ We will take appropriate actions and measures if necessary.
4547
1. Install latest stable [.NET SDK](https://dotnet.microsoft.com/en-us/download)
4648
1. Install .NET MAUI workloads (we recommend using Visual Studio installer)
4749

48-
> You will need to complete a Contribution License Agreement before any pull request can be accepted. Complete the CLA at https://cla.dotnetfoundation.org/. This will also be triggered whenever you open a PR and the link should guide you through it.
50+
> You will need to complete a Contribution License Agreement before any pull request can be accepted. Complete the CLA at <https://cla.dotnetfoundation.org/>. This will also be triggered whenever you open a PR and the link should guide you through it.
4951
5052
## Reporting a bug
5153

@@ -59,10 +61,10 @@ We always request a reproduction sample, and that's not to make your life hard o
5961
6062
And most important: **Please, help us to help you ❤️**
6163

62-
6364
## Opening a PR process
6465

6566
### TL;DR
67+
6668
* Find an issue/feature, make sure that the issue/feature has been `Approved` and is welcomed (also see [New Features](https://github.com/CommunityToolkit/Maui#submitting-a-new-feature))
6769
* Fork repository
6870
* Create branch
@@ -73,13 +75,16 @@ And most important: **Please, help us to help you ❤️**
7375

7476
### Please consider
7577

76-
#### Tabs vs. Spaces?!
78+
#### Tabs vs. Spaces?
79+
7780
[Tabs](https://www.reddit.com/r/javascript/comments/c8drjo/nobody_talks_about_the_real_reason_to_use_tabs/).
7881

7982
#### Make your changes small, don't keep adding
83+
8084
We love your enthusiasm, but small changes and small PRs are easier to digest. We're all doing this in our spare time, it is easier to review a couple of small things and merge that and iterate from there than to have a PR with 100+ files changed that will sit there forever.
8185

8286
#### Added features should have tests, a sample and documentation
87+
8388
We like quality as much as the next person, so please provide tests.
8489

8590
In addition, we would want a new feature or change to be as clear as possible for other developers. Please add a sample to the sample app as part of your PR and also provide a PR to our [documentation repository](https://github.com/MicrosoftDocs/CommunityToolkit).
@@ -88,67 +93,75 @@ In addition, we would want a new feature or change to be as clear as possible fo
8893

8994
If you are unsure on where to locate the changes you need to make then please use the following section and flowchart.
9095

91-
![](https://user-images.githubusercontent.com/13558917/145694198-7addbd35-0e5f-4816-b351-759a01ec2672.png)
96+
![New Code Workflow](https://user-images.githubusercontent.com/13558917/145694198-7addbd35-0e5f-4816-b351-759a01ec2672.png)
9297

9398
### CommunityToolkit.Maui.Core
9499

95100
In general, this project will have all the basement to develop our Toolkit, including some primitive types, interfaces and base classes, base views, and common code. This will be referenced by other Frameworks/Toolkit based on .NET MAUI that wants to have the same features that us.
96101

97-
Here we will have some:
102+
We ask that all classes in `CommunityToolkit.Maui.Core` are `public`. This allows developers to extend `CommunityToolkit.Maui.Core`. The `[EditorBrowsable(EditorBrowsableState.Never)]` attribute can be added to classes that we don't recommend developers discovering.
98103

99-
- BaseViews, could be Views that will be used by other Views, like PaddingButton (that's used by Snackbar) or the MauiPopup (used by Popup) that will be a native control implemented in a way that can work with our handler. This same approach is used here
104+
Here is an example of classes that will
100105

101-
- Primitives, which will be base types that can be used by everyone, like our MathOperator. So other frameworks may not have the concept of Behavior or Converter but they can mimic them as helper classes/methods and use our primitives.
106+
* BaseViews, could be Views that will be used by other Views, like PaddingButton (that's used by Snackbar) or the MauiPopup (used by Popup) that will be a native control implemented in a way that can work with our handler. This same approach is used here
102107

103-
- Common Code, this will be all generic code (platform-specific or not) that can be used by other Frameworks/Toolkits
108+
* Primitives, which will be base types that can be used by everyone, like our MathOperator. So other frameworks may not have the concept of Behavior or Converter but they can mimic them as helper classes/methods and use our primitives.
104109

105-
- Layout Managers, were introduced on .NET MAUI and they live on Microsoft.Maui.Core so makes sense to have our managers on Core as well.
110+
* Common Code, this will be all generic code (platform-specific or not) that can be used by other Frameworks/Toolkits
106111

107-
- Handlers, on Core will be the most general Handler with the majority of features.
112+
* Layout Managers, were introduced on .NET MAUI and they live on Microsoft.Maui.Core so makes sense to have our managers on Core as well.
108113

109-
### CommunityToolkit.Maui:
114+
* Handlers, on Core will be the most general Handler with the majority of features.
115+
116+
### CommunityToolkit.Maui
110117

111118
This project has a reference to the Core project. Here will live the implementation of our Controls, Views, Behaviors, Animations, etc. In other words, this project will work with the .NET MAUI and will be MVVM friendly. Also, other Toolkits/Frameworks can reference this package if needed.
112119

113120
Here we will have some:
114121

115-
- View Implementation, with BindableProperties, support to attach effects, behaviors, triggers, and all that jazz.
122+
* View Implementation, with BindableProperties, support to attach effects, behaviors, triggers, and all that jazz.
116123

117-
- Platform Configuration, that is Platform-specific features, that can relate to some control - like the ArrowDirection that is part of Popup and works just on iOS - or the application itself - like the StatusBarColorEffect from XCT.
124+
* Platform Configuration, that is Platform-specific features, that can relate to some control - like the ArrowDirection that is part of Popup and works just on iOS - or the application itself - like the StatusBarColorEffect from XCT.
118125

119-
- Handlers Implementation, We will add to our PropertyMapper and/or CommandMapper any Platform Configuration that some Handler/View may have. We also can implement here some features that we think will not be great to have on Core. Here is a reference for this
126+
* Handlers Implementation, We will add to our PropertyMapper and/or CommandMapper any Platform Configuration that some Handler/View may have. We also can implement here some features that we think will not be great to have on Core. Here is a reference for this
120127

121-
- Layout, will be the implementation of ours custom layouts and will use the Layout Managers on Core
128+
* Layout, will be the implementation of ours custom layouts and will use the Layout Managers on Core
122129

123130
## Contributing Code - Best Practices
124131

125132
### Debug Logging
133+
126134
* Always use `Trace.WriteLine()` instead of `Debug.WriteLine` for debug logging because `Debug.WriteLine` is removed by the compiler in Release builds
127135

128136
### Methods Returning Task and ValueTask
137+
129138
* Always include a `CancellationToken` as a parameter to every method returning `Task` or `ValueTask`
130139
* If the method is public, provide a the default value for the `CancellationToken` (eg `CancellationToken token = default`)
131140
* If the method is not publc, do not provide a default value for the `CancellationToken`
132141
* Use `CancellationToken.ThrowIfCancellationRequested()` to verify the `CancellationToken`
133142

134143
### Enums
144+
135145
* Always use `Unknown` at index 0 for return types that may have a value that is not known
136146
* Always use `Default` at index 0 for option types that can use the system default option
137147
* Follow naming guidelines for tense... `SensorSpeed` not `SensorSpeeds`
138148
* Assign values (0,1,2,3) for all enums
139149

140150
### Property Names
151+
141152
* Include units only if one of the platforms includes it in their implementation. For instance HeadingMagneticNorth implies degrees on all platforms, but PressureInHectopascals is needed since platforms don't provide a consistent API for this.
142153

143154
### Units
155+
144156
* Use the standard units and most well accepted units when possible. For instance Hectopascals are used on UWP/Android and iOS uses Kilopascals so we have chosen Hectopascals.
145157

146158
### Pattern matching
147159

148160
#### Null checking
161+
149162
* Prefer using `is` when checking for null instead of `==`.
150163

151-
e.g.
164+
e.g.
152165

153166
```csharp
154167
// null
@@ -180,9 +193,10 @@ if (something is Bucket bucket)
180193
```
181194

182195
### File Scoped Namespaces
196+
183197
* Use [file scoped namespaces](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/file-scoped-namespaces) to help reduce code verbosity.
184198

185-
e.g.
199+
e.g.
186200

187201
```csharp
188202
namespace CommunityToolkit.Maui.Converters;
@@ -215,13 +229,13 @@ In other words, `NotImplementedException` implies that a feature is still in dev
215229

216230
### Bug Fixes
217231

218-
If you're looking for something to fix, please browse [open issues](https://github.com/CommunityToolkit/Maui/issues).
232+
If you're looking for something to fix, please browse [open issues](https://github.com/CommunityToolkit/Maui/issues).
219233

220234
Follow the style used by the [.NET Foundation](https://github.com/dotnet/runtime/blob/master/docs/coding-guidelines/coding-style.md), with two primary exceptions:
221235

222-
- We do not use the `private` keyword as it is the default accessibility level in C#.
223-
- We will **not** use `_` or `s_` as a prefix for internal or private field names
224-
- We will use `camelCaseFieldName` for naming internal or private fields in both instance and static implementations
236+
* We do not use the `private` keyword as it is the default accessibility level in C#.
237+
* We will **not** use `_` or `s_` as a prefix for internal or private field names
238+
* We will use `camelCaseFieldName` for naming internal or private fields in both instance and static implementations
225239

226240
Read and follow our [Pull Request template](https://github.com/CommunityToolkit/Maui/blob/main/.github/PULL_REQUEST_TEMPLATE.md)
227241

@@ -230,6 +244,7 @@ Read and follow our [Pull Request template](https://github.com/CommunityToolkit/
230244
To propose a change or new feature, review the guidance on [Submitting a New Feature](https://github.com/CommunityToolkit/Maui#submitting-a-new-feature).
231245

232246
#### Non-Starter Topics
247+
233248
The following topics should generally not be proposed for discussion as they are non-starters:
234249

235250
* Large renames of APIs

src/CommunityToolkit.Maui.Core/AppBuilderExtensions.shared.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
namespace CommunityToolkit.Maui.Core;
1+
using System.Diagnostics;
2+
using CommunityToolkit.Maui.Core.Services;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Maui.LifecycleEvents;
5+
using Microsoft.Maui.Platform;
6+
7+
namespace CommunityToolkit.Maui.Core;
28

39
/// <summary>
410
/// <see cref="MauiAppBuilder"/> Extensions
@@ -11,9 +17,44 @@ public static class AppBuilderExtensions
1117
/// <param name="builder"><see cref="MauiAppBuilder"/> generated by <see cref="MauiApp"/> </param>
1218
/// <param name="options"><see cref="Options"/></param>
1319
/// <returns><see cref="MauiAppBuilder"/> initialized for <see cref="CommunityToolkit.Maui.Core"/></returns>
14-
public static MauiAppBuilder UseMauiCommunityToolkitCore(this MauiAppBuilder builder, Action<Options>? options = default)
20+
public static MauiAppBuilder UseMauiCommunityToolkitCore(this MauiAppBuilder builder, Action<Options>? options = null)
1521
{
1622
options?.Invoke(new Options());
23+
24+
#if ANDROID
25+
if (Options.ShouldUseStatusBarBehaviorOnAndroidModalPage)
26+
{
27+
builder.Services.AddSingleton<IDialogFragmentService, DialogFragmentService>();
28+
29+
builder.ConfigureLifecycleEvents(static lifecycleBuilder =>
30+
{
31+
lifecycleBuilder.AddAndroid(static androidBuilder =>
32+
{
33+
androidBuilder.OnCreate(static (activity, _) =>
34+
{
35+
if (activity is not AndroidX.AppCompat.App.AppCompatActivity componentActivity)
36+
{
37+
Trace.WriteLine($"Unable to Modify Android StatusBar On ModalPage: Activity {activity.LocalClassName} must be an {nameof(AndroidX.AppCompat.App.AppCompatActivity)}");
38+
return;
39+
}
40+
41+
if (componentActivity.GetFragmentManager() is not AndroidX.Fragment.App.FragmentManager fragmentManager)
42+
{
43+
Trace.WriteLine($"Unable to Modify Android StatusBar On ModalPage: Unable to retrieve fragment manager from {nameof(AndroidX.AppCompat.App.AppCompatActivity)}");
44+
return;
45+
}
46+
47+
var dialogFragmentService = IPlatformApplication.Current?.Services.GetRequiredService<IDialogFragmentService>()
48+
?? throw new InvalidOperationException($"Unable to retrieve {nameof(IDialogFragmentService)}");
49+
50+
51+
fragmentManager.RegisterFragmentLifecycleCallbacks(new FragmentLifecycleManager(dialogFragmentService), false);
52+
});
53+
});
54+
});
55+
}
56+
#endif
57+
1758
return builder;
1859
}
1960
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Android.Content;
2+
using Android.Views;
3+
4+
namespace CommunityToolkit.Maui.Core;
5+
6+
public interface IDialogFragmentService
7+
{
8+
void OnFragmentAttached(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f, Context context);
9+
void OnFragmentCreated(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f, Bundle? savedInstanceState);
10+
void OnFragmentDestroyed(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f);
11+
void OnFragmentDetached(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f);
12+
void OnFragmentPaused(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f);
13+
void OnFragmentPreAttached(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f, Context context);
14+
void OnFragmentPreCreated(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f, Bundle? savedInstanceState);
15+
void OnFragmentResumed(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f);
16+
void OnFragmentSaveInstanceState(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f, Bundle outState);
17+
void OnFragmentStarted(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f);
18+
void OnFragmentStopped(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f);
19+
void OnFragmentViewCreated(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f, View v, Bundle? savedInstanceState);
20+
void OnFragmentViewDestroyed(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment f);
21+
}

src/CommunityToolkit.Maui.Core/Options.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,14 @@ namespace CommunityToolkit.Maui.Core;
55
/// </summary>
66
public class Options
77
{
8+
internal static bool ShouldUseStatusBarBehaviorOnAndroidModalPage { get; private set; } = true;
9+
10+
/// <summary>
11+
/// Enables the use of the DialogFragment Lifecycle service for Android.
12+
/// </summary>
13+
/// <param name="value">true if yes or false if you want to implement your own.</param>
14+
/// <remarks>
15+
/// Default value is true.
16+
/// </remarks>
17+
public void SetShouldUseStatusBarBehaviorOnAndroidModalPage(bool value) => ShouldUseStatusBarBehaviorOnAndroidModalPage = value;
818
}

0 commit comments

Comments
 (0)