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

Add warning in focus page if the system's focus mode is on. #383

Closed
wants to merge 9 commits into from
2 changes: 1 addition & 1 deletion src/AmbientSounds.Uwp/AmbientSounds.Uwp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AssemblyName>AmbientSounds.Uwp</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.22000.0</TargetPlatformVersion>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.22621.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.18362.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
Expand Down
17 changes: 17 additions & 0 deletions src/AmbientSounds.Uwp/Controls/FocusTimerModule.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>

<winui:InfoBar
Title="System focus mode is active!"
Margin="0,0,0,8"
IsOpen="{x:Bind ViewModel.IsFocusMessageVisible, Mode=OneWay}"
CloseButtonClick="OnFocusMessageCloseClicked"
Message="Ambie may not be able to send you notifications while the system's Focus mode is active. Consider adding an exception for Ambie."
Visibility="{x:Bind ViewModel.IsFocusMessageVisible, Mode=OneWay}">
<winui:InfoBar.Content>
<StackPanel Spacing="8" Orientation="Horizontal" Margin="0,0,0,20">
<Button Content="Open notification settings"
Click="OnNotificationSettingsClicked"/>
<HyperlinkButton Content="Don't show this again"
Click="OnFocusMessageIgnoreClicked"/>
</StackPanel>
</winui:InfoBar.Content>
</winui:InfoBar>

<winui:InfoBar
Title="{x:Bind strings:Resources.FocusTimerHelpTitle}"
Margin="0,0,0,8"
Expand Down
51 changes: 51 additions & 0 deletions src/AmbientSounds.Uwp/Controls/FocusTimerModule.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using JeniusApps.Common.Telemetry;
using Windows.Foundation.Metadata;
using Windows.UI.Shell;
using System;
using JeniusApps.Common.Tools;
using JeniusApps.Common.Settings;
using IUserSettings = AmbientSounds.Services.IUserSettings;
using Windows.System;

namespace AmbientSounds.Controls;

Expand All @@ -19,10 +26,26 @@ public sealed partial class FocusTimerModule : UserControl, ICanInitialize
{ "page", "focus" }
};

private IDispatcherQueue _dispatcherQueue;

public FocusTimerModule()
{
this.InitializeComponent();
DataContext = App.Services.GetRequiredService<FocusTimerModuleViewModel>();
_dispatcherQueue = App.Services.GetRequiredService<IDispatcherQueue>();

// This is not present in Windows 10, prevent exception.
if (ApiInformation.IsTypePresent("Windows.UI.Shell.FocusSessionManager"))
{
if (FocusSessionManager.IsSupported)
{
FocusSessionManager manager = FocusSessionManager.GetDefault();

ViewModel.IsFocusEnabled = manager.IsFocusActive;

manager.IsFocusActiveChanged += OnFocusActiveToggled;
}
}
}

public FocusTimerModuleViewModel ViewModel => (FocusTimerModuleViewModel)this.DataContext;
Expand Down Expand Up @@ -109,6 +132,15 @@ private void CloseAll()
TeachingTip4.IsOpen = false;
}

private void OnFocusActiveToggled(FocusSessionManager sender, object args)
{
// Must run in UI thread, otherwise it gets mad.
_dispatcherQueue.TryEnqueue(() =>
{
ViewModel.IsFocusEnabled = sender.IsFocusActive;
});
}

private void OnResetClicked(object sender, RoutedEventArgs e)
{
StartButton.Focus(Windows.UI.Xaml.FocusState.Programmatic);
Expand Down Expand Up @@ -153,4 +185,23 @@ private void OnSettingsFlyoutItemToggled(object sender, RoutedEventArgs e)
}
}
}

private void OnFocusMessageCloseClicked(InfoBar sender, object args)
{
// Hides the message until the app is restarted.
ViewModel.IsFocusMessageHidden = true;
}

private void OnFocusMessageIgnoreClicked(object sender, RoutedEventArgs e)
{
ViewModel.IsFocusMessageIgnored = true;
}

private void OnNotificationSettingsClicked(object sender, RoutedEventArgs e)
{
Uri settingsUri = new Uri("ms-settings:notifications");

// Can't use await here.
Launcher.LaunchUriAsync(settingsUri).AsTask().Wait();
}
}
7 changes: 7 additions & 0 deletions src/AmbientSounds/Constants/UserSettingsConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public static class UserSettingsConstants
/// </summary>
public const string HasClosedFocusHelpMessageKey = "HasClosedFocusHelpMessage";

/// <summary>
/// Used to remember if the user has clicked "Don't show this again" on
/// the system focus mode message.
/// </summary>
public const string HasIgnoredFocusModeMessageKey = "HasIgnoredFocusModeMessage";

/// <summary>
/// Used to remember if the user has closed the interruption help
/// at least once.
Expand Down Expand Up @@ -157,6 +163,7 @@ public static class UserSettingsConstants
{ DisableSmtcSupportKey, false },
{ LastUsedScreensaverKey, string.Empty },
{ HasClosedFocusHelpMessageKey, false },
{ HasIgnoredFocusModeMessageKey, false },
{ HasClosedInterruptionMessageKey, false },
{ RecentFocusKey, Array.Empty<RecentFocusSettings>() },
{ DevicePresenceIdKey, string.Empty },
Expand Down
29 changes: 29 additions & 0 deletions src/AmbientSounds/ViewModels/FocusTimerModuleViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public partial class FocusTimerModuleViewModel : ObservableObject
private readonly ICompactNavigator _compactNavigator;
private readonly IDispatcherQueue _dispatcherQueue;
private bool _isHelpMessageVisible;
private bool _isFocusMessageVisible;
private bool _isFocusMessageIgnored;
private int _focusLength;
private int _restLength;
private int _repetitions;
Expand Down Expand Up @@ -84,6 +86,9 @@ public partial class FocusTimerModuleViewModel : ObservableObject
private string _currentStatus = string.Empty;
[ObservableProperty]
private int _selectedTaskIndex;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsFocusMessageVisible))]
public bool _isFocusEnabled;

public FocusTimerModuleViewModel(
IFocusService focusService,
Expand Down Expand Up @@ -159,6 +164,19 @@ public FocusTimerModuleViewModel(

public bool IsHelpIconVisible => !IsHelpMessageVisible && SlidersEnabled;

public bool IsFocusMessageHidden = false;

public bool IsFocusMessageIgnored
{
get => _userSettings.Get<bool>(UserSettingsConstants.HasIgnoredFocusModeMessageKey);
set
{
SetProperty(ref _isFocusMessageIgnored, value);
_userSettings.Set(UserSettingsConstants.HasIgnoredFocusModeMessageKey, value);
OnPropertyChanged(nameof(IsFocusMessageVisible));
}
}

public bool IsHelpMessageVisible
{
get => _isHelpMessageVisible;
Expand All @@ -170,6 +188,17 @@ public bool IsHelpMessageVisible
_userSettings.Set(UserSettingsConstants.HasClosedFocusHelpMessageKey, true);
}
OnPropertyChanged(nameof(IsHelpIconVisible));
OnPropertyChanged(nameof(IsFocusMessageVisible));
}
}

public bool IsFocusMessageVisible
{
get => !IsFocusMessageIgnored && !IsFocusMessageHidden && IsFocusEnabled && !IsHelpMessageVisible;
set
{
SetProperty(ref _isFocusMessageVisible, value);
OnPropertyChanged(nameof(IsFocusMessageVisible));
}
}

Expand Down