diff --git a/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs b/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs index 509248a38..77aec6faa 100644 --- a/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs +++ b/Covid19Radar/Covid19Radar/Repository/EventLogRepository.cs @@ -26,9 +26,13 @@ public Task> GetLogsAsync( public Task RemoveAsync(EventLog eventLog); + public Task RemoveAllAsync(); + public Task AddEventNotifiedAsync( long maxSize = AppConstants.EventLogMaxRequestSizeInBytes ); + + public Task IsExist(); } public class EventLogRepository : IEventLogRepository @@ -269,6 +273,82 @@ private async Task AddEventNotifiedAsyncInternal(long maxSize) }; await AddAsyncInternal(eventLog, maxSize); } + + public async Task RemoveAllAsync() + { + _loggerService.StartMethod(); + + await _semaphore.WaitAsync(); + + try + { + RemoveAllAsyncInternal(); + } + finally + { + _semaphore.Release(); + + _loggerService.EndMethod(); + } + } + + private void RemoveAllAsyncInternal() + { + _loggerService.StartMethod(); + try + { + if (Directory.Exists(_basePath)) + { + Directory.Delete(_basePath, true); + } + } + finally + { + _loggerService.EndMethod(); + } + } + + public async Task IsExist() + { + _loggerService.StartMethod(); + + await _semaphore.WaitAsync(); + + try + { + return IsExistInternal(); + } + finally + { + _semaphore.Release(); + + _loggerService.EndMethod(); + } + } + + private bool IsExistInternal() + { + _loggerService.StartMethod(); + try + { + if (!Directory.Exists(_basePath)) + { + return false; + } + + string[] filesInDirectory = Directory.GetFiles(_basePath); + if (filesInDirectory.Length == 0) + { + return false; + } + + return true; + } + finally + { + _loggerService.EndMethod(); + } + } } public class EventContentExposureNotified diff --git a/Covid19Radar/Covid19Radar/Repository/SendEventLogStateRepository.cs b/Covid19Radar/Covid19Radar/Repository/SendEventLogStateRepository.cs index e4d200848..247193d3e 100644 --- a/Covid19Radar/Covid19Radar/Repository/SendEventLogStateRepository.cs +++ b/Covid19Radar/Covid19Radar/Repository/SendEventLogStateRepository.cs @@ -49,6 +49,8 @@ public interface ISendEventLogStateRepository SendEventLogState GetSendEventLogState(EventType eventType); bool IsExistNotSetEventType(); + + void RemoveAll(); } public class SendEventLogStateRepository : ISendEventLogStateRepository @@ -156,5 +158,10 @@ public bool IsExistNotSetEventType() .Select(eventType => GetSendEventLogState(eventType)) .Any(state => state == SendEventLogState.NotSet); } + + public void RemoveAll() + { + _preferencesService.RemoveValue(PreferenceKey.SendEventLogState); + } } } diff --git a/Covid19Radar/Covid19Radar/ViewModels/Settings/DebugPageViewModel.cs b/Covid19Radar/Covid19Radar/ViewModels/Settings/DebugPageViewModel.cs index 1f5923086..1c0b85529 100644 --- a/Covid19Radar/Covid19Radar/ViewModels/Settings/DebugPageViewModel.cs +++ b/Covid19Radar/Covid19Radar/ViewModels/Settings/DebugPageViewModel.cs @@ -18,6 +18,7 @@ using Covid19Radar.Services; using Covid19Radar.Views; using Prism.Navigation; +using Xamarin.CommunityToolkit.ObjectModel; using Xamarin.Essentials; using Xamarin.Forms; @@ -34,6 +35,8 @@ public class DebugPageViewModel : ViewModelBase private readonly ICloseApplicationService _closeApplicationService; private readonly IServerConfigurationRepository _serverConfigurationRepository; private readonly ILocalNotificationService _localNotificationService; + private readonly ISendEventLogStateRepository _sendEventLogStateRepository; + private readonly IEventLogRepository _eventLogRepository; private string _debugInfo; public string DebugInfo @@ -154,7 +157,9 @@ public DebugPageViewModel( AbsExposureDetectionBackgroundService exposureDetectionBackgroundService, ICloseApplicationService closeApplicationService, IServerConfigurationRepository serverConfigurationRepository, - ILocalNotificationService localNotificationService + ILocalNotificationService localNotificationService, + ISendEventLogStateRepository sendEventLogStateRepository, + IEventLogRepository eventLogRepository ) : base(navigationService) { Title = "Title:Debug"; @@ -167,6 +172,8 @@ ILocalNotificationService localNotificationService _closeApplicationService = closeApplicationService; _serverConfigurationRepository = serverConfigurationRepository; _localNotificationService = localNotificationService; + _sendEventLogStateRepository = sendEventLogStateRepository; + _eventLogRepository = eventLogRepository; } public override async void Initialize(INavigationParameters parameters) @@ -309,6 +316,19 @@ private string ConvertSha256(string text) _ = await NavigationService.NavigateAsync("/" + nameof(ReAgreePrivacyPolicyPage), navigationParams); }); + public IAsyncCommand OnClickAddEventNotifiedIfNeeded => new AsyncCommand(async () => + { + if (_sendEventLogStateRepository.GetSendEventLogState(EventType.ExposureNotified) == SendEventLogState.Enable) + { + await _eventLogRepository.AddEventNotifiedAsync(); + } + }); + + public IAsyncCommand OnClickAddEventNotifiedForce => new AsyncCommand(async () => + { + await _eventLogRepository.AddEventNotifiedAsync(); + }); + public Command OnClickQuit => new Command(() => { Application.Current.Quit(); diff --git a/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs b/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs index f4d4c3590..c6ba95179 100644 --- a/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs +++ b/Covid19Radar/Covid19Radar/ViewModels/Settings/SettingsPageViewModel.cs @@ -32,6 +32,8 @@ public string AppVer private readonly IUserDataRepository userDataRepository; private readonly IExposureDataRepository exposureDataRepository; private readonly IExposureConfigurationRepository exposureConfigurationRepository; + private readonly ISendEventLogStateRepository _sendEventLogStateRepository; + private readonly IEventLogRepository _eventLogRepository; private readonly ILogFileService logFileService; private readonly AbsExposureNotificationApiService exposureNotificationApiService; private readonly ICloseApplicationService closeApplicationService; @@ -46,6 +48,8 @@ public SettingsPageViewModel( IUserDataRepository userDataRepository, IExposureDataRepository exposureDataRepository, IExposureConfigurationRepository exposureConfigurationRepository, + ISendEventLogStateRepository sendEventLogStateRepository, + IEventLogRepository eventLogRepository, ILogFileService logFileService, AbsExposureNotificationApiService exposureNotificationApiService, ICloseApplicationService closeApplicationService, @@ -58,11 +62,29 @@ IEssentialsService essentialsService this.userDataRepository = userDataRepository; this.exposureDataRepository = exposureDataRepository; this.exposureConfigurationRepository = exposureConfigurationRepository; + _sendEventLogStateRepository = sendEventLogStateRepository; + _eventLogRepository = eventLogRepository; this.logFileService = logFileService; this.exposureNotificationApiService = exposureNotificationApiService; this.closeApplicationService = closeApplicationService; } + public override async void Initialize(INavigationParameters parameters) + { + loggerService.StartMethod(); + base.Initialize(parameters); + + try + { + bool isExistEventLogs = await _eventLogRepository.IsExist(); + loggerService.Info($"isExistEventLogs: {isExistEventLogs}"); + } + finally + { + loggerService.EndMethod(); + } + } + public IAsyncCommand OnEventLogSend => new AsyncCommand(async () => { loggerService.StartMethod(); @@ -73,7 +95,7 @@ IEssentialsService essentialsService loggerService.EndMethod(); }); - public ICommand OnChangeResetData => new Command(async () => + public IAsyncCommand OnChangeResetData => new AsyncCommand(async () => { loggerService.StartMethod(); @@ -100,6 +122,9 @@ IEssentialsService essentialsService userDataRepository.RemoveAllUpdateDate(); userDataRepository.RemoveAllExposureNotificationStatus(); + _sendEventLogStateRepository.RemoveAll(); + await _eventLogRepository.RemoveAllAsync(); + _ = logFileService.DeleteLogsDir(); UserDialogs.Instance.HideLoading(); diff --git a/Covid19Radar/Covid19Radar/Views/Settings/DebugPage.xaml b/Covid19Radar/Covid19Radar/Views/Settings/DebugPage.xaml index 11b383c48..a85b7bfd5 100644 --- a/Covid19Radar/Covid19Radar/Views/Settings/DebugPage.xaml +++ b/Covid19Radar/Covid19Radar/Views/Settings/DebugPage.xaml @@ -1,4 +1,4 @@ - + @@ -108,6 +108,14 @@ Command="{Binding Path=OnClickRemoveAllUpdateDate}" Style="{StaticResource DefaultButton}" Text="RemoveAllUpdateDate" /> +