Skip to content

Commit

Permalink
Upgrade to .NET8 and Support logout function. (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
nodew authored Jan 14, 2024
1 parent cc25b96 commit 2ed5f2f
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 16 deletions.
2 changes: 1 addition & 1 deletion PocketClient.Core/PocketClient.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Platforms>x86;x64;arm64</Platforms>
<RootNamespace>PocketClient.Core</RootNamespace>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<RootNamespace>PocketClient.Desktop.Tests.MSTest</RootNamespace>
<Platforms>x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWinUI>true</UseWinUI>
<!-- <WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained> -->
<ProjectPriFileName>resources.pri</ProjectPriFileName>
</PropertyGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion PocketClient.Desktop/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected async override void OnLaunched(LaunchActivatedEventArgs args)

// The following code doesn't work
// See details at https://github.com/microsoft/WindowsAppSDK/issues/3179
//

//var mainInstance = AppInstance.FindOrRegisterForKey("pocket-desktop-app-main-exe");

//if (!mainInstance.IsCurrent)
Expand Down
2 changes: 2 additions & 0 deletions PocketClient.Desktop/Contracts/Services/IAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface IAuthService
public Task LaunchAuthorizationAsync();

public Task AuthorizeAsync();

public Task LogoutAsync();
}
2 changes: 2 additions & 0 deletions PocketClient.Desktop/Contracts/Services/IPocketDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public interface IPocketDbService
public string GetPocketDbPath();

public Task SyncItemsAsync(bool fullSync = false, bool force = false);

public Task ClearDbAsync();
}
5 changes: 5 additions & 0 deletions PocketClient.Desktop/Models/Messages/UserLoggedOutMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace PocketClient.Desktop.Models;

public class UserLoggedOutMessage
{
}
6 changes: 3 additions & 3 deletions PocketClient.Desktop/PocketClient.Desktop.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<RootNamespace>PocketClient.Desktop</RootNamespace>
<ApplicationIcon>Assets/WindowIcon.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<PublishProfile>Properties\PublishProfiles\win10-$(Platform).pubxml</PublishProfile>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>arm64</Platform>
<RuntimeIdentifier>win10-arm64</RuntimeIdentifier>
<RuntimeIdentifier>win-arm64</RuntimeIdentifier>
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
<SelfContained>True</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>x64</Platform>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
<SelfContained>True</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>x86</Platform>
<RuntimeIdentifier>win10-x86</RuntimeIdentifier>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
<SelfContained>True</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
Expand Down
15 changes: 14 additions & 1 deletion PocketClient.Desktop/Services/ActivationService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.Extensions.Logging;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.AppLifecycle;
using PocketClient.Desktop.Activation;
using PocketClient.Desktop.Contracts.Services;
using PocketClient.Desktop.Models;
using PocketClient.Desktop.Views;
using Windows.ApplicationModel.Activation;
using LaunchActivatedEventArgs = Microsoft.UI.Xaml.LaunchActivatedEventArgs;
Expand Down Expand Up @@ -35,6 +37,11 @@ public ActivationService(
_pocketDbService = pocketDbService;
_authService = authService;
_logger = logger;

WeakReferenceMessenger.Default.Register<UserLoggedOutMessage>(this, (sender, message) =>
{
HandleUserLoggedOutAsync();
});
}

public async Task ActivateAsync(object activationArgs)
Expand Down Expand Up @@ -166,4 +173,10 @@ private async Task StartupAsync()
}
await Task.CompletedTask;
}

private void HandleUserLoggedOutAsync()
{
App.MainWindow.Content = App.GetService<LoginPage>();
_ = App.GetService<IPocketDbService>().ClearDbAsync();
}
}
12 changes: 11 additions & 1 deletion PocketClient.Desktop/Services/AuthService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Options;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.Options;
using PocketClient.Desktop.Contracts.Services;
using PocketClient.Desktop.Models;
using PocketClient.HttpSdk;
Expand Down Expand Up @@ -90,4 +91,13 @@ public async Task AuthorizeAsync()
await _localSettingsService.SaveSettingAsync(_userNameKey, result.Username);
_isAuthorized = true;
}

public async Task LogoutAsync()
{
await _localSettingsService.SaveSettingAsync(_userAccessTokenKey, string.Empty);
await _localSettingsService.SaveSettingAsync(_userNameKey, string.Empty);
_isAuthorized = false;

WeakReferenceMessenger.Default.Send(new UserLoggedOutMessage());
}
}
6 changes: 6 additions & 0 deletions PocketClient.Desktop/Services/PocketDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,10 @@ public async Task SyncItemsAsync(bool fullSync = false, bool force = false)
}
}
}

public async Task ClearDbAsync()
{
await App.GetService<IPocketDataPersistenceService>().ClearDbAsync();
await _localSettingsService.SaveSettingAsync(_pocketLastUpdatedAtKey, string.Empty);
}
}
15 changes: 15 additions & 0 deletions PocketClient.Desktop/Strings/en-us/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,19 @@
<data name="ReadModeButton.[using:Microsoft.UI.Xaml.Controls]ToolTipService.ToolTip" xml:space="preserve">
<value>Read mode</value>
</data>
<data name="LogoutDialog_Content" xml:space="preserve">
<value>Are you sure to log out current account?</value>
</data>
<data name="LogoutDialog_PrimaryBtn" xml:space="preserve">
<value>Log out</value>
</data>
<data name="LogoutDialog_Title" xml:space="preserve">
<value>Log out?</value>
</data>
<data name="Settings_Account.Text" xml:space="preserve">
<value>Account</value>
</data>
<data name="Settings_LogoutBtn.Content" xml:space="preserve">
<value>Log out</value>
</data>
</root>
15 changes: 15 additions & 0 deletions PocketClient.Desktop/Strings/zh-Hans-CN/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@
<data name="LoginPage_Welcome.Text" xml:space="preserve">
<value>欢迎使用 Nuts, 一款为 Pocket 实现的第三方桌面应用</value>
</data>
<data name="LogoutDialog_Content" xml:space="preserve">
<value>确认要登出当前账号吗?</value>
</data>
<data name="LogoutDialog_PrimaryBtn" xml:space="preserve">
<value>登出</value>
</data>
<data name="LogoutDialog_Title" xml:space="preserve">
<value>登出?</value>
</data>
<data name="ManageTagsDialog_TagsTokenBox.PlaceholderText" xml:space="preserve">
<value>添加标签,标签之间使用分号(;)分隔</value>
</data>
Expand Down Expand Up @@ -347,6 +356,9 @@
<data name="Settings_AboutDescription.Text" xml:space="preserve">
<value>面向 GetPocket (https://getpocket.com) 的第三方 Windows 桌面应用.</value>
</data>
<data name="Settings_Account.Text" xml:space="preserve">
<value>账号</value>
</data>
<data name="Settings_Data.Text" xml:space="preserve">
<value>数据</value>
</data>
Expand All @@ -359,6 +371,9 @@
<data name="Settings_Language_Default.Content" xml:space="preserve">
<value>默认</value>
</data>
<data name="Settings_LogoutBtn.Content" xml:space="preserve">
<value>登出</value>
</data>
<data name="Settings_Personalization.Text" xml:space="preserve">
<value>个性化</value>
</data>
Expand Down
16 changes: 15 additions & 1 deletion PocketClient.Desktop/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class SettingsViewModel : ObservableRecipient
private readonly IThemeSelectorService _themeSelectorService;
private readonly IPocketDbService _pocketDbService;
private readonly ILogger<SettingsViewModel> _logger;
private readonly IAuthService _authService;

private ElementTheme _elementTheme;
private string _versionDescription;
Expand Down Expand Up @@ -64,14 +65,21 @@ public ICommand SyncDataCommand
get;
}

public ICommand LogoutCommand
{
get;
}

public SettingsViewModel(
IThemeSelectorService themeSelectorService,
IPocketDbService pocketDbService,
ILogger<SettingsViewModel> logger)
ILogger<SettingsViewModel> logger,
IAuthService authService)
{
_themeSelectorService = themeSelectorService;
_pocketDbService = pocketDbService;
_logger = logger;
_authService = authService;

_elementTheme = _themeSelectorService.Theme;
_language = GetPreferredLanguage();
Expand All @@ -81,6 +89,7 @@ public SettingsViewModel(
SwitchThemeCommand = new AsyncRelayCommand<ElementTheme>(SwitchTheme);
SwitchLanguageCommand = new RelayCommand<string>(SwitchLanguage);
SyncDataCommand = new AsyncRelayCommand(SyncData);
LogoutCommand = new AsyncRelayCommand(Logout);
}

private static string GetVersionDescription()
Expand Down Expand Up @@ -156,4 +165,9 @@ private async Task SyncData()
Syncing = false;
}
}

private async Task Logout()
{
await _authService.LogoutAsync();
}
}
8 changes: 8 additions & 0 deletions PocketClient.Desktop/Views/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@
<ProgressRing IsActive="True" Visibility="{x:Bind ViewModel.Syncing, Mode=OneWay}" HorizontalAlignment="Left" Margin="{StaticResource XXSmallTopMargin}" />
</StackPanel>

<StackPanel Margin="{StaticResource MediumTopMargin}">
<TextBlock x:Uid="Settings_Account" Style="{ThemeResource SubtitleTextBlockStyle}" />
<Button x:Uid="Settings_LogoutBtn"
Click="HandleLogoutAction"
Style="{StaticResource AccentButtonStyle}"
Margin="{StaticResource XSmallTopMargin}" />
</StackPanel>

<TextBlock x:Uid="Settings_About" Style="{ThemeResource SubtitleTextBlockStyle}" Margin="{StaticResource MediumTopMargin}" />

<StackPanel Margin="{StaticResource XSmallTopMargin}">
Expand Down
25 changes: 23 additions & 2 deletions PocketClient.Desktop/Views/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.UI.Xaml.Controls;

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using PocketClient.Desktop.Contracts.Services;
using PocketClient.Desktop.Helpers;
using PocketClient.Desktop.ViewModels;

namespace PocketClient.Desktop.Views;
Expand All @@ -17,4 +19,23 @@ public SettingsPage()
ViewModel = App.GetService<SettingsViewModel>();
InitializeComponent();
}

private async void HandleLogoutAction(object sender, RoutedEventArgs e)
{
ContentDialog dialog = new ContentDialog();
dialog.XamlRoot = this.XamlRoot;
dialog.RequestedTheme = App.GetService<IThemeSelectorService>().Theme;
dialog.Title = "LogoutDialog_Title".Format();
dialog.Content = "LogoutDialog_Content".Format();
dialog.PrimaryButtonText = "LogoutDialog_PrimaryBtn".Format();
dialog.SecondaryButtonText = "Button_Cancel".Format();
dialog.DefaultButton = ContentDialogButton.Primary;

var result = await dialog.ShowAsync();

if (result == ContentDialogResult.Primary)
{
ViewModel.LogoutCommand.Execute(null);
}
}
}
2 changes: 1 addition & 1 deletion PocketClient.HttpSdk/PocketClient.HttpSdk.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Platforms>x86;x64;arm64</Platforms>
<LangVersion>10.0</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down

0 comments on commit 2ed5f2f

Please sign in to comment.