From d956576dd3dac567d8fa6dd5c705960cdf47d800 Mon Sep 17 00:00:00 2001 From: Shevchnko Evgeniy Date: Tue, 18 Jun 2024 14:49:49 +0300 Subject: [PATCH 1/2] [ADD] New update notification --- .../Helpers/Forms/Dialogs/AppDialogs.cs | 86 +++++++++++-------- .../Forms/Dialogs/Interfaces/IDialogs.cs | 45 ++++++---- source/EduCATS/Networking/Servers.cs | 15 +++- .../Base/ViewModels/TodayPageViewModel.cs | 81 ++++++++++++++++- 4 files changed, 167 insertions(+), 60 deletions(-) diff --git a/source/EduCATS/Helpers/Forms/Dialogs/AppDialogs.cs b/source/EduCATS/Helpers/Forms/Dialogs/AppDialogs.cs index 35c7c2e4..ce85d224 100644 --- a/source/EduCATS/Helpers/Forms/Dialogs/AppDialogs.cs +++ b/source/EduCATS/Helpers/Forms/Dialogs/AppDialogs.cs @@ -7,82 +7,91 @@ using Xamarin.Forms; namespace EduCATS.Helpers.Forms.Dialogs -{ +{ /// /// implementation. /// public class AppDialogs : IDialogs - { + { /// /// Localized "OK" text. /// - static string _baseOK => CrossLocalization.Translate("base_ok"); - + static string _baseOK => CrossLocalization.Translate("base_ok"); + /// /// Localized "No" text. /// - static string _baseNo => CrossLocalization.Translate("base_no"); - + static string _baseNo => CrossLocalization.Translate("base_no"); + /// /// Localized "Yes" text. /// - static string _baseYes => CrossLocalization.Translate("base_yes"); - + static string _baseYes => CrossLocalization.Translate("base_yes"); + /// /// Localized "Error" text. /// - static string _baseError => CrossLocalization.Translate("base_error"); - + static string _baseError => CrossLocalization.Translate("base_error"); + /// /// Localized "Cancel" text. /// - static string _baseCancel => CrossLocalization.Translate("base_cancel"); - + static string _baseCancel => CrossLocalization.Translate("base_cancel"); + /// /// Localized "Loading" text. /// - static string _baseLoading => CrossLocalization.Translate("base_loading"); - + static string _baseLoading => CrossLocalization.Translate("base_loading"); + /// /// Property for getting . /// Page mainPage => - Application.Current.MainPage; - + Application.Current.MainPage; + /// /// Show error dialog. /// /// Dialog description. public void ShowError(string message) => - mainPage.DisplayAlert(_baseError, message, _baseOK); - + mainPage.DisplayAlert(_baseError, message, _baseOK); + /// /// Show message dialog. /// /// Dialog title. /// Dialog description. public void ShowMessage(string title, string message) => - mainPage.DisplayAlert(title, message, _baseOK); - + mainPage.DisplayAlert(title, message, _baseOK); + + + /// + /// Show message dialog. + /// + /// Dialog title. + /// Dialog description. + public Task ShowMessageUpdate(string title, string message, string linkButton, string cancelButton) => + mainPage.DisplayAlert(title, message, linkButton, cancelButton); + /// /// Show loading dialog. /// public void ShowLoading() => - UserDialogs.Instance.ShowLoading(_baseLoading); - + UserDialogs.Instance.ShowLoading(_baseLoading); + /// /// Show loading dialog. /// /// Dialog description. public void ShowLoading(string message) => - UserDialogs.Instance.ShowLoading(message); - + UserDialogs.Instance.ShowLoading(message); + /// /// Hide loading dialog. /// public void HideLoading() => - UserDialogs.Instance.HideLoading(); - + UserDialogs.Instance.HideLoading(); + /// /// Show progress dialog. /// @@ -91,8 +100,8 @@ public class AppDialogs : IDialogs /// Action on cancel. /// Progress dialog. public object ShowProgress(string message, string cancelText, Action onCancel) => - UserDialogs.Instance.Progress(message, onCancel, cancelText); - + UserDialogs.Instance.Progress(message, onCancel, cancelText); + /// /// Update progress dialog with percent. /// @@ -105,13 +114,14 @@ public void UpdateProgress(object dialog, int percent) { var progressDialog = getProgressDialog(dialog); - if (progressDialog == null) { + if (progressDialog == null) + { return; } progressDialog.PercentComplete = percent; - } - + } + /// /// Hide progress dialog. /// @@ -120,8 +130,8 @@ public void UpdateProgress(object dialog, int percent) /// (retrieved from ). /// public void HideProgress(object dialog) => - getProgressDialog(dialog)?.Hide(); - + getProgressDialog(dialog)?.Hide(); + /// /// Show alert sheet. /// @@ -133,14 +143,15 @@ public void ShowSheet(string title, Dictionary buttonList, ICommand { var config = new ActionSheetConfig().SetTitle(title); - foreach (var button in buttonList) { + foreach (var button in buttonList) + { config.Add(button.Value, () => command.Execute(button.Key)); } config.SetCancel(_baseCancel, () => command.Execute(-1)); UserDialogs.Instance.ActionSheet(config); - } - + } + /// /// Show confirmation dialog. /// @@ -166,7 +177,8 @@ public void ShowSheet(string title, Dictionary buttonList, ICommand /// Progress dialog. IProgressDialog getProgressDialog(object dialog) { - if (dialog == null || !(dialog is IProgressDialog)) { + if (dialog == null || !(dialog is IProgressDialog)) + { return null; } diff --git a/source/EduCATS/Helpers/Forms/Dialogs/Interfaces/IDialogs.cs b/source/EduCATS/Helpers/Forms/Dialogs/Interfaces/IDialogs.cs index 8a2f4eaf..b4cec42c 100644 --- a/source/EduCATS/Helpers/Forms/Dialogs/Interfaces/IDialogs.cs +++ b/source/EduCATS/Helpers/Forms/Dialogs/Interfaces/IDialogs.cs @@ -4,41 +4,48 @@ using System.Windows.Input; namespace EduCATS.Helpers.Forms.Dialogs -{ +{ /// /// App dialogs interface. /// public interface IDialogs - { + { /// /// Show message dialog. /// /// Dialog title. /// Dialog description. - void ShowMessage(string title, string message); - + void ShowMessage(string title, string message); + + /// + /// Show message dialog. + /// + /// Dialog title. + /// Dialog description. + Task ShowMessageUpdate(string title, string message, string linkButton, string cancelButton); + /// /// Show error dialog. /// /// Dialog description. - void ShowError(string message); - + void ShowError(string message); + /// /// Show loading dialog. /// - void ShowLoading(); - + void ShowLoading(); + /// /// Show loading dialog. /// /// Dialog description. - void ShowLoading(string message); - + void ShowLoading(string message); + /// /// Hide loading dialog. /// - void HideLoading(); - + void HideLoading(); + /// /// Show alert sheet. /// @@ -46,8 +53,8 @@ public interface IDialogs /// Dialog buttons (id and name). /// Command to execute on button click. /// Chosen button name. - void ShowSheet(string title, Dictionary buttons, ICommand command); - + void ShowSheet(string title, Dictionary buttons, ICommand command); + /// /// Show confirmation dialog. /// @@ -61,7 +68,7 @@ public interface IDialogs /// /// Dialog title. /// Dialog description. - Task ShowConfirmation(string title, string message); + Task ShowConfirmation(string title, string message); /// /// Show progress dialog. @@ -70,8 +77,8 @@ public interface IDialogs /// Cancel button text. /// Action on cancel. /// Progress dialog. - object ShowProgress(string message, string cancelText, Action onCancel); - + object ShowProgress(string message, string cancelText, Action onCancel); + /// /// Update progress dialog with percent. /// @@ -80,8 +87,8 @@ public interface IDialogs /// (retrieved from ). /// /// Percent to apply. - void UpdateProgress(object dialog, int percent); - + void UpdateProgress(object dialog, int percent); + /// /// Hide progress dialog. /// diff --git a/source/EduCATS/Networking/Servers.cs b/source/EduCATS/Networking/Servers.cs index d815d4ef..3abe6bc3 100644 --- a/source/EduCATS/Networking/Servers.cs +++ b/source/EduCATS/Networking/Servers.cs @@ -17,7 +17,7 @@ public static class Servers /// //public const string EduCatsAddress = @"https://host27072020.of.by"; public const string EduCatsAddress = @"https://educats.by"; - + /// /// Test server not host27072020 address. /// @@ -43,6 +43,16 @@ public static class Servers /// const string _educatsBntuString = "educats.bntu.by"; + /// + /// Platform services. + /// + public const string EducatsBntuAndroidMarketString = @"https://play.google.com/store/apps/details?id=by.bntu.educats"; + + /// + /// Platform services. + /// + public const string EducatsBntuIOSMarketString = @"https://apps.apple.com/by/app/educats/id1505738731"; + /// /// Platform services. /// @@ -50,7 +60,8 @@ public static class Servers static Servers() { - if (PlatformServices == null) { + if (PlatformServices == null) + { PlatformServices = new PlatformServices(); } } diff --git a/source/EduCATS/Pages/Today/Base/ViewModels/TodayPageViewModel.cs b/source/EduCATS/Pages/Today/Base/ViewModels/TodayPageViewModel.cs index b0bacf97..1b44c064 100644 --- a/source/EduCATS/Pages/Today/Base/ViewModels/TodayPageViewModel.cs +++ b/source/EduCATS/Pages/Today/Base/ViewModels/TodayPageViewModel.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Net.Http; +using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using EduCATS.Controls.RoundedListView; @@ -12,10 +14,13 @@ using EduCATS.Helpers.Date.Enums; using EduCATS.Helpers.Forms; using EduCATS.Helpers.Logs; +using EduCATS.Networking; using EduCATS.Pages.Today.Base.Models; using EduCATS.Pages.Today.Base.Views; using EduCATS.Themes; +using Newtonsoft.Json.Linq; using Nyxbull.Plugins.CrossLocalization; +using Xamarin.Essentials; using Xamarin.Forms; namespace EduCATS.Pages.Today.Base.ViewModels @@ -46,7 +51,8 @@ public TodayPageViewModel(double subjectHeight, double subjectsHeaderHeight, IPl _subjectsHeightToSubtract = services.Preferences.IsLargeFont ? 95 : 85; _subjectsFooterHeight = subjectsHeaderHeight; _isLargeFont = services.Preferences.IsLargeFont; - _services = services; + _services = services; + Version = _services.Device.GetVersion(); initSetup(); update(); @@ -131,6 +137,12 @@ public bool IsCalendarRefreshing } } } + string _version; + public string Version + { + get { return _version; } + set { SetProperty(ref _version, value); } + } Command _newsRefreshCommand; public Command NewsRefreshCommand { @@ -187,6 +199,7 @@ void update() IsNewsRefreshing = true; await getAndSetCalendarNotes(); await getAndSetNews(); + await getUpdateMessage(); //_isCreation = false; IsNewsRefreshing = false; } catch (Exception ex) { @@ -246,9 +259,73 @@ async Task getAndSetNews() NewsList = new List(news); } } + async Task getUpdateMessage() + { + string version = _version; + string storeUrl; + if (Device.RuntimePlatform == Device.Android) + { + version = await GetAndroidVersion(); + } + else if (Device.RuntimePlatform == Device.iOS) + { + version = await GetIOSVersion(); + if (version == null) + { + return; + } + } + if (version != _version) + { + string title = CrossLocalization.Translate("update_title"); + string message = CrossLocalization.Translate("update_message"); + string linkButton = CrossLocalization.Translate("update_link_button"); + string cancelButton = CrossLocalization.Translate("update_cancel_button"); - async Task> getNews() + var result = await _services.Dialogs.ShowMessageUpdate(title, message + version, linkButton, cancelButton); + if (result) + { + if (Device.RuntimePlatform == Device.Android) + await _services.Device.OpenUri(Servers.EducatsBntuAndroidMarketString); + else if (Device.RuntimePlatform == Device.iOS) + await Launcher.OpenAsync(new Uri(Servers.EducatsBntuIOSMarketString)); + } + } + } + + async Task GetAndroidVersion() + { + string storeUrl = "https://play.google.com/store/apps/details?id=by.bntu.educats"; + string html; + using (HttpClient client = new HttpClient()) + { + html = await client.GetStringAsync(storeUrl); + } + + MatchCollection matches = Regex.Matches(html, @"\[\[\[\""\d+\.\d+\.\d+"); + + return matches[0].Value.Substring(4); + } + async Task GetIOSVersion() { + using (var httpClient = new HttpClient()) + { + string iTunesUrlTemplate = "https://itunes.apple.com/lookup?bundleId=by.bntu.educats"; + string bundleId = "by.bntu.educats"; + var url = string.Format(iTunesUrlTemplate, bundleId); + var response = await httpClient.GetStringAsync(url); + var json = JObject.Parse(response); + + if (json["resultCount"].Value() == 0) + return null; + + var appInfo = json["results"].First; + + return appInfo["version"].Value(); + } + } + async Task> getNews() + { var news = await DataAccess.GetNews(_services.Preferences.UserLogin); if (DataAccess.IsError && !DataAccess.IsConnectionError) { From e62297c26091ab1484edab158dc2cfe576c2889c Mon Sep 17 00:00:00 2001 From: codefactor-io Date: Tue, 18 Jun 2024 11:52:45 +0000 Subject: [PATCH 2/2] [CodeFactor] Apply fixes --- source/EduCATS/Helpers/Forms/Dialogs/AppDialogs.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/EduCATS/Helpers/Forms/Dialogs/AppDialogs.cs b/source/EduCATS/Helpers/Forms/Dialogs/AppDialogs.cs index ce85d224..24c4c792 100644 --- a/source/EduCATS/Helpers/Forms/Dialogs/AppDialogs.cs +++ b/source/EduCATS/Helpers/Forms/Dialogs/AppDialogs.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Windows.Input; @@ -64,7 +64,6 @@ public class AppDialogs : IDialogs public void ShowMessage(string title, string message) => mainPage.DisplayAlert(title, message, _baseOK); - /// /// Show message dialog. /// @@ -166,7 +165,6 @@ public void ShowSheet(string title, Dictionary buttonList, ICommand /// /// Dialog title. /// Dialog description. - public async Task ShowConfirmation(string title, string message) => await mainPage.DisplayAlert(title, message, _baseOK);