Commit 6a02344c authored by Francesco Carraro's avatar Francesco Carraro
Browse files

fixed new reading of raw spectrum; fixed deleting of derived spectra when new...

fixed new reading of raw spectrum; fixed deleting of derived spectra when new raw is read; fixed procedures for removal of derived spectra; fixed update of UI when spectra are added/removed
parent a71fecd9
Loading
Loading
Loading
Loading
+33 −19
Original line number Diff line number Diff line
@@ -32,6 +32,9 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers
            if (isAnySummaryOfType(spectrum.Type))
                tryGetSummaryOfType(spectrum.Type).init(spectrum);

            /* set color for spectrum */
            spectrum.setColor();

            if (Spectra.Any(x => x.Type == spectrum.Type))
            {
                /* if a spectrum of same type is already present, then replace it... */
@@ -82,9 +85,9 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers
        }

        /* wrapper of 'tryRemoveSpectrumOfTypeAsync' method for aligned spectrum */
        public void removeAlignedSpectrum()
        public async Task<bool> removeAlignedSpectrumAsync()
        {
            tryRemoveSpectrumOfTypeAsync(SpectrumType.Aligned);
            return await tryRemoveSpectrumOfTypeAsync(SpectrumType.Aligned);
        }

        public IEnumerable<SpectrumModel> tryGetSpectraOfTypeOtherThan(IEnumerable<SpectrumType> spectrumTypes)
@@ -97,7 +100,11 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers
            return Spectra.FirstOrDefault(x => x.Type == spectrumType);
        }

        public async Task tryRemoveSpectrumOfTypeAsync(SpectrumType spectrumType)
        public async Task<bool> tryRemoveSpectrumOfTypeAsync(SpectrumType spectrumType)
        {
            bool isOk = true;

            try
            {
                var requestedSpectrum = tryGetSpectrumOfType(spectrumType);
                if (requestedSpectrum != null)
@@ -123,6 +130,14 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers
                SelectedSpectrumType = spectrumType;
                raiseIsAnySpectrumRemoved();
            }
            catch (Exception ex)
            {
                isOk = false;
                logger.Write<SpectraContainer>($"{nameof(tryRemoveSpectrumOfTypeAsync)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error);
            }

            return isOk;
        }
        #endregion

        public void clear()
@@ -151,7 +166,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Charts.Containers

        protected void addSpectrum(SpectrumModel spectrum)
        {
            spectrum.setColor();
            Spectra.Add(spectrum);

            /* enable navigation to chart page */
+6 −0
Original line number Diff line number Diff line
@@ -591,4 +591,10 @@
  <data name="SampleDataSuccessfullySavedMessage" xml:space="preserve">
    <value>Sample data successfully saved!</value>
  </data>
  <data name="ChartPointCoordinatesErrorMessage" xml:space="preserve">
    <value>An error occurred while trying to retrieve coordinates on chart from tapped point.</value>
  </data>
  <data name="TryRemoveSpectrumFailedMessage" xml:space="preserve">
    <value>An error occurred while trying to remove selected spectrum</value>
  </data>
</root>
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -221,7 +221,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
            isSaveFileSuccess = false;
        }

        protected void showDialogMessage(string dialogText, DialogMessageType dialogMessageType = DialogMessageType.Error)
        public void showDialogMessage(string dialogText, DialogMessageType dialogMessageType = DialogMessageType.Error)
        {
            page.FindName("operationResultErrorDialog");
            DialogMessage = dialogText;
+155 −96
Original line number Diff line number Diff line
using INAF.Apps.Uwp.SLabDataManager.Charts;
using INAF.Apps.Uwp.SLabDataManager.Converters;
using INAF.Apps.Uwp.SLabDataManager.Extensions;
using INAF.Apps.Uwp.SLabDataManager.Helpers;
using INAF.Apps.Uwp.SLabDataManager.Helpers.RemoteOperations;
@@ -141,6 +142,13 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
        #endregion

        #region properties
        private bool areButtonsForAlignedSpectrumEnabled;
        public bool AreButtonsForAlignedSpectrumEnabled
        {
            get { return areButtonsForAlignedSpectrumEnabled; }
            set { SetProperty(ref areButtonsForAlignedSpectrumEnabled, value); }
        }

        private double chartLegendHeight;
        public double ChartLegendHeight
        {
@@ -300,6 +308,50 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
        #endregion
        #endregion

        #region raise methods
        public void raiseHideAlignmentFlyoutRequired()
        {
            IsHideAlignmentFlyoutRequired = true;
            isHideAlignmentFlyoutRequired = false;
        }

        public void raiseHideCalculationsFlyoutRequired()
        {
            IsHideCalculationsFlyoutRequired = true;
            isHideCalculationsFlyoutRequired = false;
        }

        public void raiseIsChartLayoutUpdateRequired()
        {
            IsChartLayoutUpdateRequired = true;
            isChartLayoutUpdateRequired = false;
        }

        public void raiseIsDisableFitFocusRequired()
        {
            IsDisableFitFocusRequired = true;
            isDisableFitFocusRequired = false;
        }

        private void raiseIsSegmentsFitsUserControlShowRequired()
        {
            IsSegmentsFitsUserControlShowRequired = true;
            //isSegmentsFitsUserControlShowRequired = false; // DO NOT UNCOMMENT THIS!
        }

        private void raiseIsSegmentsHightlightRequired()
        {
            IsSegmentHighlightRequired = true;
            isSegmentHighlightRequired = false;
        }

        private void raiseIsSegmentsLowlightRequired()
        {
            IsSegmentLowlightRequired = true;
            isSegmentLowlightRequired = false;
        }
        #endregion

        #region methods
        private void clear()
        {
@@ -320,6 +372,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels

            SampleDataPanelTranslation = 540;

            AreButtonsForAlignedSpectrumEnabled = false;
            IsSampleDataPanelVisible = false;
            IsDisableFitFocusRequired = false;
            IsFocusRequired = false;
@@ -417,6 +470,40 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
        #endregion

        #region aligned spectrum
        private async Task deleteAlignedSpectrumAsync(bool showErrorMessages = true)
        {
            raiseHideAlignmentFlyoutRequired();

            if (!WorkingItems.SpectraContainer.isAnySpectrumOfType(SpectrumType.Aligned) && showErrorMessages)
            {
                showDialogMessage("RawSpectrumAlignmentNoAlignedToBeRemovedErrorMessage".GetText());
                return;
            }

            if (!await WorkingItems.SpectraContainer.removeAlignedSpectrumAsync() && showErrorMessages)
            {
                showDialogMessage("TryRemoveSpectrumFailedMessage".GetText());
                return;
            }

            /* disable buttons for saving/deleting aligned spectrum + disable toggleSwitch for adding points for fitting continuum */
            AreButtonsForAlignedSpectrumEnabled = false;

            /* now let's remove the continuum-removed spectrum (if available), since it is based on aligned one */
            await deleteContinuumRemovedSpectrumAsync(showErrorMessages,
                                                      deleteOnCloudIfAvailable: false,
                                                      removeFitSettings: true);
        }

        public async Task deleteDerivedSpectraAsync()
        {
            await deleteAlignedSpectrumAsync(showErrorMessages: false);

            await deleteContinuumRemovedSpectrumAsync(showErrorMessages: false,
                                                      deleteOnCloudIfAvailable: false,
                                                      removeFitSettings: true);
        }

        private async Task executeSpectrumAlignmentAsync()
        {
            raiseHideAlignmentFlyoutRequired();
@@ -442,26 +529,14 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
            IsLoading = false;

            if (string.IsNullOrEmpty(result.DialogMessage))
            {
                await WorkingItems.SpectraContainer.addOrUpdateSpectrumAsync(result.Spectrum);
                AreButtonsForAlignedSpectrumEnabled = true;
            }
            else
                showDialogMessage(result.DialogMessage, result.DialogMessageType);
        }

        private void removeAlignedSpectrum()
        {
            raiseHideAlignmentFlyoutRequired();

            var alignedSpectrum = WorkingItems.SpectraContainer.tryGetSpectrumOfType(SpectrumType.Aligned);

            if (alignedSpectrum == null)
            {
                showDialogMessage("RawSpectrumAlignmentNoAlignedToBeRemovedErrorMessage".GetText());
                return;
            }

            WorkingItems.SpectraContainer.removeAlignedSpectrum();
        }

        private async Task saveAlignmentProcedureAsync()
        {
            raiseHideAlignmentFlyoutRequired();
@@ -609,74 +684,53 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
        }
        #endregion

        #region raise methods
        public void raiseHideAlignmentFlyoutRequired()
        {
            IsHideAlignmentFlyoutRequired = true;
            isHideAlignmentFlyoutRequired = false;
        }

        public void raiseHideCalculationsFlyoutRequired()
        #region continuum
        private void askDeleteContinuumSpectrum()
        {
            IsHideCalculationsFlyoutRequired = true;
            isHideCalculationsFlyoutRequired = false;
            if (WorkingItems.SpectraContainer.isAnySpectrumOfType(SpectrumType.ContinuumRemoved))
                IsDeleteQuestionVisible = true;
        }

        public void raiseIsChartLayoutUpdateRequired()
        private async Task deleteContinuumRemovedSpectrumAsync(bool showErrorMessages = true,
                                                               bool deleteOnCloudIfAvailable = true,
                                                               bool removeFitSettings = false)
        {
            IsChartLayoutUpdateRequired = true;
            isChartLayoutUpdateRequired = false;
        }
            if (!WorkingItems.SpectraContainer.isAnySpectrumOfType(SpectrumType.ContinuumRemoved))
                return;

        public void raiseIsDisableFitFocusRequired()
        {
            IsDisableFitFocusRequired = true;
            isDisableFitFocusRequired = false;
        }
            /* retrieve continuum-removed spectrum id */
            var continuumRemovedSpectrum = WorkingItems.SpectraContainer.tryGetSpectrumOfType(SpectrumType.ContinuumRemoved);
            bool isSavedOnCloud = continuumRemovedSpectrum.IsSavedOnCloud;
            int id = continuumRemovedSpectrum.Id;

        private void raiseIsSegmentsFitsUserControlShowRequired()
        {
            IsSegmentsFitsUserControlShowRequired = true;
            //isSegmentsFitsUserControlShowRequired = false; // DO NOT UNCOMMENT THIS!
        }
            /* remove spectrum from chart */
            bool isDeleted = await WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(SpectrumType.ContinuumRemoved);

        private void raiseIsSegmentsHightlightRequired()
            /* if continuum-removed spectrum has been saved on cloud, then remove it */
            if (isSavedOnCloud && deleteOnCloudIfAvailable)
            {
            IsSegmentHighlightRequired = true;
            isSegmentHighlightRequired = false;
                var remoteOperationsManager = serviceProvider.GetRequiredService<RemoteOperationsManager>();
                if (!await remoteOperationsManager.deleteSpectrumAsync(new DeleteSpectrumRequestModel() { Id = id }))
                    isDeleted = false;
            }

        private void raiseIsSegmentsLowlightRequired()
            if (removeFitSettings)
            {
            IsSegmentLowlightRequired = true;
            isSegmentLowlightRequired = false;
        }
        #endregion
                /* remove all points and segments for creating continuum */
                await clearSegmentsFitAndContinuumSpectraAsync();

        #region continuum
        private void askDeleteContinuumSpectrum()
        {
            if (WorkingItems.SpectraContainer.isAnySpectrumOfType(SpectrumType.ContinuumRemoved))
            {
                if (WorkingItems.SpectraContainer.tryGetSpectrumOfType(SpectrumType.ContinuumRemoved).IsSavedOnCloud)
                    IsDeleteQuestionVisible = true;
            }
                /* hide panel for creating continuum */
                IsSegmentsFitsUserControlShowRequired = false;
            }

        private async Task deleteContinuumRemovedSpectrumAsync()
            /* show success/error message, if required */
            if (showErrorMessages)
            {
            var remoteOperationsManager = serviceProvider.GetRequiredService<RemoteOperationsManager>();

            var spectrum = WorkingItems.SpectraContainer.tryGetSpectrumOfType(SpectrumType.ContinuumRemoved);

            if (await remoteOperationsManager.deleteSpectrumAsync(new DeleteSpectrumRequestModel() { Id = spectrum.Id }))
                if (isDeleted)
                {
                    /* hide dialog question */
                    IsDeleteQuestionVisible = false;

                /* remove spectrum from chart */
                WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(SpectrumType.ContinuumRemoved);

                    /* show success message */
                    showInfoBar(Microsoft.UI.Xaml.Controls.InfoBarSeverity.Informational,
                                "ContinuumRemovedSpectrumDeletingTitle".GetText(),
@@ -692,6 +746,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
                    showDialogMessage("ContinuumRemovedSpectrumDeletingErrorMessage".GetText());
                }
            }
        }

        private void executeContinuumRemovalProcedure()
        {
@@ -715,17 +770,16 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
            if (string.IsNullOrEmpty(result.DialogMessage))
            {
                await WorkingItems.SpectraContainer.addOrUpdateSpectrumAsync(result.Spectrum);
                //showInfoBar(Microsoft.UI.Xaml.Controls.InfoBarSeverity.Informational,
                //            "ContinuumRemovalTitle".GetText(),
                //            "ContinuumRemovalSuccess".GetText(),
                //            autoHide: true);
                showInfoBar(Microsoft.UI.Xaml.Controls.InfoBarSeverity.Informational,
                            "ContinuumRemovalTitle".GetText(),
                            "ContinuumRemovalSuccess".GetText(),
                            autoHide: true);
            }
            else
                showDialogMessage(result.DialogMessage);
        }


        private void clearSegmentsFitAndContinuumSpectra()
        private async Task clearSegmentsFitAndContinuumSpectraAsync()
        {
            /* reset values for letting add new missing points */
            continuumMissingPointsHelper.clear(clearLists: true);
@@ -734,8 +788,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
            SegmentsFitModelHelper.clear();

            /* remove continuum fitted spectrum */
            WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(SpectrumType.Continuum);

            if (await WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(SpectrumType.Continuum))
                /* remove the continuum-removed spectrum, if existing */
                askDeleteContinuumSpectrum();
        }
@@ -805,9 +858,11 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
        {
            IsLoading = true;

            /* create spectrum model for being saved on cloud */
            var spectrumFile = WorkingItems.SpectraContainer.tryGetSpectrumOfType(SpectrumType.ContinuumRemoved).ToSpectrumFile();
            spectrumFile.ParentId = WorkingItems.SpectraContainer.tryGetSpectrumOfType(SpectrumType.Raw).Id;

            /* create request */
            var saveSpectrumRequest = new SaveSpectrumOfTypeContinuumRemovedRequestModel()
            {
                SpectrumFile = spectrumFile,
@@ -815,8 +870,11 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
                Segments = segmentsFitModelHelper.SegmentsFitModels.Select(x => x.ToSegmentFitModel()).ToList()
            };

            /* execute request */
            var remoteOperationsManager = serviceProvider.GetRequiredService<RemoteOperationsManager>();
            SaveContinuumRemovedResponseModel responseobj = await remoteOperationsManager.saveSpectrumOfTypeContinuumRemovedAsync(saveSpectrumRequest);

            /* if no response, then show error message */
            if (responseobj == null)
            {
                IsLoading = false;
@@ -824,6 +882,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
                return;
            }

            /* if bad response, then show error message */
            if (!responseobj.IsOk)
            {
                IsLoading = false;
@@ -834,10 +893,12 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
                return;
            }

            /* save isSavedOnCloud and id of spectrum */
            var spectrum = WorkingItems.SpectraContainer.tryGetSpectrumOfType(SpectrumType.ContinuumRemoved);
            spectrum.setIsSavedOnCloud(responseobj.IsOk);
            spectrum.setId(responseobj.Id);

            /* save id for each fit segment */
            foreach (var segment in responseobj.Segments)
            {
                segmentsFitModelHelper.setSegmentId(segment.Id, segment.XStart, segment.XEnd);
@@ -851,8 +912,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
                        autoHide: true);
        }

        

        private void setFocusEnabled(bool isOn)
        {
            IsFocusRequired = isOn;
@@ -962,7 +1021,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
        public RelayCommand CommandAlignSpectrum => commandAlignSpectrum ?? (commandAlignSpectrum = new RelayCommand(() => executeSpectrumAlignmentAsync()));

        private RelayCommand commandRemoveAlignedSpectrum;
        public RelayCommand CommandRemoveAlignedSpectrum => commandRemoveAlignedSpectrum ?? (commandRemoveAlignedSpectrum = new RelayCommand(() => removeAlignedSpectrum()));
        public RelayCommand CommandRemoveAlignedSpectrum => commandRemoveAlignedSpectrum ?? (commandRemoveAlignedSpectrum = new RelayCommand(() => deleteAlignedSpectrumAsync()));

        private RelayCommand commandSaveAlignment;
        public RelayCommand CommandSaveAlignment => commandSaveAlignment ?? (commandSaveAlignment = new RelayCommand(() => saveAlignmentProcedureAsync()));
@@ -976,7 +1035,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels
        public RelayCommand CommandCancelDeleteContinuumRemovedSpectrum => commandCancelDeleteContinuumRemovedSpectrum ?? (commandCancelDeleteContinuumRemovedSpectrum = new RelayCommand(() => IsDeleteQuestionVisible = false));

        private RelayCommand commandClearFitSegments;
        public RelayCommand CommandClearFitSegments => commandClearFitSegments ?? (commandClearFitSegments = new RelayCommand(() => clearSegmentsFitAndContinuumSpectra()));
        public RelayCommand CommandClearFitSegments => commandClearFitSegments ?? (commandClearFitSegments = new RelayCommand(() => clearSegmentsFitAndContinuumSpectraAsync()));

        private RelayCommand commandDeleteContinuumRemovedSpectrum;
        public RelayCommand CommandDeleteContinuumRemovedSpectrum => commandDeleteContinuumRemovedSpectrum ?? (commandDeleteContinuumRemovedSpectrum = new RelayCommand(() => deleteContinuumRemovedSpectrumAsync()));
+1 −1
Original line number Diff line number Diff line
@@ -260,7 +260,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.ViewModels


            if (spectrumType == SpectrumType.Raw)
                WorkingItems.SpectraContainer.tryRemoveSpectrumOfTypeAsync(SpectrumType.Aligned);
                await serviceProvider.GetRequiredService<ChartViewModel>().deleteDerivedSpectraAsync();

            /* try reading the file to make spectrum available */
            if (await tryReadingSpectrumFileAsync(spectrumType) && (spectrumType == SpectrumType.Ref || spectrumType == SpectrumType.RefWhite))
Loading