예제 #1
0
        public async Task <int> LoadLastSessionAsync()
        {
            if (_loaded)
            {
                return(0); // Already loaded
            }

            var data = await SessionUtility.GetSerializedSessionMetaDataAsync();

            if (data == null)
            {
                return(0); // No session data found
            }

            NotepadsSessionDataV1 sessionData;

            try
            {
                var json    = JObject.Parse(data);
                var version = (int)json["Version"];

                if (version == 1)
                {
                    sessionData = JsonConvert.DeserializeObject <NotepadsSessionDataV1>(data);
                }
                else
                {
                    throw new Exception($"Invalid version found in session metadata: {version}");
                }
            }
            catch (Exception ex)
            {
                LoggingService.LogError($"[SessionManager] Failed to load last session metadata: {ex.Message}");
                await ClearSessionDataAsync();

                return(0);
            }

            IList <ITextEditor> recoveredEditor = new List <ITextEditor>();

            foreach (var textEditorData in sessionData.TextEditors)
            {
                ITextEditor textEditor;

                try
                {
                    textEditor = await RecoverTextEditorAsync(textEditorData);
                }
                catch (Exception ex)
                {
                    LoggingService.LogError($"[SessionManager] Failed to recover TextEditor: {ex.Message}");
                    continue;
                }

                if (textEditor != null)
                {
                    recoveredEditor.Add(textEditor);
                    _sessionData.TryAdd(textEditor.Id, textEditorData);
                }
            }

            _notepadsCore.OpenTextEditors(recoveredEditor.ToArray(), sessionData.SelectedTextEditor);
            _notepadsCore.SetTabScrollViewerHorizontalOffset(sessionData.TabScrollViewerHorizontalOffset);

            _loaded = true;

            return(_sessionData.Count);
        }
예제 #2
0
        public async Task <int> LoadLastSessionAsync()
        {
            if (_loaded)
            {
                return(0); // Already loaded
            }

            var data = await SessionUtility.GetSerializedSessionMetaDataAsync(_sessionMetaDataFileName);

            if (data == null)
            {
                return(0); // No session data found
            }

            NotepadsSessionDataV1 sessionData;

            try
            {
                var json    = JsonDocument.Parse(data);
                var version = json.RootElement.GetProperty("Version").GetInt32();

                if (version == 1)
                {
                    sessionData = JsonSerializer.Deserialize <NotepadsSessionDataV1>(data);
                }
                else
                {
                    throw new Exception($"Invalid version found in session metadata: {version}");
                }
            }
            catch (Exception ex)
            {
                LoggingService.LogError($"[{nameof(SessionManager)}] Failed to load last session metadata: {ex.Message}");
                Analytics.TrackEvent("SessionManager_FailedToLoadLastSession", new Dictionary <string, string>()
                {
                    { "Exception", ex.Message }
                });
                await ClearSessionDataAsync();

                return(0);
            }

            IList <ITextEditor> recoveredEditor = new List <ITextEditor>();

            foreach (var textEditorData in sessionData.TextEditors)
            {
                ITextEditor textEditor;

                try
                {
                    textEditor = await RecoverTextEditorAsync(textEditorData);
                }
                catch (Exception ex)
                {
                    LoggingService.LogError($"[{nameof(SessionManager)}] Failed to recover TextEditor: {ex.Message}");
                    Analytics.TrackEvent("SessionManager_FailedToRecoverTextEditor", new Dictionary <string, string>()
                    {
                        { "Exception", ex.Message }
                    });
                    continue;
                }

                if (textEditor != null)
                {
                    recoveredEditor.Add(textEditor);
                    _sessionDataCache.TryAdd(textEditor.Id, textEditorData);
                }
            }

            _notepadsCore.OpenTextEditors(recoveredEditor.ToArray(), sessionData.SelectedTextEditor);
            _notepadsCore.SetTabScrollViewerHorizontalOffset(sessionData.TabScrollViewerHorizontalOffset);

            _loaded = true;

            LoggingService.LogInfo($"[{nameof(SessionManager)}] {_sessionDataCache.Count} tab(s) restored from last session.");

            return(_sessionDataCache.Count);
        }