public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            if (AppleMusicManager.FetchDeveloperToken() == null)
            {
                var alertController = UIAlertController.Create("Error",
                                                               "No Developer Token was specified. See the README for more information.",
                                                               UIAlertControllerStyle.Alert);
                alertController.AddAction(UIAlertAction.Create("Dismiss", UIAlertActionStyle.Cancel, null));
                PresentViewController(alertController, true, null);
            }
            else if (AuthorizationManager.UserToken == string.Empty)
            {
                var alertController = UIAlertController.Create("Error",
                                                               "No User Token was specified. Request Authorization using the \"Authorization\" tab.",
                                                               UIAlertControllerStyle.Alert);
                alertController.AddAction(UIAlertAction.Create("Dismiss", UIAlertActionStyle.Cancel, null));
                PresentViewController(alertController, true, null);
            }
            else
            {
                Task.Factory.StartNew(async() => await RefreshData());
            }
        }
示例#2
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            appleMusicManager = new AppleMusicManager();
            imageCacheManager = new ImageCacheManager();
            mediaItems        = new MediaItem [0] [];

            // Configure self sizing cells.
            TableView.RowHeight          = UITableView.AutomaticDimension;
            TableView.EstimatedRowHeight = 100;

            // Configure the `UISearchController`.
            searchController = new UISearchController(searchResultsController: null);
            searchController.SearchResultsUpdater             = this;
            searchController.DimsBackgroundDuringPresentation = false;
            searchController.SearchBar.Delegate = this;
            TableView.TableHeaderView           = searchController.SearchBar;
            DefinesPresentationContext          = true;

            /*
             * Add the notification observers needed to respond to events from
             * the `AuthorizationManager`, `MPMediaLibrary` and `UIApplication`.
             * This is so that if the user enables/disables capabilities in the
             * Settings app the application will reflect those changes accurately.
             */
            var notificationCenter = NSNotificationCenter.DefaultCenter;

            authorizationDidUpdateNotificationToken = notificationCenter.AddObserver(AuthorizationManager.AuthorizationDidUpdateNotification,
                                                                                     HandleAuthorizationManagerAuthorizationDidUpdateNotification,
                                                                                     null);
            willEnterForegroundNotificationToken = notificationCenter.AddObserver(UIApplication.WillEnterForegroundNotification,
                                                                                  HandleAuthorizationManagerAuthorizationDidUpdateNotification,
                                                                                  null);
        }
        async Task RefreshData()
        {
            // Your application should handle these errors appropriately depending on the kind of error.
            var items = new MediaItem [0];

            try {
                items = await AppleMusicManager.PerformAppleMusicGetRecentlyPlayedAsync(AuthorizationManager.UserToken);
            } catch (NSErrorException ex) {
                var underlyingError = ex.Error.UserInfo [NSError.UnderlyingErrorKey] as NSError;
                var message         = underlyingError?.LocalizedDescription ?? "Encountered unexpected error.";

                var alertController = UIAlertController.Create("Error", message, UIAlertControllerStyle.Alert);
                alertController.AddAction(UIAlertAction.Create("Dismiss", UIAlertActionStyle.Cancel, null));

                InvokeOnMainThread(() => PresentViewController(alertController, true, null));
            } finally {
                lock (padlock) {
                    mediaItems = items;
                }
                InvokeOnMainThread(() => TableView.ReloadData());
            }
        }
示例#4
0
        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            // Override point for customization after application launch.
            // If not required for your application you can safely delete this method

            AppleMusicManager    = new AppleMusicManager();
            MusicPlayerManager   = new MusicPlayerManager();
            AuthorizationManager = new AuthorizationManager(AppleMusicManager);
            MediaLibraryManager  = new MediaLibraryManager(AuthorizationManager);

            if (TopViewController(0) is AuthorizationTableViewController authorizationTableViewController)
            {
                authorizationTableViewController.AuthorizationManager = AuthorizationManager;
            }
            else
            {
                throw new InvalidCastException($"Unable to find expected {nameof (AuthorizationTableViewController)} in at TabBar Index 0");
            }

            if (TopViewController(1) is PlaylistTableViewController playlistTableViewController)
            {
                playlistTableViewController.AuthorizationManager = AuthorizationManager;
                playlistTableViewController.MediaLibraryManager  = MediaLibraryManager;
                playlistTableViewController.MusicPlayerManager   = MusicPlayerManager;
            }
            else
            {
                throw new InvalidCastException($"Unable to find expected {nameof (PlaylistTableViewController)} in at TabBar Index 1");
            }

            if (TopViewController(2) is PlayerViewController playerViewController)
            {
                playerViewController.MusicPlayerManager = MusicPlayerManager;
            }
            else
            {
                throw new InvalidCastException($"Unable to find expected {nameof (PlayerViewController)} in at TabBar Index 2");
            }

            if (TopViewController(3) is RecentlyPlayedTableViewController recentlyPlayedTableViewController)
            {
                recentlyPlayedTableViewController.AuthorizationManager = AuthorizationManager;
                recentlyPlayedTableViewController.AppleMusicManager    = AppleMusicManager;
                recentlyPlayedTableViewController.MediaLibraryManager  = MediaLibraryManager;
                recentlyPlayedTableViewController.MusicPlayerManager   = MusicPlayerManager;
            }
            else
            {
                throw new InvalidCastException($"Unable to find expected {nameof (RecentlyPlayedTableViewController)} in at TabBar Index 3");
            }

            if (TopViewController(4) is MediaSearchTableViewController mediaSearchTableViewController)
            {
                mediaSearchTableViewController.AuthorizationManager = AuthorizationManager;
                mediaSearchTableViewController.MediaLibraryManager  = MediaLibraryManager;
                mediaSearchTableViewController.MusicPlayerManager   = MusicPlayerManager;
            }
            else
            {
                throw new InvalidCastException($"Unable to find expected {nameof (MediaSearchTableViewController)} in at TabBar Index 4");
            }

            return(true);
        }