diff --git a/MusicApp/MainActivity.cs b/MusicApp/MainActivity.cs index 6be0e76..ef5d43c 100644 --- a/MusicApp/MainActivity.cs +++ b/MusicApp/MainActivity.cs @@ -36,6 +36,7 @@ namespace MusicApp public IMenu menu; public SwipeRefreshLayout contentRefresh; public SwipeRefreshLayout pagerRefresh; + public bool usePager; private Handler handler = new Handler(); private ProgressBar bar; @@ -179,10 +180,18 @@ namespace MusicApp { get { - return instance.SupportActionBar.Height; + return 0/*instance.SupportActionBar.Height*/; } } + public void Scroll(object sender, AbsListView.ScrollEventArgs e) + { + if (usePager) + pagerRefresh.SetEnabled(e.FirstVisibleItem == 0); + else + contentRefresh.SetEnabled(e.FirstVisibleItem == 0); + } + protected override void OnCreate(Bundle savedInstanceState) { @@ -450,6 +459,7 @@ namespace MusicApp Console.WriteLine("Switching: " + canSwitch); canSwitch = false; + usePager = true; TabLayout tabs = FindViewById(Resource.Id.tabs); ViewPager pager = FindViewById(Resource.Id.pager); @@ -469,12 +479,11 @@ namespace MusicApp oldAdapter.AddFragment(FolderBrowse.NewInstance(), "Folders"); pager.Adapter = oldAdapter; - pager.ClearOnPageChangeListeners(); } else { - FrameLayout frame = FindViewById(Resource.Id.contentView); - frame.Visibility = ViewStates.Gone; + contentRefresh.Visibility = ViewStates.Gone; + pagerRefresh.Visibility = ViewStates.Visible; tabs.Visibility = ViewStates.Visible; tabs.AddTab(tabs.NewTab().SetText("Songs")); tabs.AddTab(tabs.NewTab().SetText("Folders")); @@ -486,10 +495,12 @@ namespace MusicApp adapter.AddFragment(FolderBrowse.NewInstance(), "Folders"); pager.Adapter = adapter; + pager.AddOnPageChangeListener(this); + pager.AddOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs)); + tabs.SetupWithViewPager(pager); } - pager.AddOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs)); pager.CurrentItem = selectedTab; tabs.SetScrollPosition(selectedTab, 0f, true); @@ -509,6 +520,7 @@ namespace MusicApp Console.WriteLine("Switching: " + canSwitch); canSwitch = false; + usePager = true; TabLayout tabs = FindViewById(Resource.Id.tabs); ViewPager pager = FindViewById(Resource.Id.pager); @@ -528,12 +540,11 @@ namespace MusicApp oldAdapter.AddFragment(YtPlaylist.NewInstance(), "Youtube playlists"); pager.Adapter = oldAdapter; - pager.ClearOnPageChangeListeners(); } else { - FrameLayout frame = FindViewById(Resource.Id.contentView); - frame.Visibility = ViewStates.Gone; + contentRefresh.Visibility = ViewStates.Gone; + pagerRefresh.Visibility = ViewStates.Visible; tabs.Visibility = ViewStates.Visible; tabs.AddTab(tabs.NewTab().SetText("Playlists")); tabs.AddTab(tabs.NewTab().SetText("Youtube playlists")); @@ -545,11 +556,11 @@ namespace MusicApp adapter.AddFragment(YtPlaylist.NewInstance(), "Youtube playlists"); pager.Adapter = adapter; + pager.AddOnPageChangeListener(this); + pager.AddOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs)); tabs.SetupWithViewPager(pager); } - pager.AddOnPageChangeListener(this); - pager.AddOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabs)); pager.CurrentItem = selectedTab; tabs.SetScrollPosition(selectedTab, 0f, true); @@ -562,7 +573,10 @@ namespace MusicApp canSwitch = true; } - public void OnPageScrollStateChanged(int state) { } + public void OnPageScrollStateChanged(int state) + { + pagerRefresh.SetEnabled( state == ViewPager.ScrollStateIdle ); + } public void OnPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @@ -576,6 +590,9 @@ namespace MusicApp Playlist.instance.AddEmptyView(); if (YtPlaylist.instance.isEmpty) YtPlaylist.instance.RemoveEmptyView(); + + Playlist.instance.focused = true; + YtPlaylist.instance.focused = false; } if (position == 1) { @@ -583,16 +600,33 @@ namespace MusicApp Playlist.instance.RemoveEmptyView(); if (YtPlaylist.instance.isEmpty) YtPlaylist.instance.AddEmptyView(); + + Playlist.instance.focused = false; + YtPlaylist.instance.focused = true; } } - Console.WriteLine("Browse switched" + FolderBrowse.instance.populated); - if (Browse.instance != null && !FolderBrowse.instance.populated) - FolderBrowse.instance.PopulateList(); + if (Browse.instance != null) + { + if(!FolderBrowse.instance.populated) + FolderBrowse.instance.PopulateList(); + + if(position == 0) + { + Browse.instance.focused = true; + FolderBrowse.instance.focused = false; + } + if(position == 1) + { + Browse.instance.focused = false; + FolderBrowse.instance.focused = true; + } + } } public void HideTabs() { + usePager = false; TabLayout tabs = FindViewById(Resource.Id.tabs); tabs.RemoveAllTabs(); tabs.Visibility = ViewStates.Gone; @@ -608,8 +642,8 @@ namespace MusicApp pager.Adapter = null; } - FrameLayout frame = FindViewById(Resource.Id.contentView); - frame.Visibility = ViewStates.Visible; + pagerRefresh.Visibility = ViewStates.Gone; + contentRefresh.Visibility = ViewStates.Visible; } public void PrepareSmallPlayer() @@ -748,7 +782,10 @@ namespace MusicApp public void Transition(int Resource, Android.Support.V4.App.Fragment fragment, bool backStack) { - SupportFragmentManager.BeginTransaction().Replace(Resource, fragment).AddToBackStack(null).Commit(); + if(backStack) + SupportFragmentManager.BeginTransaction().Replace(Resource, fragment).AddToBackStack(null).Commit(); + else + SupportFragmentManager.BeginTransaction().Replace(Resource, fragment).Commit(); } } } \ No newline at end of file diff --git a/MusicApp/Resources/Portable Class/Browse.cs b/MusicApp/Resources/Portable Class/Browse.cs index 54ac3d7..c84d72e 100644 --- a/MusicApp/Resources/Portable Class/Browse.cs +++ b/MusicApp/Resources/Portable Class/Browse.cs @@ -7,12 +7,13 @@ using Android.Support.V4.Widget; using Android.Support.V7.App; using Android.Views; using Android.Widget; +using Java.Lang; using MusicApp.Resources.values; using System.Collections.Generic; namespace MusicApp.Resources.Portable_Class { - public class Browse : ListFragment, SwipeRefreshLayout.IOnRefreshListener + public class Browse : ListFragment { public static Browse instance; public static Context act; @@ -21,6 +22,7 @@ namespace MusicApp.Resources.Portable_Class public List result; public Adapter adapter; public View emptyView; + public bool focused = true; private View view; private string[] actions = new string[] { "Play", "Play Next", "Play Last", "Add To Playlist" }; @@ -34,7 +36,8 @@ namespace MusicApp.Resources.Portable_Class inflater = LayoutInflater; emptyView = LayoutInflater.Inflate(Resource.Layout.NoSong, null); ListView.EmptyView = emptyView; - MainActivity.instance.pagerRefresh.SetOnRefreshListener(this); + MainActivity.instance.pagerRefresh.Refresh += OnRefresh; + ListView.Scroll += MainActivity.instance.Scroll; if (ListView.Adapter == null) MainActivity.instance.GetStoragePermission(); @@ -42,6 +45,7 @@ namespace MusicApp.Resources.Portable_Class public override void OnDestroy() { + MainActivity.instance.pagerRefresh.Refresh -= OnRefresh; if (isEmpty) { ViewGroup rootView = Activity.FindViewById(Android.Resource.Id.Content); @@ -74,7 +78,6 @@ namespace MusicApp.Resources.Portable_Class CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null); ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground(); - if (musicCursor != null && musicCursor.MoveToFirst()) { int titleID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Title); @@ -117,9 +120,60 @@ namespace MusicApp.Resources.Portable_Class } } + private void OnRefresh(object sender, System.EventArgs e) + { + if (!focused) + return; + Refresh(); + MainActivity.instance.pagerRefresh.Refreshing = false; + } + public void Refresh() { + musicList.Clear(); + Android.Net.Uri musicUri = MediaStore.Audio.Media.ExternalContentUri; + + CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null); + ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground(); + + if (musicCursor != null && musicCursor.MoveToFirst()) + { + int titleID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Title); + int artistID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Artist); + int albumID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Album); + int thisID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Id); + int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data); + do + { + string Artist = musicCursor.GetString(artistID); + string Title = musicCursor.GetString(titleID); + string Album = musicCursor.GetString(albumID); + long AlbumArt = musicCursor.GetLong(musicCursor.GetColumnIndex(MediaStore.Audio.Albums.InterfaceConsts.AlbumId)); + long id = musicCursor.GetLong(thisID); + string path = musicCursor.GetString(pathID); + + if (Title == null) + Title = "Unknown Title"; + if (Artist == null) + Artist = "Unknow Artist"; + if (Album == null) + Album = "Unknow Album"; + + musicList.Add(new Song(Title, Artist, Album, AlbumArt, id, path)); + } + while (musicCursor.MoveToNext()); + musicCursor.Close(); + } + + adapter = new Adapter(Android.App.Application.Context, Resource.Layout.SongList, musicList); + ListAdapter = adapter; + + if (adapter == null || adapter.Count == 0) + { + isEmpty = true; + Activity.AddContentView(emptyView, View.LayoutParameters); + } } public void Search(string search) @@ -306,13 +360,5 @@ namespace MusicApp.Resources.Portable_Class AddToPlaylist(item, playList, playlistID); } - - public void OnRefresh() - - - { - System.Console.WriteLine("Refreshing"); - Refresh(); //Handle refresh things (like iding the progress rotator) - } } } \ No newline at end of file diff --git a/MusicApp/Resources/Portable Class/FolderBrowse.cs b/MusicApp/Resources/Portable Class/FolderBrowse.cs index d482ee0..dd33376 100644 --- a/MusicApp/Resources/Portable Class/FolderBrowse.cs +++ b/MusicApp/Resources/Portable Class/FolderBrowse.cs @@ -24,6 +24,7 @@ namespace MusicApp.Resources.Portable_Class public ArrayAdapter adapter; public View emptyView; public bool populated = false; + public bool focused = false; private View view; private string[] actions = new string[] { "List songs", "Add To Playlist", "Random Play" }; @@ -37,13 +38,16 @@ namespace MusicApp.Resources.Portable_Class inflater = LayoutInflater; emptyView = LayoutInflater.Inflate(Resource.Layout.NoSong, null); ListView.EmptyView = emptyView; + ListView.Scroll += MainActivity.instance.Scroll; + MainActivity.instance.pagerRefresh.Refresh += OnRefresh; - if(ListView.Adapter == null) + if (ListView.Adapter == null) PopulateList(); } public override void OnDestroy() { + MainActivity.instance.pagerRefresh.Refresh -= OnRefresh; if (isEmpty) { ViewGroup rootView = Activity.FindViewById(Android.Resource.Id.Content); @@ -117,6 +121,58 @@ namespace MusicApp.Resources.Portable_Class } } + private void OnRefresh(object sender, System.EventArgs e) + { + if (!focused) + return; + Refresh(); + MainActivity.instance.pagerRefresh.Refreshing = false; + } + + public void Refresh() + { + Uri musicUri = MediaStore.Audio.Media.ExternalContentUri; + + CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null); + ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground(); + + paths.Clear(); + pathDisplay.Clear(); + pathUse.Clear(); + + + if (musicCursor != null && musicCursor.MoveToFirst()) + { + int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data); + do + { + string path = musicCursor.GetString(pathID); + path = path.Substring(0, path.LastIndexOf("/")); + string displayPath = path.Substring(path.LastIndexOf("/") + 1, path.Length - (path.LastIndexOf("/") + 1)); + + if (!paths.Contains(path)) + { + pathDisplay.Add(displayPath); + paths.Add(path); + pathUse.Add(1); + } + else + pathUse[paths.IndexOf(path)] += 1; + } + while (musicCursor.MoveToNext()); + musicCursor.Close(); + } + + adapter = new TwoLineAdapter(Android.App.Application.Context, Resource.Layout.TwoLineLayout, pathDisplay, pathUse); + ListAdapter = adapter; + + if (adapter == null || adapter.Count == 0) + { + isEmpty = true; + Activity.AddContentView(emptyView, View.LayoutParameters); + } + } + public void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) { ListSongs(pathDisplay[e.Position], paths[e.Position]); diff --git a/MusicApp/Resources/Portable Class/FolderTracks.cs b/MusicApp/Resources/Portable Class/FolderTracks.cs index dcb307e..717f525 100644 --- a/MusicApp/Resources/Portable Class/FolderTracks.cs +++ b/MusicApp/Resources/Portable Class/FolderTracks.cs @@ -30,6 +30,8 @@ namespace MusicApp.Resources.Portable_Class base.OnActivityCreated(savedInstanceState); emptyView = LayoutInflater.Inflate(Resource.Layout.NoPlaylist, null); ListView.EmptyView = emptyView; + ListView.Scroll += MainActivity.instance.Scroll; + MainActivity.instance.contentRefresh.Refresh += OnRefresh; PopulateList(); MainActivity.instance.DisplaySearch(1); @@ -37,6 +39,7 @@ namespace MusicApp.Resources.Portable_Class public override void OnDestroy() { + MainActivity.instance.contentRefresh.Refresh -= OnRefresh; instance = null; base.OnDestroy(); } @@ -90,6 +93,7 @@ namespace MusicApp.Resources.Portable_Class if (Album == null) Album = "Unknow Album"; + System.Console.WriteLine(Title); tracks.Add(new Song(Title, Artist, Album, AlbumArt, id, path)); } while (musicCursor.MoveToNext()); @@ -109,6 +113,60 @@ namespace MusicApp.Resources.Portable_Class } } + private void OnRefresh(object sender, System.EventArgs e) + { + tracks.Clear(); + + Uri musicUri = MediaStore.Audio.Media.GetContentUriForPath(path); + + CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null); + ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground(); + + + if (musicCursor != null && musicCursor.MoveToFirst()) + { + int titleID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Title); + int artistID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Artist); + int albumID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Album); + int thisID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Id); + int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data); + do + { + string path = musicCursor.GetString(pathID); + + if (!path.Contains(this.path)) + continue; + + string Artist = musicCursor.GetString(artistID); + string Title = musicCursor.GetString(titleID); + string Album = musicCursor.GetString(albumID); + long AlbumArt = musicCursor.GetLong(musicCursor.GetColumnIndex(MediaStore.Audio.Albums.InterfaceConsts.AlbumId)); + long id = musicCursor.GetLong(thisID); + + if (Title == null) + Title = "Unknown Title"; + if (Artist == null) + Artist = "Unknow Artist"; + if (Album == null) + Album = "Unknow Album"; + + tracks.Add(new Song(Title, Artist, Album, AlbumArt, id, path)); + } + while (musicCursor.MoveToNext()); + musicCursor.Close(); + } + + adapter = new Adapter(Android.App.Application.Context, Resource.Layout.SongList, tracks); + ListAdapter = adapter; + + if (adapter == null || adapter.Count == 0) + { + isEmpty = true; + Activity.AddContentView(emptyView, View.LayoutParameters); + } + MainActivity.instance.contentRefresh.Refreshing = false; + } + public void Search(string search) { result = new List(); diff --git a/MusicApp/Resources/Portable Class/Playlist.cs b/MusicApp/Resources/Portable Class/Playlist.cs index 109b8cb..bcee234 100644 --- a/MusicApp/Resources/Portable Class/Playlist.cs +++ b/MusicApp/Resources/Portable Class/Playlist.cs @@ -18,6 +18,7 @@ namespace MusicApp.Resources.Portable_Class public Adapter adapter; public View emptyView; public bool isEmpty = false; + public bool focused = true; private List playList = new List(); private List playListCount = new List(); @@ -33,6 +34,8 @@ namespace MusicApp.Resources.Portable_Class ListView.TextFilterEnabled = true; ListView.ItemClick += ListView_ItemClick; ListView.ItemLongClick += ListView_ItemLongClick; + ListView.Scroll += MainActivity.instance.Scroll; + MainActivity.instance.pagerRefresh.Refresh += OnRefresh; if (ListView.Adapter == null) MainActivity.instance.GetStoragePermission(); @@ -54,6 +57,7 @@ namespace MusicApp.Resources.Portable_Class public override void OnDestroy() { + MainActivity.instance.pagerRefresh.Refresh -= OnRefresh; if (isEmpty) { ViewGroup rootView = Activity.FindViewById(Android.Resource.Id.Content); @@ -79,6 +83,9 @@ namespace MusicApp.Resources.Portable_Class public void PopulateView() { + playList.Clear(); + playlistId.Clear(); + Uri uri = Playlists.ExternalContentUri; CursorLoader loader = new CursorLoader(Android.App.Application.Context, uri, null, null, null, null); ICursor cursor = (ICursor)loader.LoadInBackground(); @@ -113,9 +120,17 @@ namespace MusicApp.Resources.Portable_Class } } + private void OnRefresh(object sender, System.EventArgs e) + { + if (!focused) + return; + Refresh(); + MainActivity.instance.pagerRefresh.Refreshing = false; + } + public void Refresh() { - + PopulateView(); } private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) diff --git a/MusicApp/Resources/Portable Class/PlaylistTracks.cs b/MusicApp/Resources/Portable Class/PlaylistTracks.cs index 3028049..b9c07ab 100644 --- a/MusicApp/Resources/Portable Class/PlaylistTracks.cs +++ b/MusicApp/Resources/Portable Class/PlaylistTracks.cs @@ -34,7 +34,8 @@ namespace MusicApp.Resources.Portable_Class base.OnActivityCreated(savedInstanceState); emptyView = LayoutInflater.Inflate(Resource.Layout.NoPlaylist, null); ListView.EmptyView = emptyView; - ListAdapter = adapter; + ListView.Scroll += MainActivity.instance.Scroll; + MainActivity.instance.contentRefresh.Refresh += OnRefresh; PopulateList(); MainActivity.instance.DisplaySearch(1); @@ -42,6 +43,7 @@ namespace MusicApp.Resources.Portable_Class public override void OnDestroy() { + MainActivity.instance.contentRefresh.Refresh -= OnRefresh; base.OnDestroy(); instance = null; } @@ -156,6 +158,82 @@ namespace MusicApp.Resources.Portable_Class } } + private async void OnRefresh(object sender, System.EventArgs e) + { + tracks.Clear(); + if (playlistId != 0) + { + Uri musicUri = MediaStore.Audio.Playlists.Members.GetContentUri("external", playlistId); + + CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null); + ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground(); + + + if (musicCursor != null && musicCursor.MoveToFirst()) + { + int titleID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Title); + int artistID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Artist); + int albumID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Album); + int thisID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Id); + int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data); + do + { + string Artist = musicCursor.GetString(artistID); + string Title = musicCursor.GetString(titleID); + string Album = musicCursor.GetString(albumID); + long AlbumArt = musicCursor.GetLong(musicCursor.GetColumnIndex(MediaStore.Audio.Albums.InterfaceConsts.AlbumId)); + long id = musicCursor.GetLong(thisID); + string path = musicCursor.GetString(pathID); + + if (Title == null) + Title = "Unknown Title"; + if (Artist == null) + Artist = "Unknow Artist"; + if (Album == null) + Album = "Unknow Album"; + + tracks.Add(new Song(Title, Artist, Album, AlbumArt, id, path)); + } + while (musicCursor.MoveToNext()); + musicCursor.Close(); + } + + adapter = new Adapter(Android.App.Application.Context, Resource.Layout.SongList, tracks); + ListAdapter = adapter; + } + else if (ytID != null) + { + string nextPageToken = ""; + while (nextPageToken != null) + { + var ytPlaylistRequest = YoutubeEngine.youtubeService.PlaylistItems.List("snippet, contentDetails"); + ytPlaylistRequest.PlaylistId = ytID; + ytPlaylistRequest.MaxResults = 50; + ytPlaylistRequest.PageToken = nextPageToken; + + var ytPlaylist = await ytPlaylistRequest.ExecuteAsync(); + + foreach (var item in ytPlaylist.Items) + { + Song song = new Song(item.Snippet.Title, item.Snippet.ChannelTitle, item.Snippet.Thumbnails.Default__.Url, -1, -1, item.ContentDetails.VideoId, true); + tracks.Add(song); + ytTracksIDs.Add(item.Id); + } + + nextPageToken = ytPlaylist.NextPageToken; + } + } + + adapter = new Adapter(Android.App.Application.Context, Resource.Layout.SongList, tracks); + ListAdapter = adapter; + + if (adapter == null || adapter.Count == 0) + { + isEmpty = true; + Activity.AddContentView(emptyView, View.LayoutParameters); + } + } + public void Search(string search) { result = new List(); diff --git a/MusicApp/Resources/Portable Class/Preferences.cs b/MusicApp/Resources/Portable Class/Preferences.cs index 5852e1b..c626629 100644 --- a/MusicApp/Resources/Portable Class/Preferences.cs +++ b/MusicApp/Resources/Portable Class/Preferences.cs @@ -1,15 +1,13 @@ -using Android.Preferences; -using Android.Support.V7.App; -using Android.Views; +using Android.App; +using Android.Content; using Android.OS; +using Android.Preferences; +using Android.Views; +using Android.Widget; +using Java.IO; +using MusicApp.Resources.values; using System; using System.Collections.Generic; -using MusicApp.Resources.values; -using Java.IO; -using Android.Widget; -using Android.Content; -using Android.App; - using AlertDialog = Android.Support.V7.App.AlertDialog; using Toolbar = Android.Support.V7.Widget.Toolbar; @@ -65,7 +63,7 @@ namespace MusicApp.Resources.Portable_Class public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = base.OnCreateView(inflater, container, savedInstanceState); - view.SetPadding(0, MainActivity.paddinTop, 0, 0); + view.SetPadding(0, MainActivity.instance.SupportActionBar.Height, 0, 0); return view; } diff --git a/MusicApp/Resources/Portable Class/Queue.cs b/MusicApp/Resources/Portable Class/Queue.cs index 3f80a47..003cf91 100644 --- a/MusicApp/Resources/Portable Class/Queue.cs +++ b/MusicApp/Resources/Portable Class/Queue.cs @@ -21,12 +21,15 @@ namespace MusicApp.Resources.Portable_Class emptyView = LayoutInflater.Inflate(Resource.Layout.NoQueue, null); ListView.EmptyView = emptyView; + ListView.Scroll += MainActivity.instance.Scroll; + MainActivity.instance.contentRefresh.Refresh += OnRefresh; PopulateView(); } public override void OnDestroy() { + MainActivity.instance.contentRefresh.Refresh -= OnRefresh; if (isEmpty) { ViewGroup rootView = Activity.FindViewById(Android.Resource.Id.Content); @@ -53,13 +56,12 @@ namespace MusicApp.Resources.Portable_Class void PopulateView() { adapter = new Adapter(Android.App.Application.Context, Resource.Layout.SongList, MusicPlayer.queue); - ListAdapter = adapter; + ListView.TextFilterEnabled = true; ListView.ItemClick += ListView_ItemClick; ListView.ItemLongClick += ListView_ItemLongClick; - if (adapter == null || adapter.Count == 0) { isEmpty = true; @@ -67,9 +69,22 @@ namespace MusicApp.Resources.Portable_Class } } + private void OnRefresh(object sender, System.EventArgs e) + { + Refresh(); + MainActivity.instance.contentRefresh.Refreshing = false; + } + public void Refresh() { + adapter = new Adapter(Android.App.Application.Context, Resource.Layout.SongList, MusicPlayer.queue); + ListAdapter = adapter; + if (adapter == null || adapter.Count == 0) + { + isEmpty = true; + Activity.AddContentView(emptyView, View.LayoutParameters); + } } private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) diff --git a/MusicApp/Resources/Portable Class/YoutubeEngine.cs b/MusicApp/Resources/Portable Class/YoutubeEngine.cs index 2bf1f46..ae169cf 100644 --- a/MusicApp/Resources/Portable Class/YoutubeEngine.cs +++ b/MusicApp/Resources/Portable Class/YoutubeEngine.cs @@ -27,6 +27,7 @@ namespace MusicApp.Resources.Portable_Class private View emptyView; private bool isEmpty = true; private string[] actions = new string[] { "Play", "Play Next", "Play Last", "Add To Playlist", "Download" }; + private string searchKeyWorld; public override void OnActivityCreated(Bundle savedInstanceState) { @@ -34,6 +35,7 @@ namespace MusicApp.Resources.Portable_Class ListView.ItemClick += ListView_ItemClick; ListView.ItemLongClick += ListView_ItemLongClick; + ListView.Scroll += MainActivity.instance.Scroll; emptyView = LayoutInflater.Inflate(Resource.Layout.DownloadLayout, null); ListView.EmptyView = emptyView; @@ -49,10 +51,13 @@ namespace MusicApp.Resources.Portable_Class if(youtubeService == null) MainActivity.instance.Login(); + + MainActivity.instance.contentRefresh.Refresh += OnRefresh; } public override void OnDestroy() { + MainActivity.instance.contentRefresh.Refresh -= OnRefresh; if (isEmpty) { ViewGroup rootView = Activity.FindViewById(Android.Resource.Id.Content); @@ -75,11 +80,13 @@ namespace MusicApp.Resources.Portable_Class return instance; } - public async void Search(string search) + public async Task Search(string search) { if (search == null || search == "") return; + searchKeyWorld = search; + if (MainActivity.instance.TokenHasExpire()) { youtubeService = null; @@ -108,9 +115,15 @@ namespace MusicApp.Resources.Portable_Class ListAdapter = new Adapter(Android.App.Application.Context, Resource.Layout.SongList, result); } - public void Refresh() + private async void OnRefresh(object sender, System.EventArgs e) { + await Refresh(); + MainActivity.instance.contentRefresh.Refreshing = false; + } + public async Task Refresh() + { + await Search(searchKeyWorld); } private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) diff --git a/MusicApp/Resources/Portable Class/YtPlaylists.cs b/MusicApp/Resources/Portable Class/YtPlaylists.cs index 737d2cc..8df7511 100644 --- a/MusicApp/Resources/Portable Class/YtPlaylists.cs +++ b/MusicApp/Resources/Portable Class/YtPlaylists.cs @@ -18,6 +18,7 @@ namespace MusicApp.Resources.Portable_Class public Adapter adapter; public View emptyView; public bool isEmpty = false; + public bool focused = false; private List playlists = new List(); private List YtPlaylists = new List(); @@ -29,6 +30,8 @@ namespace MusicApp.Resources.Portable_Class base.OnActivityCreated(savedInstanceState); emptyView = LayoutInflater.Inflate(Resource.Layout.NoYtPlaylist, null); ListView.EmptyView = emptyView; + ListView.Scroll += MainActivity.instance.Scroll; + MainActivity.instance.pagerRefresh.Refresh += OnRefresh; if (YoutubeEngine.youtubeService == null) MainActivity.instance.Login(); @@ -49,6 +52,7 @@ namespace MusicApp.Resources.Portable_Class public override void OnDestroy() { + MainActivity.instance.pagerRefresh.Refresh -= OnRefresh; if (isEmpty) { ViewGroup rootView = Activity.FindViewById(Android.Resource.Id.Content); @@ -144,6 +148,85 @@ namespace MusicApp.Resources.Portable_Class } } + private async void OnRefresh(object sender, System.EventArgs e) + { + await Refresh(); + MainActivity.instance.pagerRefresh.Refreshing = false; + } + + private async Task Refresh() + { + if (MainActivity.instance.TokenHasExpire()) + { + YoutubeEngine.youtubeService = null; + MainActivity.instance.Login(); + + while (YoutubeEngine.youtubeService == null) + await Task.Delay(500); + } + + HashMap parameters = new HashMap(); + parameters.Put("part", "snippet,contentDetails"); + parameters.Put("mine", "true"); + parameters.Put("maxResults", "25"); + parameters.Put("onBehalfOfContentOwner", ""); + parameters.Put("onBehalfOfContentOwnerChannel", ""); + + YouTubeService youtube = YoutubeEngine.youtubeService; + + PlaylistsResource.ListRequest ytPlaylists = youtube.Playlists.List(parameters.Get("part").ToString()); + + if (parameters.ContainsKey("mine") && parameters.Get("mine").ToString() != "") + { + bool mine = (parameters.Get("mine").ToString() == "true") ? true : false; + ytPlaylists.Mine = mine; + } + + if (parameters.ContainsKey("maxResults")) + { + ytPlaylists.MaxResults = long.Parse(parameters.Get("maxResults").ToString()); + } + + if (parameters.ContainsKey("onBehalfOfContentOwner") && parameters.Get("onBehalfOfContentOwner").ToString() != "") + { + ytPlaylists.OnBehalfOfContentOwner = parameters.Get("onBehalfOfContentOwner").ToString(); + } + + if (parameters.ContainsKey("onBehalfOfContentOwnerChannel") && parameters.Get("onBehalfOfContentOwnerChannel").ToString() != "") + { + ytPlaylists.OnBehalfOfContentOwnerChannel = parameters.Get("onBehalfOfContentOwnerChannel").ToString(); + } + + PlaylistListResponse response = await ytPlaylists.ExecuteAsync(); + + if (instance == null) + return; + + playlists = new List(); + playlists.Clear(); + + for (int i = 0; i < response.Items.Count; i++) + { + Google.Apis.YouTube.v3.Data.Playlist playlist = response.Items[i]; + YtPlaylists.Add(playlist); + Song song = new Song(playlist.Snippet.Title, playlist.Snippet.ChannelTitle, playlist.Snippet.Thumbnails.Default__.Url, -1, -1, playlist.Id, true); + playlists.Add(song); + } + + adapter = new Adapter(Android.App.Application.Context, Resource.Layout.SongList, playlists); + ListAdapter = adapter; + + if (adapter == null || adapter.Count == 0) + { + if (isEmpty) + return; + isEmpty = true; + Activity.AddContentView(emptyView, View.LayoutParameters); + } + + System.Console.WriteLine("Refreshing done"); + } + private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) { AppCompatActivity act = (AppCompatActivity)Activity; diff --git a/MusicApp/Resources/layout/Main.axml b/MusicApp/Resources/layout/Main.axml index 5051be2..eb55ae7 100644 --- a/MusicApp/Resources/layout/Main.axml +++ b/MusicApp/Resources/layout/Main.axml @@ -1,11 +1,12 @@ - + android:orientation="vertical" + android:fitsSystemWindows="true" > - - - - + - + - + + + + - - - + android:background="@android:color/white" + android:elevation="12dp" + android:layout_alignParentBottom="true"> + + + + + - - \ No newline at end of file + + \ No newline at end of file