From 3854c4049f65bb3aed539dbff5e311b6f3cbae9d Mon Sep 17 00:00:00 2001 From: Tomasz Jaworski Date: Mon, 8 Jul 2019 15:55:07 +0200 Subject: [PATCH 1/2] Cache app display name --- Assets/PatchKit Patcher/Scripts/GameTitle.cs | 49 ++++++++++++++++++-- CHANGELOG.md | 4 ++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/Assets/PatchKit Patcher/Scripts/GameTitle.cs b/Assets/PatchKit Patcher/Scripts/GameTitle.cs index ee9fc886..c7b0d2e4 100644 --- a/Assets/PatchKit Patcher/Scripts/GameTitle.cs +++ b/Assets/PatchKit Patcher/Scripts/GameTitle.cs @@ -2,6 +2,7 @@ using UnityEngine.UI; using UniRx; using PatchKit.Unity.Patcher.Debug; +using PatchKit.Unity.Patcher.AppData.Local; namespace PatchKit.Unity { @@ -9,6 +10,8 @@ public class GameTitle : MonoBehaviour { public Text Text; + private bool _hasBeenSet; + private void Start() { var patcher = Patcher.Patcher.Instance; @@ -16,12 +19,52 @@ private void Start() Assert.IsNotNull(patcher); Assert.IsNotNull(Text); + patcher.Data + .ObserveOnMainThread() + .SkipWhile(data => string.IsNullOrEmpty(data.AppSecret)) + .Select(x => x.AppSecret) + .First() + .Subscribe(UseCachedText) + .AddTo(this); + patcher.AppInfo .ObserveOnMainThread() - .Select(app => app.DisplayName) - .Where(s => !string.IsNullOrEmpty(s)) - .SubscribeToText(Text) + .Where(x => !string.IsNullOrEmpty(x.DisplayName)) + .Subscribe(SetAndCacheText) .AddTo(this); } + + private void UseCachedText(string appSecret) + { + if(_hasBeenSet) + { + return; + } + + var cachedDisplayName = GetCache(appSecret) + .GetValue("app-display-name", null); + + if (string.IsNullOrEmpty(cachedDisplayName)) + { + return; + } + + Text.text = cachedDisplayName; + } + + private void SetAndCacheText(PatchKit.Api.Models.Main.App app) + { + string displayName = app.DisplayName; + + GetCache(app.Secret).SetValue("app-display-name", displayName); + Text.text = displayName; + + _hasBeenSet = true; + } + + private ICache GetCache(string appSecret) + { + return new UnityCache(appSecret); + } } } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index a841658a..7675a4c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [3.x.x.x] +### Added +- Caching application display name so it can be displayed in offline mode (#1350) + ## [3.14.0.0] ### Added - Automated scripting runtime changing to .NET 3.5 on Unity 2017 or newer (#1241) From ef861c3fb2b5e3c5379fe23972577ed3fcc276fe Mon Sep 17 00:00:00 2001 From: Tomasz Jaworski Date: Wed, 10 Jul 2019 10:30:43 +0200 Subject: [PATCH 2/2] Refactoring --- Assets/PatchKit Patcher/Scripts/GameTitle.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Assets/PatchKit Patcher/Scripts/GameTitle.cs b/Assets/PatchKit Patcher/Scripts/GameTitle.cs index c7b0d2e4..f22bfabf 100644 --- a/Assets/PatchKit Patcher/Scripts/GameTitle.cs +++ b/Assets/PatchKit Patcher/Scripts/GameTitle.cs @@ -8,6 +8,8 @@ namespace PatchKit.Unity { public class GameTitle : MonoBehaviour { + private const string TitleCacheKey = "app-display-name"; + public Text Text; private bool _hasBeenSet; @@ -21,8 +23,8 @@ private void Start() patcher.Data .ObserveOnMainThread() - .SkipWhile(data => string.IsNullOrEmpty(data.AppSecret)) .Select(x => x.AppSecret) + .SkipWhile(string.IsNullOrEmpty) .First() .Subscribe(UseCachedText) .AddTo(this); @@ -36,13 +38,13 @@ private void Start() private void UseCachedText(string appSecret) { - if(_hasBeenSet) + if (_hasBeenSet) { return; } var cachedDisplayName = GetCache(appSecret) - .GetValue("app-display-name", null); + .GetValue(TitleCacheKey, null); if (string.IsNullOrEmpty(cachedDisplayName)) { @@ -56,7 +58,7 @@ private void SetAndCacheText(PatchKit.Api.Models.Main.App app) { string displayName = app.DisplayName; - GetCache(app.Secret).SetValue("app-display-name", displayName); + GetCache(app.Secret).SetValue(TitleCacheKey, displayName); Text.text = displayName; _hasBeenSet = true;