Cleaning up the main activity.

This commit is contained in:
Tristan Roux
2019-04-28 19:04:12 +02:00
parent 6bbddb43f0
commit 00de07b997
7 changed files with 957 additions and 971 deletions
+1 -1
View File
@@ -215,7 +215,7 @@ namespace Opus.Api.Services
meta.Tag.Album = title + " - " + artist;
meta.Tag.Comment = youtubeID;
IPicture[] pictures = new IPicture[1];
Bitmap bitmap = Picasso.With(Application.Context).Load(await MainActivity.GetBestThumb(thumbnails)).Transform(new RemoveBlackBorder(true)).Get();
Bitmap bitmap = Picasso.With(Application.Context).Load(await YoutubeManager.GetBestThumb(thumbnails)).Transform(new RemoveBlackBorder(true)).Get();
byte[] data;
using (var MemoryStream = new MemoryStream())
{
+1 -1
View File
@@ -492,7 +492,7 @@ namespace Opus.Api.Services
Video video = await client.GetVideoAsync(song.YoutubeID);
song.Title = video.Title;
song.Artist = video.Author;
song.Album = await MainActivity.GetBestThumb(new string[] { video.Thumbnails.MaxResUrl, video.Thumbnails.StandardResUrl, video.Thumbnails.HighResUrl });
song.Album = await YoutubeManager.GetBestThumb(new string[] { video.Thumbnails.MaxResUrl, video.Thumbnails.StandardResUrl, video.Thumbnails.HighResUrl });
song.Duration = (int)video.Duration.TotalMilliseconds;
Player.instance?.RefreshPlayer();
+65
View File
@@ -11,15 +11,18 @@ using Google.Apis.YouTube.v3.Data;
using Opus.Api.Services;
using Opus.DataStructure;
using Opus.Fragments;
using SQLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using TagLib;
using YoutubeExplode;
using YoutubeExplode.Models;
using CursorLoader = Android.Support.V4.Content.CursorLoader;
using PlaylistItem = Opus.DataStructure.PlaylistItem;
namespace Opus.Api
{
@@ -188,6 +191,41 @@ namespace Opus.Api
if (names.Count > 0)
DownloadFiles(names.ToArray(), videoIDs.ToArray(), playlist);
}
/// <summary>
/// Update all playlists that should be synced.
/// </summary>
public async static void SyncPlaylists()
{
if (!await MainActivity.instance.WaitForYoutube())
return;
ISharedPreferences prefManager = PreferenceManager.GetDefaultSharedPreferences(MainActivity.instance);
DateTime lastSync = DateTime.Parse(prefManager.GetString("syncDate", DateTime.MinValue.ToString()));
if (lastSync.AddHours(1) > DateTime.Now || !MainActivity.instance.HasWifi()) //Make a time check, do not check if the user is downloading or if the user has just started the app two times
return;
ISharedPreferencesEditor editor = prefManager.Edit();
editor.PutString("syncDate", DateTime.Now.ToString());
List<PlaylistItem> SyncedPlaylists = new List<PlaylistItem>();
await Task.Run(() =>
{
SQLiteConnection db = new SQLiteConnection(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "SyncedPlaylists.sqlite"));
db.CreateTable<PlaylistItem>();
SyncedPlaylists = db.Table<PlaylistItem>().ToList();
});
foreach (PlaylistItem item in SyncedPlaylists)
{
if (item.YoutubeID != null)
{
DownloadPlaylist(item.Name, item.YoutubeID, false);
}
}
}
#endregion
#region PlaylistsImplementation
@@ -376,6 +414,33 @@ namespace Opus.Api
#endregion
#region Metadata
/// <summary>
/// Return the thumbnail with the greatest quality.
/// </summary>
/// <param name="thumbnails">This array should contains the urls of the thumbnails in this order: MaxResUrl, StandardResUrl, HighResUrl</param>
/// <returns></returns>
public async static Task<string> GetBestThumb(string[] thumbnails)
{
foreach (string thumb in thumbnails)
{
HttpWebRequest request = new HttpWebRequest(new Uri(thumb))
{
Method = "HEAD"
};
try
{
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
if (response.StatusCode == HttpStatusCode.OK)
return thumb;
}
catch (WebException) { }
}
return thumbnails.Last();
}
/// <summary>
/// Return the local path of a youtube video (if downloaded). If the video is not downloaded, return null.
/// </summary>
+517 -585
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -11,6 +11,7 @@ using Android.Support.V7.App;
using Android.Util;
using Android.Views;
using Android.Widget;
using Opus.Api;
using Opus.DataStructure;
using Opus.Others;
using Square.Picasso;
@@ -331,7 +332,7 @@ namespace Opus.Fragments
album.Text = video.Title + " - " + video.Author;
}
ytThumbUri = await MainActivity.GetBestThumb(new string[] { video.Thumbnails.MaxResUrl, video.Thumbnails.StandardResUrl, video.Thumbnails.HighResUrl });
ytThumbUri = await YoutubeManager.GetBestThumb(new string[] { video.Thumbnails.MaxResUrl, video.Thumbnails.StandardResUrl, video.Thumbnails.HighResUrl });
Picasso.With(this).Load(ytThumbUri).Placeholder(Resource.Drawable.noAlbum).Transform(new RemoveBlackBorder(true)).MemoryPolicy(MemoryPolicy.NoCache, MemoryPolicy.NoStore).Into(albumArt);
}
+2 -2
View File
@@ -69,8 +69,8 @@ namespace Opus.Fragments
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
if(useHeader)
CreateHeader();
if (item.SyncState == SyncState.Error)
CreateSyncBanner();
//if (item.SyncState == SyncState.Error)
// CreateSyncBanner();
return view;
}
+369 -381
View File
File diff suppressed because it is too large Load Diff