Skip to content
This repository was archived by the owner on Sep 22, 2023. It is now read-only.

Commit e68a963

Browse files
committed
App finally gets apikey from settings and more
but i cant remember what
1 parent 3d44bd7 commit e68a963

File tree

13 files changed

+191
-32
lines changed

13 files changed

+191
-32
lines changed

Y2Offline.Android/MainActivity.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using System.Linq;
1212
using Android.Widget;
1313
using Android.Views;
14+
using System.IO;
15+
using Newtonsoft.Json.Linq;
1416

1517
namespace Y2Offline.Droid
1618
{
@@ -25,7 +27,12 @@ protected override void OnCreate(Bundle savedInstanceState)
2527

2628
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
2729
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
30+
31+
32+
2833
LoadApplication(new App(0, null));
34+
35+
2936

3037

3138
if (Intent.Action.Equals(Intent.Action) &&
@@ -126,7 +133,7 @@ private async void handleSendUrl()
126133
Services.YTVidDetails videoDetails = null;
127134
using (var youtubeService = new YouTubeService(new BaseClientService.Initializer()
128135
{
129-
ApiKey = "AIzaSyDSf8QFsOOdjTMwOpa1408Beo1vskmNPkI",
136+
ApiKey = GetApiKey(),
130137
}))
131138
{
132139
var searchRequest = youtubeService.Videos.List("snippet");
@@ -142,7 +149,7 @@ private async void handleSendUrl()
142149
Description = youTubeVideo.Snippet.Description,
143150
Title = youTubeVideo.Snippet.Title,
144151
ChannelTitle = youTubeVideo.Snippet.ChannelTitle,
145-
PublicationDate = youTubeVideo.Snippet.PublishedAt
152+
PublicationDate = (DateTime)youTubeVideo.Snippet.PublishedAt
146153
};
147154
}
148155

@@ -181,5 +188,26 @@ public static string GetBetween(string strSource, string strStart, string strEnd
181188

182189
return "";
183190
}
191+
192+
public string GetApiKey()
193+
{
194+
string filePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyVideos);
195+
string mypath = Path.Combine(filePath, "settings.json");
196+
197+
if (File.Exists(mypath))
198+
{
199+
//if settings have been generated
200+
201+
202+
JObject jsonObject = JObject.Parse(File.ReadAllText(mypath));
203+
204+
return (string)jsonObject["apikey"];
205+
206+
}
207+
else
208+
{
209+
return "";
210+
}
211+
}
184212
}
185213
}

Y2Offline.Android/ToastAndroid.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Android.App;
2+
using Android.Content;
3+
using Android.OS;
4+
using Android.Runtime;
5+
using Android.Views;
6+
using Android.Widget;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Text;
11+
12+
namespace Y2Offline.Droid
13+
{
14+
public class ToastAndroid : IToastMessage
15+
{
16+
public void LongAlert(string message)
17+
{
18+
Toast.MakeText(Application.Context, message, ToastLength.Long).Show();
19+
}
20+
21+
public void ShortAlert(string message)
22+
{
23+
Toast.MakeText(Application.Context, message, ToastLength.Short).Show();
24+
}
25+
}
26+
}

Y2Offline.Android/Y2Offline.Android.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<Compile Include="Resources\Resource.designer.cs" />
9696
<Compile Include="Properties\AssemblyInfo.cs" />
9797
<Compile Include="StatusBarImplementation.cs" />
98+
<Compile Include="ToastAndroid.cs" />
9899
</ItemGroup>
99100
<ItemGroup>
100101
<AndroidAsset Include="Assets\FontAwesome5Brands.otf" />

Y2Offline/App.xaml.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.IO;
24
using Xamarin.Forms;
35
using Xamarin.Forms.Xaml;
46
using Y2Offline.Services;
@@ -28,10 +30,14 @@ public App(int state, Services.YTVidDetails details)
2830
}
2931

3032

33+
3134
}
3235

3336
protected override void OnStart()
3437
{
38+
base.OnStart();
39+
40+
3541
}
3642

3743
protected override void OnSleep()
@@ -41,5 +47,6 @@ protected override void OnSleep()
4147
protected override void OnResume()
4248
{
4349
}
50+
4451
}
4552
}

Y2Offline/IToastMessage.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Y2Offline
6+
{
7+
public interface IToastMessage
8+
{
9+
void LongAlert(string message);
10+
void ShortAlert(string message);
11+
}
12+
}

Y2Offline/Services/YTSearch.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void Main(string[] args)
4040
if(searchterm == null) { throw new Exception("searchterm was null"); }
4141
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
4242
{
43-
ApiKey = "AIzaSyDSf8QFsOOdjTMwOpa1408Beo1vskmNPkI",
43+
ApiKey = GetApiKey(),
4444
ApplicationName = this.GetType().ToString()
4545
});
4646

@@ -111,7 +111,7 @@ static void Main(string[] args)
111111

112112
public double GetSearchLimit()
113113
{
114-
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
114+
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
115115
string mypath = Path.Combine(filePath, "settings.json");
116116

117117
if (File.Exists(mypath))
@@ -132,7 +132,7 @@ public double GetSearchLimit()
132132

133133
public bool GetShowThumbnails()
134134
{
135-
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
135+
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
136136
string mypath = Path.Combine(filePath, "settings.json");
137137

138138
if (File.Exists(mypath))
@@ -150,5 +150,26 @@ public bool GetShowThumbnails()
150150
return true;
151151
}
152152
}
153+
154+
public string GetApiKey()
155+
{
156+
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
157+
string mypath = Path.Combine(filePath, "settings.json");
158+
159+
if (File.Exists(mypath))
160+
{
161+
//if settings have been generated
162+
163+
164+
JObject jsonObject = JObject.Parse(File.ReadAllText(mypath));
165+
166+
return (string)jsonObject["apikey"];
167+
168+
}
169+
else
170+
{
171+
return "";
172+
}
173+
}
153174
}
154175
}

Y2Offline/Services/YTVidDetails.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ public class YTVidDetails
1010
public string Description { get; set; }
1111
public string Title { get; set; }
1212
public string ChannelTitle { get; set; }
13-
public DateTime? PublicationDate { get; set; }
13+
public DateTime PublicationDate { get; set; }
1414
}
1515
}

Y2Offline/Views/SearchAndAdd.xaml.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public SearchAndAdd()
2020
{
2121
InitializeComponent();
2222

23+
if (GetApiKey() == "")
24+
{
25+
DisplayAlert("Welcome to Y2Offline", "Please set your api key in the settings first, otherwise the app won't work", "OK");
26+
}
27+
2328
ToolbarItems.Add(new ToolbarItem("Downloads", "download_icon.png", async () =>
2429
{
2530
await Navigation.PushAsync(new Downloads());
@@ -120,7 +125,14 @@ private async void SearchButton_Clicked(object sender, EventArgs e)
120125
activityIndicator2.IsEnabled = true;
121126
activityIndicator2.IsRunning = true;
122127

123-
await Y2Sharp.Youtube.Video.GetInfo(video.Id);
128+
try
129+
{
130+
await Y2Sharp.Youtube.Video.GetInfo(video.Id);
131+
}
132+
catch (Exception)
133+
{
134+
await DisplayAlert("Error", "Error while getting video id, try againg later", "OK");
135+
}
124136

125137
activityIndicator2.IsEnabled = false;
126138
activityIndicator2.IsRunning = false;
@@ -148,7 +160,7 @@ private async void SearchButton_Clicked(object sender, EventArgs e)
148160

149161
public bool GetShowThumbnails()
150162
{
151-
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
163+
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
152164
string mypath = Path.Combine(filePath, "settings.json");
153165

154166
if (File.Exists(mypath))
@@ -167,6 +179,27 @@ public bool GetShowThumbnails()
167179
}
168180
}
169181

182+
public string GetApiKey()
183+
{
184+
string filePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyVideos);
185+
string mypath = Path.Combine(filePath, "settings.json");
186+
187+
if (File.Exists(mypath))
188+
{
189+
//if settings have been generated
190+
191+
192+
JObject jsonObject = JObject.Parse(File.ReadAllText(mypath));
193+
194+
return (string)jsonObject["apikey"];
195+
196+
}
197+
else
198+
{
199+
return "";
200+
}
201+
}
202+
170203

171204

172205
}

Y2Offline/Views/SettingsPage.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
<ContentPage.Content>
77
<StackLayout BackgroundColor="#3b403f" >
88

9+
<StackLayout>
10+
<Label Text="Your API key" HorizontalOptions="Center" FontSize="20" TextColor="White"></Label>
11+
<Entry HorizontalOptions="CenterAndExpand" Placeholder="Your yotube data api v3 key" FontSize="20" TextColor="White" Margin="15, 0 ,15, 10" x:Name="ApiKeyEntry" />
12+
</StackLayout>
13+
14+
915
<StackLayout Orientation="Vertical" Margin="15">
1016

1117
<Label Text="Search result limit" HorizontalOptions="Center" FontSize="20" TextColor="White"></Label>

Y2Offline/Views/SettingsPage.xaml.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,23 @@ namespace Y2Offline.Views
1515
[XamlCompilation(XamlCompilationOptions.Compile)]
1616
public partial class SettingsPage : ContentPage
1717
{
18-
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
18+
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
1919

2020

2121
public SettingsPage()
2222
{
2323

2424
InitializeComponent();
25-
26-
27-
25+
26+
27+
try
28+
{
29+
Directory.CreateDirectory(filePath);
30+
}
31+
catch(Exception ex)
32+
{
33+
DisplayAlert("Error while creating app folder", ex.Message, "OK");
34+
}
2835

2936
string mypath = Path.Combine(filePath, "settings.json");
3037

@@ -33,7 +40,8 @@ public SettingsPage()
3340
var myData = new
3441
{
3542
searchlimit = "15",
36-
showthumbnails = "true"
43+
showthumbnails = "true",
44+
apikey = ""
3745
};
3846

3947
string jsonData = JsonConvert.SerializeObject(myData);
@@ -48,15 +56,24 @@ public SettingsPage()
4856
}
4957
}
5058

51-
JObject jsonObject = JObject.Parse(File.ReadAllText(mypath));
59+
try
60+
{
61+
JObject jsonObject = JObject.Parse(File.ReadAllText(mypath));
5262

5363

54-
int searchlimit = (int)jsonObject["searchlimit"];
55-
bool showthumbnails = (bool)jsonObject["showthumbnails"];
64+
int searchlimit = (int)jsonObject["searchlimit"];
65+
bool showthumbnails = (bool)jsonObject["showthumbnails"];
66+
string apikey = (string)jsonObject["apikey"];
5667

5768

58-
searchlimitslider.Value = searchlimit;
59-
LoadThumbnailsSwitch.IsToggled = showthumbnails;
69+
searchlimitslider.Value = searchlimit;
70+
LoadThumbnailsSwitch.IsToggled = showthumbnails;
71+
ApiKeyEntry.Text = apikey;
72+
}
73+
catch(Exception ex)
74+
{
75+
DisplayAlert("Error while reading data", ex.Message, "OK");
76+
}
6077

6178
}
6279

@@ -69,7 +86,8 @@ private void Save_Clicked(object sender, EventArgs e)
6986
var myData = new
7087
{
7188
searchlimit = Math.Round(searchlimitslider.Value).ToString(),
72-
showthumbnails = LoadThumbnailsSwitch.IsToggled.ToString()
89+
showthumbnails = LoadThumbnailsSwitch.IsToggled.ToString(),
90+
apikey = ApiKeyEntry.Text
7391
};
7492

7593
string jsonData = JsonConvert.SerializeObject(myData);
@@ -103,5 +121,7 @@ private void searchlimitslider_ValueChanged(object sender, ValueChangedEventArgs
103121

104122
SliderValueLabel.Text = noobslider.ToString();
105123
}
124+
125+
106126
}
107127
}

0 commit comments

Comments
 (0)