You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
QuickPlay/QuickPlay/MainActivity.cs

140 lines
5.1 KiB
C#

using System;
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Android.Support.V7.App;
using Toolbar = Android.Support.V7.Widget.Toolbar;
using GridLayoutManager = Android.Support.V7.Widget.GridLayoutManager;
namespace QuickPlay
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
private AppConfiguration appConfig;
private Android.Support.V7.Widget.RecyclerView recyclerView;
private IPlayer currentPlayer;
protected override async void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// App initialization
appConfig = AppConfiguration.loadConfiguration();
var playerConfig = await appConfig.GetPlayerConfig();
currentPlayer = playerConfig.GetPlayer();
try
{
await currentPlayer.ConnectAsync();
} catch (CannotConnectException e)
{
//TODO: View a toast with details and change some colors?
var t = Toast.MakeText(this, e.Message + ": " + e.InnerException.Message ?? "", ToastLength.Long);
t.Show();
}
// UI initialization
SetContentView(Resource.Layout.activity_main);
// UI Toolbar initialization
var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionBar.Title = "My Toolbar";
// Hide the play bar by default
var bar = FindViewById(Resource.Id.currentSongBar);
bar.Visibility = ViewStates.Invisible;
// Initialize the RecyclerView
// Since this is rather complicated, it is in a separate method
InitializeRecyclerView();
// FIXME: This should be in OnResume...
// Refresh player info
OnPlayerUpdate();
}
private void InitializeRecyclerView()
{
recyclerView = FindViewById<Android.Support.V7.Widget.RecyclerView>(Resource.Id.recyclerView1);
var layoutStrategy = new LexicographicLayoutStrategy();
var adapter = new SongRecyclerAdapter(currentPlayer, layoutStrategy);
adapter.SongClick += OnSongClick;
recyclerView.SetAdapter(adapter);
var layoutManager = new GridLayoutManager(this, 2, GridLayoutManager.Vertical, false);
recyclerView.SetLayoutManager(layoutManager);
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu_main, menu);
return base.OnCreateOptionsMenu(menu);
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (item.ItemId == Resource.Id.action_settings)
{
// Show the play bar
var bar = FindViewById(Resource.Id.currentSongBar);
bar.Visibility = ViewStates.Visible;
}
if (item.ItemId == Resource.Id.action_edit)
{
// Hide the play bar
var bar = FindViewById(Resource.Id.currentSongBar);
bar.Visibility = ViewStates.Invisible;
}
return base.OnOptionsItemSelected(item);
}
/// <summary>
/// A callback for all player updates.
///
/// The update should be easy on resources, so no need to have separate
/// partial updates. This simply performs full update of the activity
/// state and views.
/// </summary>
public void OnPlayerUpdate()
{
TextView playerName = FindViewById<TextView>(Resource.Id.playerNameText);
playerName.Text = currentPlayer.PlayerName;
var bar = FindViewById(Resource.Id.currentSongBar);
switch (currentPlayer.Status)
{
case IPlayer.PlayerStatus.Disconnected:
Toolbar tb = FindViewById<Toolbar>(Resource.Id.toolbar);
tb.SetBackgroundColor(Android.Graphics.Color.Red);
bar.Visibility = ViewStates.Invisible;
break;
case IPlayer.PlayerStatus.Playing:
ResetToolbarColor();
// Show progress bar
bar.Visibility = ViewStates.Visible;
break;
case IPlayer.PlayerStatus.Stopped:
ResetToolbarColor();
bar.Visibility = ViewStates.Invisible;
break;
}
}
private void ResetToolbarColor()
{
Toolbar tb = FindViewById<Toolbar>(Resource.Id.toolbar);
tb.SetBackgroundColor(
new Android.Graphics.Color(
Android.Support.V4.Content.ContextCompat.GetColor(this, Resource.Color.colorPrimary)
)
);
}
public async void OnSongClick(object sender, IPlayable song)
{
await currentPlayer.Play(song);
}
}
}