Ejemplo n.º 1
0
        /// <summary>
        /// Loads an empty source code
        /// </summary>
        private void LoadNewFile()
        {
            Code = SourceCode.CreateEmpty();

            _ = FilesHistoryService.DismissCurrentActivityAsync();

            CodeLoaded?.Invoke(this, Code.Content);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads and restores the serialized state of the current instance, if available
        /// </summary>
        /// <param name="file">The optional file to load, if present</param>
        public async Task RestoreStateAsync(IFile?file)
        {
            if (file is null)
            {
                string
                    temporaryPath = FilesService.TemporaryFilesPath,
                    statePath     = Path.Combine(temporaryPath, "state.json");

                if (!(await FilesService.TryGetFileFromPathAsync(statePath) is IFile jsonFile))
                {
                    return;
                }

                using Stream stream = await jsonFile.OpenStreamForReadAsync();

                using StreamReader reader = new StreamReader(stream);

                string json = await reader.ReadToEndAsync();

                IdeState state = JsonSerializer.Deserialize <IdeState>(json);

                if (state.FilePath is null)
                {
                    Code = SourceCode.CreateEmpty();
                }
                else
                {
                    IFile?sourceFile = await FilesService.TryGetFileFromPathAsync(state.FilePath);

                    if (sourceFile is null)
                    {
                        Code = SourceCode.CreateEmpty();
                    }
                    else
                    {
                        Code = await SourceCode.TryLoadFromEditableFileAsync(sourceFile) ?? SourceCode.CreateEmpty();
                    }
                }

                Text   = state.Text.AsMemory();
                Row    = state.Row;
                Column = state.Column;

                if (!(Code.File is null))
                {
                    _ = FilesHistoryService.LogOrUpdateActivityAsync(Code.File);
                }

                StateRestored?.Invoke(this, state);
            }
            else
            {
                await TryLoadTextFromFileAsync(file);
            }
        }