Exemplo n.º 1
0
        /// <summary>
        ///     Handle app protocol. If true is returned this method handled page
        ///     navigation. If false is returned, you must handle app protocol
        /// </summary>
        public static async Task <bool> HandleProtocolAsync(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            try
            {
                var parser  = DeepLinkParser.Create(path);
                var section = parser.Root?.Split('/')[0]?.ToLower();

                // Try get the session ID is one was passed through.
                parser.TryGetValue("session", out var sessionId);
                if (!string.IsNullOrEmpty(sessionId))
                {
                    SettingsService.Instance.SessionId = sessionId;
                }

                switch (section)
                {
                case "resume-playback":
                    await HandleResumeAsync();

                    break;

                case "cortana":
                    if (!parser.TryGetValue("command", out var command))
                    {
                        throw new SoundByteException("Incorrect Protocol", "Command was not supplied (command={command}).");
                    }

                    await HandleCortanaCommandAsync(command);

                    break;

                case "track":
                    if (!parser.TryGetValue("d", out var rawProtocolData))
                    {
                        throw new SoundByteException("Incorrect Protocol", "Data was not supplied (d={data}).");
                    }

                    await HandleTrackProtocolAsync(rawProtocolData);

                    break;

                case "user":
                    parser.TryGetValue("id", out var userId);
                    parser.TryGetValue("service", out var userService);

                    // Get user
                    var user = await BaseUser.GetUserAsync(int.Parse(userService), userId);

                    if (user == null)
                    {
                        throw new Exception("User does not exist");
                    }

                    App.NavigateTo(typeof(UserView), user);
                    return(true);

                case "playlist":
                    parser.TryGetValue("id", out var playlistId);
                    parser.TryGetValue("service", out var playlistService);

                    // Get playlist
                    var playlist = await BasePlaylist.GetPlaylistAsync(int.Parse(playlistService), playlistId);

                    if (playlist == null)
                    {
                        throw new Exception("Playlist does not exist");
                    }

                    App.NavigateTo(typeof(PlaylistView), playlist);
                    return(true);

                case "playback":
                    parser.TryGetValue("command", out var playbackCommand);
                    HandlePlaybackCommand(playbackCommand);
                    break;
                }

                return(false);
            }
            catch (Exception e)
            {
                await NavigationService.Current.CallMessageDialogAsync("The specified protocol is not correct. App will now launch as normal.\n\n" + e.Message);

                return(false);
            }
        }