-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
98 lines (91 loc) · 4.1 KB
/
Copy pathMainPage.xaml.cs
File metadata and controls
98 lines (91 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Net;
using System.Web;
using Mopups.Services;
using TuneXtend.Interfaces;
using TuneXtend.Models.Spotify;
using TuneXtend.Pages;
namespace TuneXtend
{
public partial class MainPage : ContentPage
{
private readonly IStorageService _storageService;
private readonly ISpotifyRestService _spotifyRestService;
public MainPage(IStorageService storageService, ISpotifyRestService spotifyRestService)
{
_storageService = storageService;
_spotifyRestService = spotifyRestService;
InitializeComponent();
}
private async void BtnCopySpotifyPlaylist_OnClicked(object sender, EventArgs e)
{
BtnCopySpotifyPlaylist.IsEnabled = false;
var spotifyToken = await _storageService.GetSpotifyTokenAsync();
if (spotifyToken is null)
{
bool setCodeVerifier = await _storageService.SetCodeVerifierAsync(SpotifyConstants.CODE_VERIFIER);
if (!setCodeVerifier)
{
// todo : handle if setting code verifier is not success
return;
}
string queryString = $"{SpotifyConstants.AUTH_URI}?" +
$"response_type={SpotifyConstants.RESPONSE_TYPE}" +
$"&client_id={SpotifyConstants.CLIENT_ID}" +
$"&scope={WebUtility.UrlEncode(SpotifyConstants.SCOPE)}" +
$"&code_challenge_method={SpotifyConstants.CODE_CHALLANGE_METHOD}" +
$"&code_challenge={SpotifyConstants.CODE_CHALLANGE}" +
$"&redirect_uri={WebUtility.UrlEncode(SpotifyConstants.REDIRECT_URI)}";
//$"&state={SpotifyConstants.SCOPE}";
var popupPage = new UserLoginPopupPage(queryString);
await MopupService.Instance.PushAsync(popupPage);
var popupResult = await popupPage.PopupDismissedTask;
if (popupResult is null)
{
await DisplayAlert("Alert", "You need to be logged in to access this feature", "Ok");
}
else
{
var callbackUrl = new Uri(popupResult);
var authCode = HttpUtility.ParseQueryString(callbackUrl.Query).Get("code");
if (authCode is null)
{
await DisplayAlert("Alert", "You need to be logged in to access this feature", "Ok");
}
else
{
Token token = await _spotifyRestService.GetTokenAsync(authCode);
if (token is null)
{
// todo : better error
await DisplayAlert("Error", "Something Went Wrong", "Ok");
}
else
{
var setToken = await _storageService.SetSpotifyTokenAsync(token);
if (setToken)
{
await Shell.Current.GoToAsync(nameof(CopyPlaylistSpotify));
return;
}
else
{
// todo : better error
await DisplayAlert("Error", "Something Went Wrong", "Ok");
}
}
}
}
}
else
{
if (!await _storageService.ValidateSpotifyTokenAsync(spotifyToken))
{
var token = await _spotifyRestService.GetTokenAsync(refresh: true);
await _storageService.SetSpotifyTokenAsync(token);
}
await Shell.Current.GoToAsync(nameof(CopyPlaylistSpotify));
}
BtnCopySpotifyPlaylist.IsEnabled = true;
}
}
}