|
| 1 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 2 | +using JSharp.Domain.Abstractions; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.ComponentModel; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace JSharp.Services |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Represents a temporary, editable copy of application settings for the UI and passing settings to be saved |
| 14 | + /// </summary> |
| 15 | + public class EditableSettings : ObservableObject, IEditableObject |
| 16 | + { |
| 17 | + // current value fields |
| 18 | + private string _language; |
| 19 | + private double _zoomFactor; |
| 20 | + private byte _pngCompressionLevel; |
| 21 | + private byte _jpgSaveQuality; |
| 22 | + private string _saveFileExtension; |
| 23 | + |
| 24 | + // Backup fields for edit session |
| 25 | + private string _backupLanguage; |
| 26 | + private double _backupZoomFactor; |
| 27 | + private byte _backupPngCompressionLevel; |
| 28 | + private byte _backupJpgSaveQuality; |
| 29 | + private string _backupSaveFileExtension; |
| 30 | + |
| 31 | + private bool inEdit; |
| 32 | + |
| 33 | + // exposed properties for UI |
| 34 | + public string Language |
| 35 | + { |
| 36 | + get => _language; |
| 37 | + set => SetProperty(ref _language, value); |
| 38 | + } |
| 39 | + |
| 40 | + public double ZoomFactor |
| 41 | + { |
| 42 | + get => _zoomFactor; |
| 43 | + set => SetProperty(ref _zoomFactor, value); |
| 44 | + } |
| 45 | + |
| 46 | + public byte PngCompressionLevel |
| 47 | + { |
| 48 | + get => _pngCompressionLevel; |
| 49 | + set => SetProperty(ref _pngCompressionLevel, value); |
| 50 | + } |
| 51 | + |
| 52 | + public byte JpgSaveQuality |
| 53 | + { |
| 54 | + get => _jpgSaveQuality; |
| 55 | + set => SetProperty(ref _jpgSaveQuality, value); |
| 56 | + } |
| 57 | + |
| 58 | + public string SaveFileExtension |
| 59 | + { |
| 60 | + get => _saveFileExtension; |
| 61 | + set => SetProperty(ref _saveFileExtension, value); |
| 62 | + } |
| 63 | + |
| 64 | + public EditableSettings() |
| 65 | + { |
| 66 | + } |
| 67 | + |
| 68 | + public void BeginEdit() |
| 69 | + { |
| 70 | + if (inEdit) return; |
| 71 | + |
| 72 | + _backupLanguage = _language; |
| 73 | + _backupZoomFactor = _zoomFactor; |
| 74 | + _backupPngCompressionLevel = _pngCompressionLevel; |
| 75 | + _backupJpgSaveQuality = _jpgSaveQuality; |
| 76 | + _backupSaveFileExtension = _saveFileExtension; |
| 77 | + |
| 78 | + inEdit = true; |
| 79 | + } |
| 80 | + |
| 81 | + public void CancelEdit() |
| 82 | + { |
| 83 | + if (!inEdit) return; |
| 84 | + |
| 85 | + Language = _backupLanguage; |
| 86 | + ZoomFactor = _backupZoomFactor; |
| 87 | + PngCompressionLevel = _backupPngCompressionLevel; |
| 88 | + JpgSaveQuality = _backupJpgSaveQuality; |
| 89 | + SaveFileExtension = _backupSaveFileExtension; |
| 90 | + |
| 91 | + inEdit = false; |
| 92 | + } |
| 93 | + |
| 94 | + public void EndEdit() |
| 95 | + { |
| 96 | + if (!inEdit) return; |
| 97 | + |
| 98 | + inEdit = false; |
| 99 | + |
| 100 | + _backupLanguage = default!; |
| 101 | + _backupZoomFactor = default; |
| 102 | + _backupPngCompressionLevel = default; |
| 103 | + _backupJpgSaveQuality = default; |
| 104 | + _backupSaveFileExtension = default!; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments