Commit 5eb7e1d7 authored by Francesco Carraro's avatar Francesco Carraro
Browse files

fixing save/delete behavior for usercontrols

parent 33926168
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ using INAF.Apps.Uwp.SLabDataManager.Models.Fit;
using INAF.Apps.Uwp.SLabDataManager.Models.Spectrum;
using INAF.Apps.Uwp.SLabDataManager.Services;
using INAF.Apps.Uwp.SLabDataManager.ViewModels;
using INAF.Apps.Uwp.SLabDataManager.ViewModels.ContentDialogsViewModel;
using INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels;
using INAF.Libraries.NetStandard.Math.Fit.Linear;
using INAF.Libraries.NetStandard.Math.Fit.Spline;
@@ -145,11 +146,13 @@ namespace INAF.Apps.Uwp.SLabDataManager
                .AddSingleton<WorkingFoldersContainer>()
                .AddSingleton<WorkingItemsModel>()
                /* scoped */
                .AddScoped<ActionQuestionViewModel>()
                .AddScoped<AlignmentViewModel>()
                .AddScoped<MeasurementInfoViewModel>()
                .AddScoped<SampleDataViewModel>()
                .AddScoped<AlignmentViewModel>()
                .AddScoped<SegmentsFitViewModel>()
                .AddScoped<SmoothingViewModel>()
                .AddScoped<StorageItemsHelper>()
                /* transient */
                .AddTransient<AnimationsHelper>()
                .AddTransient<AuthenticationManager>()
+1 −1
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
		<operation type="IsTokenValid">Account/IsTokenValid</operation>
		<operation type="IsLoginValid">Account/IsLoginValid</operation>
		<!-- SPECTRA FILES ACTIONS -->
		<operation type="DeleteSpectrum">AppSpectraFilesActions/DeleteSpectrum</operation>
		<operation type="SaveSpectrumOfTypeAligned">AppSpectraFilesActions/SaveSpectrumOfTypeAligned</operation>
		<operation type="SaveSpectrumOfTypeContinuumRemoved">AppSpectraFilesActions/SaveSpectrumOfTypeContinuumRemoved</operation>
		<operation type="SaveAsCopySpectrumOfTypeContinuumRemoved">AppSpectraFilesActions/SaveAsCopySpectrumOfTypeContinuumRemoved</operation>
@@ -17,7 +18,6 @@
		<operation type="IsAnySpectrumTypeSavedOnCloud">AppSpectraFilesActions/IsAnySpectrumTypeSavedOnCloud</operation>
		<operation type="IsSpectrumTypeSavedOnCloud">AppSpectraFilesActions/IsSpectrumTypeSavedOnCloud</operation>
		<operation type="GetSampleDataValues">AppSpectraFilesActions/GetSampleDataValues</operation>
		<operation type="DeleteSpectrum">AppSpectraFilesActions/DeleteSpectrum</operation>
		<!-- USERS -->
		<operation type="GetUserPermissions">Users/GetUserPermissions</operation>
	</operationUrls>
+98 −0
Original line number Diff line number Diff line
using INAF.Libraries.Uwp.Logging;
using INAF.Libraries.Uwp.StorageItemsAccess;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using static INAF.Libraries.NetStandard.SLabCommonModels.Enums.Enums;

namespace INAF.Apps.Uwp.SLabDataManager.Helpers
{
    public sealed class StorageItemsHelper
    {
        private readonly StorageItemsAccessHelper storageItemsAccessHelper;
        private readonly Logger logger;

        public StorageItemsHelper(StorageItemsAccessHelper storageItemsAccessHelper,
                                  Logger logger)
        {
            this.storageItemsAccessHelper = storageItemsAccessHelper;
            this.logger = logger;
        }

        public async Task<(bool isOk, string exMsg)> deleteFileAsync(string filepath,
                                                                     string folderTokenName,
                                                                     SpectrumType spectrumType)
        {
            (bool isOk, string exMsg) result = (true, string.Empty);

            try
            {
                var folder = (StorageFolder)await storageItemsAccessHelper.getAsync(folderTokenName);
                if (folder == null)
                    return (false, "DeleteProcedureFolderNotDefinedMessage".GetText());

                string filename = createFilenameForLocalSaving(filepath,
                                                               spectrumType);

                var item = await folder.GetItemAsync(filename);
                if (item == null)
                    return (false, "DeleteProcedureFileNotAvailableMessage".GetText());

                await item.DeleteAsync(StorageDeleteOption.Default);
            }
            catch (Exception ex)
            {
                result.isOk = false;
                logger.Write<StorageItemsHelper>($"{nameof(deleteFileAsync)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error);
            }

            return result;
        }

        public async Task<bool> isLocalFileAvailableAsync(string filepath,
                                                          SpectrumType spectrumType)
        {
            bool isAvailable = false;

            if (string.IsNullOrEmpty(filepath))
                throw new ArgumentNullException("'filepath' argument cannot be null!");

            var folder = (StorageFolder)await storageItemsAccessHelper.getAsync(Constants.Constants.OUTPUT_SPECTRA_FOLDER_TOKEN);
            if (folder == null)
                return false;

            string filename = createFilenameForLocalSaving(filepath,
                                                           spectrumType);

            if (folder.GetItemAsync(filename) != null)
                isAvailable = false;

            return isAvailable;
        }

        public string createFilenameForLocalSaving(string filepath,
                                                   SpectrumType spectrumType)
        {
            FileInfo fi = new FileInfo(filepath);

            string datetime = DateTime.UtcNow.ToString("yyyyMMddHHmmss");

            /* create the new filename */
            List<string> temp = fi.Name.Split('.').ToList();

            var filenameNoExt = temp[0];

            /* remove 1st elem to preserve only composite extension */
            temp.RemoveAt(0);
            string ext = string.Join(".", temp.ToArray());

            /* create new filename */
            string filename = string.Join(string.Empty, filenameNoExt, "_", spectrumType, "_", datetime, ".", ext);

            return filename;
        }
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -176,6 +176,7 @@
    <Compile Include="Helpers\PermissionsHelper.cs" />
    <Compile Include="Helpers\RemoteOperations\RemoteOperationsManagerSpectraFileActions.cs" />
    <Compile Include="Helpers\RemoteOperations\RemoteOperationsManagerUsers.cs" />
    <Compile Include="Helpers\StorageItemsHelper.cs" />
    <Compile Include="Helpers\UI\AnimationsHelper.cs" />
    <Compile Include="Helpers\UI\Chart\ContinuumMissingPointsHelper.cs" />
    <Compile Include="Helpers\UI\Chart\ContinuumRemovalHelper.cs" />
@@ -215,6 +216,7 @@
    <Compile Include="ViewModels\ChartViewModelInfo.cs" />
    <Compile Include="ViewModels\ChartViewModelSmoothing.cs" />
    <Compile Include="ViewModels\ChartViewModelZoom.cs" />
    <Compile Include="ViewModels\ContentDialogsViewModel\ActionQuestionViewModel.cs" />
    <Compile Include="ViewModels\UserControlViewModels\AlignmentViewModel.cs" />
    <Compile Include="Helpers\VisualTreeHelpers.cs" />
    <Compile Include="Helpers\XmlReaders\FitFunctionsReader.cs" />
+33 −9
Original line number Diff line number Diff line
@@ -278,9 +278,6 @@
    <value>SLab Data Manager</value>
    <comment>Application display name</comment>
  </data>
  <data name="RawSpectrumAlignmentSaveAlignedFileOnCloudErrorMessage" xml:space="preserve">
    <value>An error occurred while trying to save aligned file on cloud. If error persists, try to contact the system admin.</value>
  </data>
  <data name="RawSpectrumFileReadingSuccessMessage" xml:space="preserve">
    <value>File has been successfully saved!</value>
  </data>
@@ -296,9 +293,6 @@
  <data name="RawSpectrumAlignmentNoRefSegmentSelectedErrorMessage" xml:space="preserve">
    <value>No ref segment selected for alignment. Selection of a segment from alignment options is required</value>
  </data>
  <data name="RawSpectrumAlignmentSaveResultLabel.Text" xml:space="preserve">
    <value>Save aligned spectrum</value>
  </data>
  <data name="RawSpectrumAlignmentAlreadyExecutedErrorMessage" xml:space="preserve">
    <value>RAW spectrum has been already aligned!</value>
  </data>
@@ -317,9 +311,6 @@
  <data name="RawSpectrumAlignmentRemoveLabel.Text" xml:space="preserve">
    <value>Remove aligned spectrum</value>
  </data>
  <data name="RawSpectrumAlignmentSaveAlignedNoAlignedErrorMessage" xml:space="preserve">
    <value>No aligned spectrum created yet</value>
  </data>
  <data name="RefSpectrumSummaryLabel" xml:space="preserve">
    <value>REF Spectrum summary</value>
  </data>
@@ -783,4 +774,37 @@
  <data name="AlignmentAskDeletingContinuumRemoved" xml:space="preserve">
    <value>Do you really want to delete aligned spectrum?</value>
  </data>
  <data name="DeleteProcedureFileLocallyDeletedSuccessfullyMessage" xml:space="preserve">
    <value>Local file successfully deleted</value>
  </data>
  <data name="DeleteProcedureFileNotAvailableMessage" xml:space="preserve">
    <value>No file to be deleted found</value>
  </data>
  <data name="DeleteProcedureFileOnCloudDeletedSuccessfullyMessage" xml:space="preserve">
    <value>On cloud file successfully deleted</value>
  </data>
  <data name="DeleteProcedureFileOnCloudErrorMessage" xml:space="preserve">
    <value>An error occurred while trying to delete on cloud file. If error persists, try to contact the system admin.</value>
  </data>
  <data name="DeleteProcedureFolderNotDefinedMessage" xml:space="preserve">
    <value>Container folder is not defined</value>
  </data>
  <data name="DeleteProcedureTitle" xml:space="preserve">
    <value>File deletion</value>
  </data>
  <data name="RawSpectrumAlignmentSaveResultLabel.Text" xml:space="preserve">
    <value>Save aligned spectrum</value>
  </data>
  <data name="SaveSpectrumLocalOutputFolderNotDefinedMessage" xml:space="preserve">
    <value>Couldn't save spectrum locally: output folder is not defined. To save locally a file, use settings page in order to select the output folder</value>
  </data>
  <data name="SaveSpectrumNoCurrentTypeErrorMessage" xml:space="preserve">
    <value>No current type of spectrum created yet</value>
  </data>
  <data name="SaveSpectrumOnCloudErrorMessage" xml:space="preserve">
    <value>An error occurred while trying to save file on cloud. If error persists, try to contact the system admin.</value>
  </data>
  <data name="SaveSpectrumResultLabel.Text" xml:space="preserve">
    <value>Save spectrum</value>
  </data>
</root>
 No newline at end of file
Loading