Example #1
0
        public static async Task SelectSessionAsync(Session newSelectedSession, ILocalStorageService localStorage)
        {
            Session.selectedSession = newSelectedSession;
            await localStorage.SetItemAsync(
                "selected_session_id", newSelectedSession.id.ToString());

            OnSessionSelected?.Invoke(null, newSelectedSession);
        }
Example #2
0
        public static async Task OnInitializedAsync(ILocalStorageService localStorage)
        {
            var sessionIdsJson = await localStorage.GetItemAsync <string>("session_ids");

            Console.WriteLine($"Available Stored Session Ids = `{sessionIdsJson}`");
            if (sessionIdsJson.IsNullOrWhiteSpace())
            {
                return;
            }
            var sessionIds = Newtonsoft.Json.JsonConvert.DeserializeObject <Guid[]>(sessionIdsJson);

            var sessions = await sessionIds
                           .NullToEmpty()
                           .Select(
                async sessionId =>
            {
                var sessionJson = await localStorage.GetItemAsync <string>(sessionId.ToString("N"));
                if (sessionJson.IsNullOrWhiteSpace())
                {
                    Console.WriteLine($"Session [{sessionId}] not found in storage");
                    await Session.DeleteSession(sessionId, localStorage);
                    return(default(Session?));
                }
                var session = Newtonsoft.Json.JsonConvert.DeserializeObject <Session>(sessionJson);
                if (!Server.Servers.Select(server => server.id).Contains(session.serverId))
                {
                    Console.WriteLine($"Session [{sessionId}]'s server has been deleted; deleting session.");
                    await Session.DeleteSession(sessionId, localStorage);
                    return(default(Session?));
                }
                return(session);
            })
                           .AsyncEnumerable()
                           .SelectWhereHasValue()
                           .ToArrayAsync();

            Session.Sessions = new List <Session>(sessions);

            var selectedSessionIdStr = await localStorage.GetItemAsync <string>("selected_session_id");

            Console.WriteLine($"SELECTING: Session Id = `{selectedSessionIdStr}`");
            if (selectedSessionIdStr.HasBlackSpace())
            {
                if (Guid.TryParse(selectedSessionIdStr, out Guid selectedSessionId))
                {
                    selectedSession = Sessions
                                      .Where(session => session.id == selectedSessionId)
                                      .First(
                        (session, next) => session,
                        () => default(Session?));
                    if (selectedSession.HasValue)
                    {
                        OnSessionSelected?.Invoke(null, selectedSession.Value);
                    }
                }
                if (!selectedSession.HasValue)
                {
                    Console.WriteLine($"INVALID Stored Selected Session Id = `{selectedSessionIdStr}`, deleting...");
                    await localStorage.RemoveItemAsync("selected_session_id");

                    return;
                }
                if (!Server.Servers.Select(server => server.id).Contains(selectedSession.Value.serverId))
                {
                    Console.WriteLine($"Session [{selectedSession.Value.id}]'s server has been deleted; unselecting session.");
                    await localStorage.RemoveItemAsync("selected_session_id");

                    selectedSession = default(Session?);
                    return;
                }
            }
        }