Skip to content

Commit

Permalink
Add information about loading stage
Browse files Browse the repository at this point in the history
  • Loading branch information
Drombeys committed Apr 30, 2024
1 parent ba6861a commit 0d7f2a0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
57 changes: 38 additions & 19 deletions src/ImeSense.Launchers.Belarus.Core/Manager/InitializerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ public class InitializerManager(

public void InitializeLocale()
{
var userSettings = _userManager.UserSettings ??
throw new Exception("Error loading user config!");

if (userSettings.Locale is null) {
if (_userManager is null) {
throw new NullReferenceException("User manager object is null");
}
if (_userManager.UserSettings is null) {
throw new NullReferenceException("User settings object is null");
}
if (_userManager.UserSettings.Locale is null) {
_logger?.LogError("Locale was not set");
userSettings.Locale = _launcherStorage.Locales[0];
_userManager.UserSettings.Locale = _launcherStorage.Locales[0];
}

_localeManager.SetLocale(userSettings.Locale.Key);
_localeManager.SetLocale(_userManager.UserSettings.Locale.Key);
}

public async Task InitializeAsync(ISplashScreenManager splashScreenManager)
Expand Down Expand Up @@ -71,7 +74,11 @@ public async Task InitializeAsync(ISplashScreenManager splashScreenManager)
if (_launcherStorage.IsCheckGitHubConnection) {
await HandleOnlineInitializationAsync(splashScreenManager, stopwatch);
} else {
await HandleOfflineInitializationAsync(splashScreenManager);
splashScreenManager.UpdateInformation(new InformationMessage(
_localeManager.GetStringByKey("LocalizedStrings.OffineLoading"),
_localeManager.GetStringByKey("LocalizedStrings.LoadLocalData")));

await HandleOfflineInitializationAsync(splashScreenManager.CancellationToken);
}

stopwatch.Stop();
Expand All @@ -92,6 +99,10 @@ private async Task HandleOnlineInitializationAsync(ISplashScreenManager splashSc
throw new NullReferenceException("User settings object is null");
}

splashScreenManager.UpdateInformation(new InformationMessage(
_localeManager.GetStringByKey("LocalizedStrings.Loading"),
_localeManager.GetStringByKey("LocalizedStrings.CheckLauncherUpdate")));

var isLauncherReleaseCurrent = await IsLauncherReleaseCurrentAsync(splashScreenManager.CancellationToken);
_logger?.LogInformation("Check launcher update time: {Time}", stopwatch.ElapsedMilliseconds);
if (!isLauncherReleaseCurrent) {
Expand All @@ -104,21 +115,29 @@ private async Task HandleOnlineInitializationAsync(ISplashScreenManager splashSc
return;
}

splashScreenManager.UpdateInformation(new InformationMessage(
_localeManager.GetStringByKey("LocalizedStrings.Loading"),
_localeManager.GetStringByKey("LocalizedStrings.CheckGameUpdate")));
_launcherStorage.GitHubRelease = await _gitStorageApiService.GetLastReleaseAsync(cancellationToken: splashScreenManager.CancellationToken);
_logger?.LogInformation("Check last release time: {Time}", stopwatch.ElapsedMilliseconds);

_launcherStorage.IsGameReleaseCurrent = await IsGameReleaseCurrentAsync(splashScreenManager.CancellationToken);
_launcherStorage.IsUserAuthorized = File.Exists(PathStorage.LauncherSetting);

if (_launcherStorage.IsGameReleaseCurrent) {
await HandleGameReleaseCurrentAsync(splashScreenManager);
splashScreenManager.UpdateInformation(new InformationMessage(
_localeManager.GetStringByKey("LocalizedStrings.Loading"),
_localeManager.GetStringByKey("LocalizedStrings.LoadLocalData")));

await HandleGameReleaseCurrentAsync(splashScreenManager.CancellationToken);
} else {
await LoadRemoteContent(splashScreenManager, _userManager.UserSettings.Locale);

await LoadRemoteContent(_userManager.UserSettings.Locale, splashScreenManager.CancellationToken);
await Task.Factory.StartNew(() => RemoteLoadWebResourcesAsync(cancellationToken: splashScreenManager.CancellationToken));
}
}

private async Task HandleGameReleaseCurrentAsync(ISplashScreenManager splashScreenManager)
private async Task HandleGameReleaseCurrentAsync(CancellationToken cancellationToken)
{
if (_userManager is null) {
throw new NullReferenceException("User manager object is null");
Expand All @@ -127,26 +146,26 @@ private async Task HandleGameReleaseCurrentAsync(ISplashScreenManager splashScre
throw new NullReferenceException("User settings object is null");
}

var contentNews = await LocaleLoadCacheAsync<LangNewsContent>(PathStorage.NewsCache) ?? [];
var contentNews = await LocaleLoadCacheAsync<LangNewsContent>(PathStorage.NewsCache, cancellationToken) ?? [];
var isContentNews = contentNews.Any(x =>
x.Locale is not null
&& _userManager.UserSettings.Locale is not null
&& x.Locale.Key.Equals(_userManager.UserSettings.Locale.Key));
if (isContentNews) {
_launcherStorage.NewsContents = new(contentNews);
} else {
await LoadRemoteContent(splashScreenManager, _userManager.UserSettings.Locale);
await LoadRemoteContent(_userManager.UserSettings.Locale, cancellationToken);
}

var contentRes = await LocaleLoadCacheAsync<WebResource>(PathStorage.WebResourcesCache);
var contentRes = await LocaleLoadCacheAsync<WebResource>(PathStorage.WebResourcesCache, cancellationToken);
if (contentRes is not null && contentRes.Count != 0) {
_launcherStorage.WebResources = new(contentRes);
} else {
await Task.Factory.StartNew(() => RemoteLoadWebResourcesAsync(cancellationToken: splashScreenManager.CancellationToken));
await Task.Factory.StartNew(() => RemoteLoadWebResourcesAsync(cancellationToken: cancellationToken));
}
}

private async Task HandleOfflineInitializationAsync(ISplashScreenManager splashScreenManager)
private async Task HandleOfflineInitializationAsync(CancellationToken cancellationToken = default)
{
if (_userManager is null) {
throw new NullReferenceException("User manager object is null");
Expand All @@ -161,15 +180,15 @@ private async Task HandleOfflineInitializationAsync(ISplashScreenManager splashS
_launcherStorage.NewsContents = new(LoadErrorNews() ?? []);
}

_launcherStorage.GitHubRelease = await FileDataHelper.LoadDataAsync<GitHubRelease>(PathStorage.CurrentRelease, splashScreenManager.CancellationToken);
_launcherStorage.GitHubRelease = await FileDataHelper.LoadDataAsync<GitHubRelease>(PathStorage.CurrentRelease, cancellationToken);
}

private async Task LoadRemoteContent(ISplashScreenManager splashScreenManager, Locale? locale)
private async Task LoadRemoteContent(Locale? locale, CancellationToken cancellationToken = default)
{
if (_launcherStorage.IsUserAuthorized) {
await Task.Factory.StartNew(() => RemoteLoadNewsAsync(locale, splashScreenManager.CancellationToken), splashScreenManager.CancellationToken);
await Task.Factory.StartNew(() => RemoteLoadNewsAsync(locale, cancellationToken), cancellationToken);
} else {
await Task.Factory.StartNew(() => RemoteLoadNewsAsync(cancellationToken: splashScreenManager.CancellationToken), splashScreenManager.CancellationToken);
await Task.Factory.StartNew(() => RemoteLoadNewsAsync(cancellationToken: cancellationToken), cancellationToken);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ImeSense.Launchers.Belarus/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public override async void OnFrameworkInitializationCompleted()
var userManager = _serviceProvider.GetRequiredService<UserManager>();

var splashScreenManager = _serviceProvider.GetRequiredService<ISplashScreenManager>();
splashScreenManager.MaxProgress = 3;
splashScreenManager.MaxProgress = 4;

await userManager.LoadAsync(splashScreenManager.CancellationToken);
initializerManager.InitializeLocale();
Expand Down
4 changes: 4 additions & 0 deletions src/ImeSense.Launchers.Belarus/Assets/Locales/eng.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<s:String x:Key="LocalizedStrings.Message">Message</s:String>
<s:String x:Key="LocalizedStrings.Cancel">Cancel</s:String>
<s:String x:Key="LocalizedStrings.Loading">Loading</s:String>
<s:String x:Key="LocalizedStrings.OffineLoading">Offline loading</s:String>
<s:String x:Key="LocalizedStrings.AccessingRepository">Accessing the repository</s:String>
<s:String x:Key="LocalizedStrings.DataInitialization">Data initialization</s:String>
<s:String x:Key="LocalizedStrings.CheckLauncherUpdate">Checking for launcher updates</s:String>
<s:String x:Key="LocalizedStrings.CheckGameUpdate">Checking for game updates</s:String>
<s:String x:Key="LocalizedStrings.LoadLocalData">Loading local data</s:String>
</ResourceDictionary>
4 changes: 4 additions & 0 deletions src/ImeSense.Launchers.Belarus/Assets/Locales/rus.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<s:String x:Key="LocalizedStrings.Message">Сообщение</s:String>
<s:String x:Key="LocalizedStrings.Cancel">Отменить</s:String>
<s:String x:Key="LocalizedStrings.Loading">Загрузка лаунчера</s:String>
<s:String x:Key="LocalizedStrings.OffineLoading">Offline загрузка</s:String>
<s:String x:Key="LocalizedStrings.AccessingRepository">Обращение к репозиторию</s:String>
<s:String x:Key="LocalizedStrings.DataInitialization">Инициализация данных</s:String>
<s:String x:Key="LocalizedStrings.CheckLauncherUpdate">Проверка обновлений лаунчера</s:String>
<s:String x:Key="LocalizedStrings.CheckGameUpdate">Проверка обновлений игры</s:String>
<s:String x:Key="LocalizedStrings.LoadLocalData">Загрузка локальных данных</s:String>
</ResourceDictionary>

0 comments on commit 0d7f2a0

Please sign in to comment.