diff --git a/kr.devany.googleapi.sdPlugin/propertyInspector/adsensemanagement/index.html b/kr.devany.googleapi.sdPlugin/propertyInspector/adsensemanagement/index.html index 803b6ad..bbe2107 100644 --- a/kr.devany.googleapi.sdPlugin/propertyInspector/adsensemanagement/index.html +++ b/kr.devany.googleapi.sdPlugin/propertyInspector/adsensemanagement/index.html @@ -81,6 +81,10 @@
Back Color
+
+
User Token Name
+ +
diff --git a/kr.devany.googleapi.sdPlugin/propertyInspector/googlecalendar/index.html b/kr.devany.googleapi.sdPlugin/propertyInspector/googlecalendar/index.html index 41b66b8..c3c7713 100644 --- a/kr.devany.googleapi.sdPlugin/propertyInspector/googlecalendar/index.html +++ b/kr.devany.googleapi.sdPlugin/propertyInspector/googlecalendar/index.html @@ -1,24 +1,28 @@ - - Google Calendar - - + + Google Calendar + + - + -
-
-
Front Color
- -
-
-
Back Color
- -
-
+
+
+
Front Color
+ +
+
+
Back Color
+ +
+
+
User Token Name
+ +
+
diff --git a/src/GoogleAPIs/AdSenseManagement/ApiService.cs b/src/GoogleAPIs/AdSenseManagement/ApiService.cs index a5e2b6f..2b9d915 100644 --- a/src/GoogleAPIs/AdSenseManagement/ApiService.cs +++ b/src/GoogleAPIs/AdSenseManagement/ApiService.cs @@ -6,6 +6,7 @@ using Google.Apis.Adsense.v2; // https://googleapis.dev/dotnet/Google.Apis.Adsense.v2/latest/api/Google.Apis.Adsense.v2.html using Google.Apis.Adsense.v2.Data; +using Google.Apis.Auth.OAuth2; using DateRangeEnum = Google.Apis.Adsense.v2.AccountsResource.ReportsResource.GenerateRequest.DateRangeEnum; using DimensionsEnum = Google.Apis.Adsense.v2.AccountsResource.ReportsResource.GenerateRequest.DimensionsEnum; @@ -16,28 +17,21 @@ namespace StreamDock.Plugins.GoogleAPIs.AdSenseManagement /// /// 구글 API 서비스 대리자 /// - internal class ApiService + internal class ApiService : GoogleAuth { - private AdsenseService service; - private int maxListPageSize = 50; - private Account adSenseAccount; + readonly AdsenseService service; + readonly int maxListPageSize = 50; + Account adSenseAccount; /// /// 클래스의 새 인스턴스를 초기화합니다. /// /// 요청을 실행할 애드센스 서비스 개체입니다. /// 검색할 최대 페이지 크기입니다. - private ApiService(AdsenseService service) + internal ApiService(UserCredential userCredential, AdsenseService service) { this.service = service; - } - /// - /// 서비스 생성 - /// - /// - internal static async Task GetService() - { - return new ApiService(new AdsenseService(await GoogleAuth.GetServiceInitializer())); + base.userCredential = userCredential; } /// /// 로그인한 사용자의 모든 계정을 가져오고 출력합니다. diff --git a/src/GoogleAPIs/AdSenseManagement/ChartReport.cs b/src/GoogleAPIs/AdSenseManagement/ChartReport.cs index 0830cde..5816551 100644 --- a/src/GoogleAPIs/AdSenseManagement/ChartReport.cs +++ b/src/GoogleAPIs/AdSenseManagement/ChartReport.cs @@ -12,7 +12,7 @@ namespace StreamDock.Plugins.GoogleAPIs.AdSenseManagement { internal class ChartReport { - PluginSettings pluginSettings; + readonly PluginSettings pluginSettings; internal ChartReport(PluginSettings pluginSettings) { diff --git a/src/GoogleAPIs/AdSenseManagement/DataBinder.cs b/src/GoogleAPIs/AdSenseManagement/DataBinder.cs index 3fb6f2a..8c76bf3 100644 --- a/src/GoogleAPIs/AdSenseManagement/DataBinder.cs +++ b/src/GoogleAPIs/AdSenseManagement/DataBinder.cs @@ -5,6 +5,8 @@ using BarRaider.SdTools; +using Google.Apis.Adsense.v2; + namespace StreamDock.Plugins.GoogleAPIs.AdSenseManagement { /// @@ -12,25 +14,24 @@ namespace StreamDock.Plugins.GoogleAPIs.AdSenseManagement /// internal class DataBinder { - PluginSettings pluginSettings { get; set; } - Item item { get; set; } - + PluginSettings pluginSettings; + Item item; + GoogleAuth googleAuth; internal DataBinder(PluginSettings pluginsettings, Item item) { this.pluginSettings = pluginsettings; this.item = item; + this.googleAuth = new(); } /// /// 키가 눌렸을 때 동작 정의. Google API 통신. /// internal async Task ExecuteAsync() { - Item _item = this.item; - try { // 구글 API 통신 인스턴스 - ApiService apiSevice = await ApiService.GetService(); + ApiService apiSevice = new ApiService(googleAuth.userCredential, new AdsenseService(await googleAuth.GetServiceInitializerAsync(pluginSettings.UserTokenName))); string accountName = apiSevice.GetAccountName(); if (Item.AccountName.IsNullOrEmpty()) @@ -59,14 +60,14 @@ internal async Task ExecuteAsync() Logger.Instance.LogMessage(TracingLevel.INFO, "보고서 저장 완료."); #endif // 디스플레이용 데이터 가공 - _item = SetDisplayValue(); + item = SetDisplayValue(); } catch (Exception ex) { Logger.Instance.LogMessage(TracingLevel.ERROR, ex.Message); Logger.Instance.LogMessage(TracingLevel.ERROR, ex.StackTrace); } - return _item; + return item; } /// diff --git a/src/GoogleAPIs/AdSenseManagement/Models/Item.cs b/src/GoogleAPIs/AdSenseManagement/Models/Item.cs index e0aba70..e0618b8 100644 --- a/src/GoogleAPIs/AdSenseManagement/Models/Item.cs +++ b/src/GoogleAPIs/AdSenseManagement/Models/Item.cs @@ -27,11 +27,11 @@ public class Item internal void Init() { - Accounts = new List(); - Payments = new List(); - Sites = new List(); - ReportResults = new Dictionary { }; - ReportCharts = new Dictionary { }; + Accounts?.Clear(); + Payments?.Clear(); + Sites?.Clear(); + ReportResults?.Clear(); + ReportCharts?.Clear(); } } } diff --git a/src/GoogleAPIs/AdSenseManagement/Models/PluginSettings.cs b/src/GoogleAPIs/AdSenseManagement/Models/PluginSettings.cs index f6e68ff..fbb20ee 100644 --- a/src/GoogleAPIs/AdSenseManagement/Models/PluginSettings.cs +++ b/src/GoogleAPIs/AdSenseManagement/Models/PluginSettings.cs @@ -14,11 +14,13 @@ namespace StreamDock.Plugins.GoogleAPIs.AdSenseManagement { public class PluginSettings : INotifyPropertyChanged { + //? Json 속성 이름과 CS 속성 이름이 중복되면 안 됨. 대소문자 구분. [JsonProperty(PropertyName = "resource")] public string PiResource { get; set; } = "Payments"; private Resources resources; - public Resources Resource { + public Resources Resource + { get { var _ = PiResource.TryParse(); @@ -90,6 +92,10 @@ public DimensionsEnum Dimensions public string PiBackColor { get; set; } public Color BackColor => GraphicsTools.ColorFromHex(PiBackColor); + [JsonProperty(PropertyName = "userTokenName")] + public string PiUserTokenName { get; set; } + public string UserTokenName => PiUserTokenName; + public ValueTypes ValueType { get; set; } public static PluginSettings CreateDefaultSettings() @@ -101,6 +107,7 @@ public static PluginSettings CreateDefaultSettings() instance.PiDimensions = String.Empty; instance.PiFrontColor = "#FFFFFF"; instance.PiBackColor = String.Empty; + instance.PiUserTokenName = "user"; instance.ValueType = ValueTypes.String; return instance; } diff --git a/src/GoogleAPIs/AdSenseManagement/PluginAction.cs b/src/GoogleAPIs/AdSenseManagement/PluginAction.cs index 2624a88..a044431 100644 --- a/src/GoogleAPIs/AdSenseManagement/PluginAction.cs +++ b/src/GoogleAPIs/AdSenseManagement/PluginAction.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Drawing; using System.IO; using System.Threading.Tasks; @@ -19,7 +20,7 @@ public class PluginAction : KeypadBase { Item item; PluginSettings pluginSettings; - DataBinder apiAction; + DataBinder dataBinder; public PluginAction(ISDConnection connection, InitialPayload payload) : base(connection, payload) { @@ -34,8 +35,9 @@ public PluginAction(ISDConnection connection, InitialPayload payload) : base(con Connection.OnPropertyInspectorDidDisappear += Connection_OnPropertyInspectorDidDisappear; Connection.OnSendToPlugin += Connection_OnSendToPlugin; Connection.OnTitleParametersDidChange += Connection_OnTitleParametersDidChange; + pluginSettings.PropertyChanged += PropertyChanged; } - + #region Event /// /// 제목이 변경되거나 스트림독에 플러그인이 나타날 때 호출됩니다.titleParametersDidChange /// 하드웨어 기준. @@ -45,15 +47,25 @@ public PluginAction(ISDConnection connection, InitialPayload payload) : base(con private async void Connection_OnTitleParametersDidChange(object sender, SDEventReceivedEventArgs e) { Logger.Instance.LogMessage(TracingLevel.INFO, "OnTitleParametersDidChange Event Handled"); + //Tools.AutoPopulateSettings(pluginSettings, e.Event.Payload.Settings); - if (!GoogleAuth.CredentialIsExist()) + if (!GoogleAuth.CredentialIsExist(pluginSettings.UserTokenName)) { await DisplayInitialAsync(); } else { + //TODO 자동 갱신 or 수동 갱신 await DisplayBusyAsync(); await UpdateApiDataAsync(); + //if (!CheckExistData()) + //{ + // await DisplayInitialAsync(); + //} + //else + //{ + // await DisplayPreValueAsync(); + //} } } @@ -129,7 +141,12 @@ private void Connection_OnApplicationDidLaunch(object sender, SDEventReceivedEve { Logger.Instance.LogMessage(TracingLevel.INFO, "OnApplicationDidLaunch Event Handled"); } - + private void PropertyChanged(object sender, PropertyChangedEventArgs e) + { + Logger.Instance.LogMessage(TracingLevel.INFO, $"{e.PropertyName} Property Changed"); + } + #endregion + #region Steam Dock /// /// 스트림독에 플러그인이 표시되지 않으면 호출됩니다. /// @@ -175,16 +192,31 @@ public async override void ReceivedSettings(ReceivedSettingsPayload payload) Logger.Instance.LogMessage(TracingLevel.INFO, "ReceivedSettings called"); Tools.AutoPopulateSettings(pluginSettings, payload.Settings); + await SaveSettingsAsync(); // 스트림독으로 설정 업로드 - await SaveSettingsAsync(); - await DisplayInitialAsync(); + if (!GoogleAuth.CredentialIsExist(pluginSettings.UserTokenName)) + { + item.Init(); + dataBinder = null; + await DisplayInitialAsync(); + } + else if (CheckExistData()) + { + await DisplayBusyAsync(); + await UpdateApiDataAsync(); + } + else + { + await DisplayInitialAsync(); + } } public override void ReceivedGlobalSettings(ReceivedGlobalSettingsPayload payload) { Logger.Instance.LogMessage(TracingLevel.INFO, "ReceivedGlobalSettings called"); + Tools.AutoPopulateSettings(pluginSettings, payload.Settings); } - + #endregion #region Private Methods /// /// 설정 값을 스트림독으로 전달합니다. @@ -217,21 +249,23 @@ private async Task DisplayInitialAsync() { try { - - if (!CheckExistData()) - { -#if DEBUG - Logger.Instance.LogMessage(TracingLevel.INFO, "기존 데이터 없음."); -#endif - item.DisplayValues.OnlyOne("Press Key..."); - } - else - { + item.DisplayValues.OnlyOne("Press Key..."); #if DEBUG - Logger.Instance.LogMessage(TracingLevel.INFO, "기존 데이터 발견."); + Logger.Instance.LogMessage(TracingLevel.INFO, "DisplayInitialAsync: 스트림독으로 이미지 전송 중..."); #endif - UpdateValues(); - } + await Connection.SetImageAsync(UpdateKeyImage(item, true)); // 초기 이미지 출력 + } + catch (Exception ex) + { + Logger.Instance.LogMessage(TracingLevel.ERROR, ex.Message); + Logger.Instance.LogMessage(TracingLevel.ERROR, ex.StackTrace); + } + } + private async Task DisplayPreValueAsync() + { + try + { + UpdateValues(); #if DEBUG Logger.Instance.LogMessage(TracingLevel.INFO, "DisplayInitialAsync: 스트림독으로 이미지 전송 중..."); #endif @@ -345,7 +379,7 @@ private Bitmap UpdateKeyImage(Item item, bool initial = false) /// private DataBinder GetApiInstance() { - return apiAction ?? new DataBinder(pluginSettings, item); + return dataBinder ?? new DataBinder(pluginSettings, item); } #endregion } diff --git a/src/GoogleAPIs/GoogleAuth.cs b/src/GoogleAPIs/GoogleAuth.cs index 7af43f6..42f596c 100644 --- a/src/GoogleAPIs/GoogleAuth.cs +++ b/src/GoogleAPIs/GoogleAuth.cs @@ -3,6 +3,8 @@ using System.Threading; using System.Threading.Tasks; +using BarRaider.SdTools; + using Google.Apis.Adsense.v2; using Google.Apis.Auth.OAuth2; using Google.Apis.Calendar.v3; @@ -14,26 +16,29 @@ namespace StreamDock.Plugins.GoogleAPIs /// /// API 인증 정보 클래스 /// - internal sealed class GoogleAuth + internal class GoogleAuth { internal static readonly string TokenPath = @"DevAny\StreamDock.Plugins\GoogleAPI"; - internal static string TokenUser = "user"; - internal static readonly string TokenFile = $"Google.Apis.Auth.OAuth2.Responses.TokenResponse-{TokenUser}"; - private static UserCredential userCredential; - static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1); + internal static readonly string TokenFilePrefix = $"Google.Apis.Auth.OAuth2.Responses.TokenResponse-"; + internal UserCredential userCredential; + static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1); + + internal GoogleAuth() + { + } /// /// 사용자 인증 정보를 가져옵니다. /// /// - private static async Task GetCredential() + private async Task GetCredentialAsync(string user) { - if (userCredential != null && !userCredential.Token.IsStale) { return userCredential; } + // API 서비스를 추가합니다. var scopes = new string[] { Oauth2Service.Scope.UserinfoEmail, @@ -41,13 +46,25 @@ private static async Task GetCredential() CalendarService.Scope.Calendar, }; - using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read)) + CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(30 * 1000); + CancellationToken token = cancellationTokenSource.Token; + + try { - GoogleWebAuthorizationBroker.Folder = TokenPath; - userCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromStream(stream).Secrets, scopes, TokenUser, CancellationToken.None); + using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read)) + { + GoogleWebAuthorizationBroker.Folder = TokenPath; + userCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.FromStream(stream).Secrets, scopes, user, token); - if (userCredential.Token.IsStale) - await userCredential.RefreshTokenAsync(CancellationToken.None); + if (userCredential.Token.IsStale) + await userCredential.RefreshTokenAsync(CancellationToken.None); + } + } + catch (Exception ex) + { + userCredential = null; + Logger.Instance.LogMessage(TracingLevel.ERROR, ex.Message); + Logger.Instance.LogMessage(TracingLevel.ERROR, ex.StackTrace); } return userCredential; @@ -56,17 +73,17 @@ private static async Task GetCredential() /// 인증 서비스를 초기화합니다. /// /// - internal static async Task GetServiceInitializer() + internal async Task GetServiceInitializerAsync(string user) { BaseClientService.Initializer init = null; - if (await semaphoreSlim.WaitAsync(1 * 1000)) + if (await semaphoreSlim.WaitAsync(2 * 1000)) { try { init = new BaseClientService.Initializer { - HttpClientInitializer = await GetCredential(), + HttpClientInitializer = await GetCredentialAsync(user), ApplicationName = "StreamDock Plugin", }; } @@ -81,11 +98,11 @@ private static async Task GetCredential() } return init; } - internal static bool CredentialIsExist() + internal static bool CredentialIsExist(string user) { //TODO 구글 메서드로 유효성 점검 //! UserCredential 인증 과정 보류 시 프로세스가 멈추므로 토큰 파일 직접 확인 - return File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), TokenPath, TokenFile)); + return File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), TokenPath, $"{TokenFilePrefix}{user}")); } } } diff --git a/src/GoogleAPIs/GoogleCalendar/ApiService.cs b/src/GoogleAPIs/GoogleCalendar/ApiService.cs index 80dd3d7..e05099b 100644 --- a/src/GoogleAPIs/GoogleCalendar/ApiService.cs +++ b/src/GoogleAPIs/GoogleCalendar/ApiService.cs @@ -3,31 +3,24 @@ using System.Linq; using System.Threading.Tasks; +using Google.Apis.Auth.OAuth2; using Google.Apis.Calendar.v3; using Google.Apis.Calendar.v3.Data; namespace StreamDock.Plugins.GoogleAPIs.GoogleCalendar { - internal class ApiService + internal class ApiService: GoogleAuth { - private CalendarService service; - + readonly CalendarService service; /// /// 클래스의 새 인스턴스를 초기화합니다. /// /// 요청을 실행할 애드센스 서비스 개체입니다. /// 검색할 최대 페이지 크기입니다. - internal ApiService(CalendarService service) + internal ApiService(UserCredential userCredential, CalendarService service) { this.service = service; - } - /// - /// 서비스 생성 - /// - /// - internal static async Task GetService() - { - return new ApiService(new CalendarService(await GoogleAuth.GetServiceInitializer())); + base.userCredential = userCredential; } /// /// 계정의 모든 캘린더를 가져옵니다. diff --git a/src/GoogleAPIs/GoogleCalendar/DataBinder.cs b/src/GoogleAPIs/GoogleCalendar/DataBinder.cs index eb6479b..ffea054 100644 --- a/src/GoogleAPIs/GoogleCalendar/DataBinder.cs +++ b/src/GoogleAPIs/GoogleCalendar/DataBinder.cs @@ -4,6 +4,8 @@ using BarRaider.SdTools; +using Google.Apis.Calendar.v3; + namespace StreamDock.Plugins.GoogleAPIs.GoogleCalendar { /// @@ -11,13 +13,15 @@ namespace StreamDock.Plugins.GoogleAPIs.GoogleCalendar /// internal class DataBinder { - PluginSettings pluginSettings { get; set; } - Item item { get; set; } + PluginSettings pluginSettings; + Item item; + GoogleAuth googleAuth; internal DataBinder(PluginSettings pluginsettings, Item item) { this.pluginSettings = pluginsettings; this.item = item; + this.googleAuth = new(); } /// /// 키가 눌렸을 때 동작 정의. Google API 통신. @@ -27,7 +31,7 @@ internal async Task ExecuteAsync() try { // 구글 API 통신 인스턴스 - ApiService apiSevice = await ApiService.GetService(); + ApiService apiSevice = new ApiService(googleAuth.userCredential, new CalendarService(await googleAuth.GetServiceInitializerAsync(pluginSettings.UserTokenName))); if (pluginSettings.CalendarSummary.IsNullOrEmpty()) { @@ -62,7 +66,8 @@ internal Item SetDisplayValue() { item.DisplayValues.Clear(); - foreach (var value in item.Events) { + foreach (var value in item.Events) + { if (value.End.Date.IsDateTime()) { diff --git a/src/GoogleAPIs/GoogleCalendar/Models/Item.cs b/src/GoogleAPIs/GoogleCalendar/Models/Item.cs index f0f0d32..2740d7a 100644 --- a/src/GoogleAPIs/GoogleCalendar/Models/Item.cs +++ b/src/GoogleAPIs/GoogleCalendar/Models/Item.cs @@ -26,6 +26,10 @@ public class Item internal void Init() { + Calendars?.Clear(); + calendarID = string.Empty; + Events?.Clear(); + DisplayValues?.Clear(); } } } diff --git a/src/GoogleAPIs/GoogleCalendar/Models/PluginSettings.cs b/src/GoogleAPIs/GoogleCalendar/Models/PluginSettings.cs index 6d7b8a0..79a37b5 100644 --- a/src/GoogleAPIs/GoogleCalendar/Models/PluginSettings.cs +++ b/src/GoogleAPIs/GoogleCalendar/Models/PluginSettings.cs @@ -25,12 +25,17 @@ public class PluginSettings : INotifyPropertyChanged public string PiBackColor { get; set; } public Color BackColor => GraphicsTools.ColorFromHex(PiBackColor); + [JsonProperty(PropertyName = "userTokenName")] + public string PiUserTokenName { get; set; } + public string UserTokenName => PiUserTokenName; + public static PluginSettings CreateDefaultSettings() { PluginSettings instance = new PluginSettings(); instance.PiCalendarList = String.Empty; instance.PiFrontColor = "#FFFFFF"; instance.PiBackColor = String.Empty; + instance.PiUserTokenName = "user"; return instance; } diff --git a/src/GoogleAPIs/GoogleCalendar/PluginAction.cs b/src/GoogleAPIs/GoogleCalendar/PluginAction.cs index 5b1bcb8..f3f6a86 100644 --- a/src/GoogleAPIs/GoogleCalendar/PluginAction.cs +++ b/src/GoogleAPIs/GoogleCalendar/PluginAction.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Drawing; -using System.IO; using System.Linq; using System.Threading.Tasks; @@ -20,7 +20,7 @@ public class PluginAction : KeypadBase { Item item; PluginSettings pluginSettings; - DataBinder apiAction; + DataBinder dataBinder; public PluginAction(ISDConnection connection, InitialPayload payload) : base(connection, payload) { @@ -35,6 +35,7 @@ public PluginAction(ISDConnection connection, InitialPayload payload) : base(con Connection.OnPropertyInspectorDidDisappear += Connection_OnPropertyInspectorDidDisappear; Connection.OnSendToPlugin += Connection_OnSendToPlugin; Connection.OnTitleParametersDidChange += Connection_OnTitleParametersDidChange; + pluginSettings.PropertyChanged += PropertyChanged; } /// @@ -46,8 +47,9 @@ public PluginAction(ISDConnection connection, InitialPayload payload) : base(con private async void Connection_OnTitleParametersDidChange(object sender, SDEventReceivedEventArgs e) { Logger.Instance.LogMessage(TracingLevel.INFO, "OnTitleParametersDidChange Event Handled"); + //Tools.AutoPopulateSettings(pluginSettings, e.Event.Payload.Settings); - if (!GoogleAuth.CredentialIsExist()) + if (!GoogleAuth.CredentialIsExist(pluginSettings.UserTokenName)) { await DisplayInitialAsync(); } @@ -55,6 +57,14 @@ private async void Connection_OnTitleParametersDidChange(object sender, SDEventR { await DisplayBusyAsync(); await UpdateApiDataAsync(); + //if (!CheckExistData()) + //{ + // await DisplayInitialAsync(); + //} + //else + //{ + // await DisplayPreValueAsync(); + //} } } @@ -130,6 +140,10 @@ private void Connection_OnApplicationDidLaunch(object sender, SDEventReceivedEve { Logger.Instance.LogMessage(TracingLevel.INFO, "OnApplicationDidLaunch Event Handled"); } + private void PropertyChanged(object sender, PropertyChangedEventArgs e) + { + Logger.Instance.LogMessage(TracingLevel.INFO, $"{e.PropertyName} Property Changed"); + } /// /// 스트림독에 플러그인이 표시되지 않으면 호출됩니다. @@ -156,7 +170,7 @@ public async override void KeyReleased(KeyPayload payload) { Logger.Instance.LogMessage(TracingLevel.INFO, "KeyReleased called"); - if (CheckExistData()) + if (GoogleAuth.CredentialIsExist(pluginSettings.UserTokenName) && CheckExistData()) { System.Diagnostics.Process.Start(item.Events.First().HtmlLink); } @@ -183,14 +197,29 @@ public async override void ReceivedSettings(ReceivedSettingsPayload payload) Logger.Instance.LogMessage(TracingLevel.INFO, "ReceivedSettings called"); Tools.AutoPopulateSettings(pluginSettings, payload.Settings); - await SaveSettingsAsync(); - await DisplayInitialAsync(); + + if (!GoogleAuth.CredentialIsExist(pluginSettings.UserTokenName)) + { + item.Init(); + dataBinder = null; + await DisplayInitialAsync(); + } + else if (CheckExistData()) + { + await DisplayBusyAsync(); + await UpdateApiDataAsync(); + } + else + { + await DisplayInitialAsync(); + } } public override void ReceivedGlobalSettings(ReceivedGlobalSettingsPayload payload) { Logger.Instance.LogMessage(TracingLevel.INFO, "ReceivedGlobalSettings called"); + Tools.AutoPopulateSettings(pluginSettings, payload.Settings); } #region Private Methods @@ -219,14 +248,23 @@ private async Task DisplayInitialAsync() { try { - if (!CheckExistData()) - { - item.DisplayValues.OnlyOne("Press Key..."); - } - else - { - UpdateValues(); - } + item.DisplayValues.OnlyOne("Press Key..."); + await Connection.SetImageAsync(UpdateKeyImage(item, true)); // 초기 이미지 출력 + } + catch (Exception ex) + { + Logger.Instance.LogMessage(TracingLevel.ERROR, ex.Message); + Logger.Instance.LogMessage(TracingLevel.ERROR, ex.StackTrace); + } + } + private async Task DisplayPreValueAsync() + { + try + { + UpdateValues(); +#if DEBUG + Logger.Instance.LogMessage(TracingLevel.INFO, "DisplayInitialAsync: 스트림독으로 이미지 전송 중..."); +#endif await Connection.SetImageAsync(UpdateKeyImage(item, true)); // 초기 이미지 출력 } catch (Exception ex) @@ -287,7 +325,7 @@ private void UpdateValues() /// /// 키 이미지를 변경합니다. 출력할 정보를 이미지로 변환합니다. /// - private Bitmap UpdateKeyImage(Item item, bool initial = false) + private Bitmap UpdateKeyImage(Item item, bool autoSize = false) { Bitmap bmp = null; try @@ -306,7 +344,7 @@ private Bitmap UpdateKeyImage(Item item, bool initial = false) using (var graphics = Graphics.FromImage(bmp)) { - //font = ImageHelper.ResizeFont(graphics, item.DisplayValues[i], font); + font = autoSize ? ImageHelper.ResizeFont(graphics, item.DisplayValues[i], font) : font; graphics.DrawString(item.DisplayValues[i], font, new SolidBrush(pluginSettings.FrontColor), !isRGB ? 72 : 5, (144 / (item.DisplayValues.Count + 1)) * (i + 1), stringFormat); } @@ -326,7 +364,7 @@ private Bitmap UpdateKeyImage(Item item, bool initial = false) /// private DataBinder GetApiInstance() { - return apiAction ?? new DataBinder(pluginSettings, item); + return dataBinder ?? new DataBinder(pluginSettings, item); } #endregion }