Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #873 from keiji/make_check_version_service
Browse files Browse the repository at this point in the history
Make CheckVersion as a service.
  • Loading branch information
cocoa-dev003 authored Mar 3, 2022
2 parents f315a0a + 9ace3e1 commit eb43b40
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 50 deletions.
1 change: 1 addition & 0 deletions Covid19Radar/Covid19Radar/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ private static void RegisterCommonTypes(IContainer container)
container.Register<IDiagnosisKeyRepository, DiagnosisKeyRepository>(Reuse.Singleton);
container.Register<IExposureConfigurationRepository, ExposureConfigurationRepository>(Reuse.Singleton);
container.Register<IExposureRiskCalculationConfigurationRepository, ExposureRiskCalculationConfigurationRepository>(Reuse.Singleton);
container.Register<ICheckVersionService, CheckVersionService>(Reuse.Singleton);

#if EVENT_LOG_ENABLED
container.Register<IEventLogService, EventLogService>(Reuse.Singleton);
Expand Down
48 changes: 0 additions & 48 deletions Covid19Radar/Covid19Radar/Common/AppUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using Acr.UserDialogs;
using Covid19Radar.Resources;
using Covid19Radar.Services.Logs;
using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System.Net.Http;
using Xamarin.Essentials;
using Xamarin.Forms;

Expand Down Expand Up @@ -36,46 +29,5 @@ await Share.RequestAsync(new ShareTextRequest
}

}

public static async void CheckVersion(ILoggerService loggerService)
{
loggerService.StartMethod();

var uri = AppResources.UrlVersion;
using (var client = new HttpClient())
{
try
{
var json = await client.GetStringAsync(uri);
var key = Device.RuntimePlatform == Device.iOS ? "ios" : "android";
var versionString = JObject.Parse(json).Value<string>(key);

if (new Version(versionString).CompareTo(new Version(AppInfo.VersionString)) > 0)
{
await UserDialogs.Instance.AlertAsync(AppResources.AppUtilsGetNewVersionDescription, AppResources.AppUtilsGetNewVersionTitle, Resources.AppResources.ButtonOk);

if (Device.RuntimePlatform == Device.iOS)
{
await Browser.OpenAsync(AppSettings.Instance.AppStoreUrl, BrowserLaunchMode.External);
}
else if (Device.RuntimePlatform == Device.Android)
{
await Browser.OpenAsync(AppSettings.Instance.GooglePlayUrl, BrowserLaunchMode.External);
}

}

}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
loggerService.Exception("Failed to check version.", ex);
}
finally
{
loggerService.EndMethod();
}
}
}
}
}
68 changes: 68 additions & 0 deletions Covid19Radar/Covid19Radar/Services/CheckVersionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Covid19Radar.Resources;
using Covid19Radar.Services.Logs;
using Newtonsoft.Json.Linq;

namespace Covid19Radar.Services
{
public interface ICheckVersionService
{
public Task<bool> IsUpdateVersionExistAsync();
}

public class CheckVersionServiceNop : ICheckVersionService
{
public Task<bool> IsUpdateVersionExistAsync()
=> Task.FromResult(false);
}

public class CheckVersionService : ICheckVersionService
{
private readonly HttpClient _httpClient;
private readonly IEssentialsService _essentialsService;
private readonly ILoggerService _loggerService;

public CheckVersionService(
IHttpClientService httpClientService,
IEssentialsService essentialsService,
ILoggerService loggerService
)
{
_httpClient = httpClientService.Create();
_essentialsService = essentialsService;
_loggerService = loggerService;
}

public async Task<bool> IsUpdateVersionExistAsync()
{
_loggerService.StartMethod();

var uri = AppResources.UrlVersion;
try
{
var json = await _httpClient.GetStringAsync(uri);
var key = _essentialsService.IsIos ? "ios" : "android";
var versionString = JObject.Parse(json).Value<string>(key);

return new Version(versionString).CompareTo(new Version(_essentialsService.AppVersion)) > 0;
}
catch (Exception ex)
{
_loggerService.Exception("Failed to check version.", ex);
}
finally
{
_loggerService.EndMethod();
}

return false;
}

}
}
15 changes: 15 additions & 0 deletions Covid19Radar/Covid19Radar/Services/EssentialsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ public class EssentialsService : IEssentialsService
public string BuildNumber => AppInfo.BuildString;
public string AppPackageName => AppInfo.PackageName;

public string StoreUrl
{
get
{
if (IsIos)
{
return AppSettings.Instance.AppStoreUrl;
}
else
{
return AppSettings.Instance.GooglePlayUrl;
}
}
}

// PhoneDialer
public void PhoneDialerOpen(string number) => PhoneDialer.Open(number);
}
Expand Down
2 changes: 2 additions & 0 deletions Covid19Radar/Covid19Radar/Services/IEssentialsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public interface IEssentialsService
string BuildNumber { get; }
string AppPackageName { get; }

string StoreUrl { get; }

// PhoneDialer
void PhoneDialerOpen(string number);
}
Expand Down
24 changes: 22 additions & 2 deletions Covid19Radar/Covid19Radar/ViewModels/HomePage/HomePageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
using Covid19Radar.Views;
using Prism.Navigation;
using Xamarin.Forms;
using Covid19Radar.Model;
using Xamarin.Essentials;

namespace Covid19Radar.ViewModels
{
Expand All @@ -31,6 +31,8 @@ public class HomePageViewModel : ViewModelBase, IExposureNotificationEventCallba
private readonly AbsExposureNotificationApiService exposureNotificationApiService;
private readonly ILocalNotificationService localNotificationService;
private readonly AbsExposureDetectionBackgroundService exposureDetectionBackgroundService;
private readonly ICheckVersionService checkVersionService;
private readonly IEssentialsService essentialsService;
private readonly IDialogService dialogService;
private readonly IExternalNavigationService externalNavigationService;

Expand Down Expand Up @@ -90,6 +92,8 @@ public HomePageViewModel(
AbsExposureDetectionBackgroundService exposureDetectionBackgroundService,
IExposureConfigurationRepository exposureConfigurationRepository,
IExposureRiskCalculationConfigurationRepository exposureRiskCalculationConfigurationRepository,
ICheckVersionService checkVersionService,
IEssentialsService essentialsService,
IDialogService dialogService,
IExternalNavigationService externalNavigationService
) : base(navigationService)
Expand All @@ -105,6 +109,8 @@ IExternalNavigationService externalNavigationService
this.exposureDetectionBackgroundService = exposureDetectionBackgroundService;
this.exposureConfigurationRepository = exposureConfigurationRepository;
this.exposureRiskCalculationConfigurationRepository = exposureRiskCalculationConfigurationRepository;
this.checkVersionService = checkVersionService;
this.essentialsService = essentialsService;
this.dialogService = dialogService;
this.externalNavigationService = externalNavigationService;
}
Expand All @@ -124,7 +130,21 @@ public override async void Initialize(INavigationParameters parameters)
});

// Check Version
AppUtils.CheckVersion(loggerService);
_ = Task.Run(async () => {
bool isUpdated = await checkVersionService.IsUpdateVersionExistAsync();
if (isUpdated)
{
MainThread.BeginInvokeOnMainThread(async () =>
{
await UserDialogs.Instance.AlertAsync(
AppResources.AppUtilsGetNewVersionDescription,
AppResources.AppUtilsGetNewVersionTitle,
AppResources.ButtonOk
);
await Browser.OpenAsync(essentialsService.StoreUrl, BrowserLaunchMode.External);
});
}
});

// Load necessary files asynchronous
_ = exposureConfigurationRepository.GetExposureConfigurationAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<None Remove="Files\exposure_risk_calculation_configuration_v1_1.json" />
<None Remove="Files\exposure_risk_configuration1.json" />
<None Remove="Files\exposure_risk_configuration2.json" />
<None Remove="Files\check_version1.json" />
</ItemGroup>
<ItemGroup>
<Content Include="Files\exposure_configuration1.json">
Expand Down Expand Up @@ -86,4 +87,9 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Files\check_version1.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": "2.0.1",
"android": "2.0.2",
"ios": "2.0.3"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using Covid19Radar.Services;
using Covid19Radar.Services.Logs;
using Moq;
using Xunit;

namespace Covid19Radar.UnitTests.Services
{
public class CheckVersionServiceTests
{
private const string JSON_VERSION1 = "check_version1.json";

#region Instance Properties

private readonly MockRepository _mockRepository;
private readonly Mock<ILoggerService> _mockLoggerService;
private readonly Mock<IHttpClientService> _mockClientService;
private readonly Mock<IEssentialsService> _mockEssentialsService;

#endregion

#region Constructors

public CheckVersionServiceTests()
{
_mockRepository = new MockRepository(MockBehavior.Default);
_mockLoggerService = _mockRepository.Create<ILoggerService>();
_mockClientService = _mockRepository.Create<IHttpClientService>();
_mockEssentialsService = _mockRepository.Create<IEssentialsService>();
}

#endregion

#region Other Private Methods

private CheckVersionService CreateService()
{
return new CheckVersionService(
_mockClientService.Object,
_mockEssentialsService.Object,
_mockLoggerService.Object
);
}

private static string GetTestJson(string fileName)
{
var path = TestDataUtils.GetLocalFilePath(fileName);
using (var reader = File.OpenText(path))
{
return reader.ReadToEnd();
}
}

#endregion

[Theory]
[InlineData(false, "2.0.1", true)]
[InlineData(false, "2.0.2", false)]
[InlineData(true, "2.0.1", true)]
[InlineData(true, "2.0.2", true)]
[InlineData(true, "2.0.3", false)]
private async void IsUpdate(bool isIos, string version, bool expected)
{
string content = GetTestJson(JSON_VERSION1);

var jsonContent = new StringContent(
content,
Encoding.UTF8,
"application/json"
);
var client = HttpClientUtils.CreateHttpClient(HttpStatusCode.OK, jsonContent);
_mockClientService.Setup(x => x.Create()).Returns(client);

_mockEssentialsService.Setup(x => x.IsIos).Returns(isIos);
_mockEssentialsService.Setup(x => x.AppVersion).Returns(version);

ICheckVersionService service = CreateService();

bool isUpdated = await service.IsUpdateVersionExistAsync();

Assert.Equal(expected, isUpdated);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class HomePageViewModelTests: IDisposable
private readonly IUserDataRepository userDataRepository;
private readonly IExposureDataRepository exposureDataRepository;
private readonly Mock<IExposureRiskCalculationService> mockExposureRiskCalculationService;
private readonly Mock<ICheckVersionService> mockCheckVersionService;
private readonly Mock<IEssentialsService> mockEssentialsService;
private readonly Mock<AbsExposureDetectionBackgroundService> mockExposureDetectionBackgroundService;
private readonly Mock<IDialogService> mockDialogService;
private readonly Mock<IExternalNavigationService> mockExternalNavigationService;
Expand All @@ -62,6 +64,8 @@ public HomePageViewModelTests()
mockLocalPathService = mockRepository.Create<ILocalPathService>();
mockDialogService = mockRepository.Create<IDialogService>();
mockExposureRiskCalculationService = mockRepository.Create<IExposureRiskCalculationService>();
mockCheckVersionService = mockRepository.Create<ICheckVersionService>();
mockEssentialsService = mockRepository.Create<IEssentialsService>();
mockExternalNavigationService = mockRepository.Create<IExternalNavigationService>();

userDataRepository = new UserDataRepository(
Expand Down Expand Up @@ -108,6 +112,8 @@ private HomePageViewModel CreateViewModel()
mockExposureDetectionBackgroundService.Object,
mockExposureConfigurationRepository.Object,
mockExposureRiskCalculationConfigurationRepository.Object,
mockCheckVersionService.Object,
mockEssentialsService.Object,
mockDialogService.Object,
mockExternalNavigationService.Object
);
Expand Down

0 comments on commit eb43b40

Please sign in to comment.