Beispiel #1
0
        public DocumentViewModel(string filePath, Action <string, string> filePathChangingCallback)
        {
            _fileInfo = ArgsAndSettings.CachedFiles.FirstOrDefault(x => x.OriginalFilePath == filePath || x.CachedFilePath == filePath);
            IsDirty   = new NotifyingProperty <bool>(x => SaveFileInfo());

            if (_fileInfo == null)
            {
                var uid                 = Guid.NewGuid().ToString();
                var fileName            = Path.GetFileName(filePath);
                var cachedDirectoryPath = Path.Combine(Constants.FileCachePath, uid);

                _fileInfo = new SerializableFileInfo();
                _fileInfo.CachedFilePath = Path.Combine(cachedDirectoryPath, fileName);
                Directory.CreateDirectory(cachedDirectoryPath);

                if (File.Exists(filePath))
                {
                    _fileInfo.OriginalFilePath = filePath;
                    _fileInfo.Hash             = Methods.HashFile(filePath);
                    File.Copy(filePath, _fileInfo.CachedFilePath);
                }
                else
                {
                    File.WriteAllText(_fileInfo.CachedFilePath, "");
                }

                ArgsAndSettings.CachedFiles.Add(_fileInfo);
            }

            FileName = new NotifyingProperty <string>(
                (oldName, newName) => {
                UpdateCachedFileName(newName);
                filePathChangingCallback(oldName, newName);
            },
                Path.GetFileName(_fileInfo.CachedFilePath)
                );

            FilePath = new NotifyingProperty <string>(_fileInfo.OriginalFilePath);

            DocumentContent = new AvalonTextViewModel(_fileInfo.CachedFilePath);
            DocumentContent.Content.PropertyChanged += (s, e) => UpdateHash();
            DocumentContent.ApiProvider.Save         = SaveChanges;

            _fileCheckTimer.Interval = TimeSpan.FromMilliseconds(150);
            _fileCheckTimer.Tick    += (s, e) => UpdateIsDirty();
            _fileCheckTimer.Start();
            UpdateIsDirty();
        }
        public DocumentViewModel(string filePath, Action<string, string> filePathChangingCallback)
        {
            _fileInfo = ArgsAndSettings.CachedFiles.FirstOrDefault(x => x.OriginalFilePath == filePath || x.CachedFilePath == filePath);
            IsDirty = new NotifyingProperty<bool>(x => SaveFileInfo());

            if(_fileInfo == null) {
                var uid = Guid.NewGuid().ToString();
                var fileName = Path.GetFileName(filePath);
                var cachedDirectoryPath = Path.Combine(Constants.FileCachePath, uid);

                _fileInfo = new SerializableFileInfo();
                _fileInfo.CachedFilePath = Path.Combine(cachedDirectoryPath, fileName);
                Directory.CreateDirectory(cachedDirectoryPath);

                if(File.Exists(filePath)) {
                    _fileInfo.OriginalFilePath = filePath;
                    _fileInfo.Hash = Methods.HashFile(filePath);
                    File.Copy(filePath, _fileInfo.CachedFilePath);
                } else {
                    File.WriteAllText(_fileInfo.CachedFilePath, "");
                }

                ArgsAndSettings.CachedFiles.Add(_fileInfo);
            }

            FileName = new NotifyingProperty<string>(
                (oldName, newName) => {
                    UpdateCachedFileName(newName);
                    filePathChangingCallback(oldName, newName);
                },
                Path.GetFileName(_fileInfo.CachedFilePath)
            );

            FilePath = new NotifyingProperty<string>(_fileInfo.OriginalFilePath);

            DocumentContent = new AvalonTextViewModel(_fileInfo.CachedFilePath);
            DocumentContent.Content.PropertyChanged += (s,e) => UpdateHash();
            DocumentContent.ApiProvider.Save = SaveChanges;

            _fileCheckTimer.Interval = TimeSpan.FromMilliseconds(150);
            _fileCheckTimer.Tick += (s, e) => UpdateIsDirty();
            _fileCheckTimer.Start();
            UpdateIsDirty();
        }