Skip to content

Commit

Permalink
Android: Fixed back/up navigation of Playlist; it now returns to the …
Browse files Browse the repository at this point in the history
…correct previous activity. Added move icon for movable cells. More work on trying to make cells movable in ListView.

Related to issue #406.
  • Loading branch information
ycastonguay committed Aug 31, 2013
1 parent 5a45830 commit 94debbf
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 85 deletions.
9 changes: 7 additions & 2 deletions MPfm/MPfm.Android/Classes/Activities/PlaylistActivity.cs
Expand Up @@ -43,6 +43,7 @@ public class PlaylistActivity : BaseActivity, IPlaylistView
CustomListView _listView;
PlaylistListAdapter _listAdapter;
Playlist _playlist;
string _sourceActivityType;

protected override void OnCreate(Bundle bundle)
{
Expand All @@ -65,6 +66,9 @@ protected override void OnCreate(Bundle bundle)
_listView.ItemClick += ListViewOnItemClick;
_listView.ItemLongClick += ListViewOnItemLongClick;

// Save the source activity type for later (for providing Up navigation)
_sourceActivityType = Intent.GetStringExtra("sourceActivity");

// Since the onViewReady action could not be added to an intent, tell the NavMgr the view is ready
((AndroidNavigationManager)_navigationManager).SetPlaylistActivityInstance(this);
}
Expand Down Expand Up @@ -139,8 +143,9 @@ public override bool OnOptionsItemSelected(IMenuItem item)
switch (item.ItemId)
{
case global::Android.Resource.Id.Home:
var intent = new Intent(this, typeof (MainActivity));
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
var type = Type.GetType(_sourceActivityType);
var intent = new Intent(this, type);
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
this.StartActivity(intent);
this.Finish();
return true;
Expand Down
48 changes: 25 additions & 23 deletions MPfm/MPfm.Android/Classes/Adapters/PlaylistListAdapter.cs
Expand Up @@ -16,13 +16,9 @@
// along with MPfm. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using Android.App;
using Android.Graphics;
using Android.Views;
using Android.Views.Animations;
using Android.Widget;
using MPfm.Library.Objects;
using MPfm.Sound.AudioFiles;
using MPfm.Sound.Playlists;
using org.sessionsapp.android;
Expand Down Expand Up @@ -79,17 +75,23 @@ public override View GetView(int position, View convertView, ViewGroup parent)
return view;

view.Tag = position;
view.SetOnTouchListener(this);
//view.SetOnTouchListener(this);

var index = view.FindViewById<TextView>(Resource.Id.playlistCell_lblIndex);
var title = view.FindViewById<TextView>(Resource.Id.playlistCell_lblTitle);
var subtitle = view.FindViewById<TextView>(Resource.Id.playlistCell_lblSubtitle);
var imageNowPlaying = view.FindViewById<ImageView>(Resource.Id.playlistCell_imageNowPlaying);

//index.Text = item.AudioFile.TrackNumber.ToString();
index.Text = (position+1).ToString();
title.Text = item.AudioFile.ArtistName + " / " + item.AudioFile.Title;
subtitle.Text = item.AudioFile.Length;

if (item.AudioFile != null && item.AudioFile.Id == _nowPlayingAudioFileId)
imageNowPlaying.Visibility = ViewStates.Visible;
else
imageNowPlaying.Visibility = ViewStates.Gone;

return view;
}

Expand Down Expand Up @@ -214,24 +216,24 @@ public void OnClick(View v)

public bool OnTouch(View v, MotionEvent e)
{
Console.WriteLine("PlaylistListAdapter - OnTouch - action: {0} buttonState: {1} downTime: {2} eventTime: {3} x: {4} y: {5}", e.Action, e.ButtonState, e.DownTime, e.EventTime, e.GetX(), e.GetY());

float x = e.GetX();
float y = e.GetY();

// Keep cancel on top because the flag can contain both move and cancel.
if (e.Action.HasFlag(MotionEventActions.Cancel))
{
Console.WriteLine("PlaylistListAdapter - OnTouch - Cancel - (x,y): ({0},{1})", x, y);
_listView.IsScrollable = true;
}
else if (e.Action.HasFlag(MotionEventActions.Move))
{
Console.WriteLine("PlaylistListAdapter - OnTouch - Move - (x,y): ({0},{1})", x, y);
_listView.IsScrollable = false;
}

//Console.WriteLine("PlaylistListAdapter - OnTouch - Current cell - height: {0} - position: {1}", v.Height, (int)v.Tag);
//Console.WriteLine("PlaylistListAdapter - OnTouch - action: {0} buttonState: {1} downTime: {2} eventTime: {3} x: {4} y: {5}", e.Action, e.ButtonState, e.DownTime, e.EventTime, e.GetX(), e.GetY());

//float x = e.GetX();
//float y = e.GetY();

//// Keep cancel on top because the flag can contain both move and cancel.
//if (e.Action.HasFlag(MotionEventActions.Cancel))
//{
// Console.WriteLine("PlaylistListAdapter - OnTouch - Cancel - (x,y): ({0},{1})", x, y);
// _listView.IsScrollable = true;
//}
//else if (e.Action.HasFlag(MotionEventActions.Move))
//{
// Console.WriteLine("PlaylistListAdapter - OnTouch - Move - (x,y): ({0},{1})", x, y);
// _listView.IsScrollable = false;
//}

////Console.WriteLine("PlaylistListAdapter - OnTouch - Current cell - height: {0} - position: {1}", v.Height, (int)v.Tag);

return true;
}
Expand Down
94 changes: 89 additions & 5 deletions MPfm/MPfm.Android/Classes/Controls/CustomListView.cs
Expand Up @@ -17,6 +17,7 @@

using System;
using Android.Content;
using Android.Graphics;
using Android.Runtime;
using Android.Util;
using Android.Views;
Expand All @@ -30,7 +31,9 @@ namespace org.sessionsapp.android
/// </summary>
public class CustomListView : ListView
{
public bool CanItemsBeMoved { get; set; }
public bool IsScrollable { get; set; }
public bool IsMovingItem { get; private set; }

protected CustomListView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
Expand All @@ -54,17 +57,98 @@ public CustomListView(Context context, IAttributeSet attrs, int defStyle) : base

private void Initialize()
{
Console.WriteLine("CustomListView - Initialize");
IsScrollable = true;
CanItemsBeMoved = true;
IsMovingItem = false;
}

public override bool DispatchTouchEvent(Android.Views.MotionEvent e)
public override bool DispatchTouchEvent(MotionEvent e)
{
// Cancel scrolling
if(!IsScrollable && e.Action.HasFlag(MotionEventActions.Move))
return true;
//Console.WriteLine("CustomListView - DispatchTouchEvent - action: {0} buttonState: {1} downTime: {2} eventTime: {3} x,y: ({4},{5}) width: {6} density: {7} canItemsBeMoved: {8} isMovingItem: {9} isScrollable: {10}", e.Action, e.ButtonState, e.DownTime, e.EventTime, e.GetX(), e.GetY(), Width, Resources.DisplayMetrics.Density, CanItemsBeMoved, IsMovingItem, IsScrollable);
Console.WriteLine("CustomListView - DispatchTouchEvent - action: {0} x,y: ({1},{2})", e.Action, e.GetX(), e.GetY());

float density = Resources.DisplayMetrics.Density;
float x = e.GetX();
float y = e.GetY();

if (CanItemsBeMoved && !IsMovingItem && x >= Width - 48 * density)
{
// The user is trying to move an item using the right hand 'button'.
Console.WriteLine("CustomListView - DispatchTouchEvent - Starting to move item...");
View viewItemMove = GetChildAtPosition(x, y);
if (viewItemMove != null)
{
IsMovingItem = true;
IsScrollable = false;
int tag = -1;
if (viewItemMove.Tag != null)
tag = (int)viewItemMove.Tag;

Console.WriteLine("CustomListView - DispatchTouchEvent - Found moving item! tag: {0}", tag);
}
else
{
Console.WriteLine("CustomListView - DispatchTouchEvent - Did NOT find moving item :-(");
}
}

// Keep cancel on top because the flag can contain both move and cancel.
// if (e.Action.HasFlag(MotionEventActions.Cancel)) // This was cancel when using OnTouchListener on a child view
if(e.Action.HasFlag(MotionEventActions.Up))
{
//Console.WriteLine("CustomListView - DispatchTouchEvent - Up - (x,y): ({0},{1})", x, y);
Console.WriteLine("CustomListView - DispatchTouchEvent - CANCELLING MOVE...");
IsMovingItem = false;
IsScrollable = true;
}
else if (e.Action.HasFlag(MotionEventActions.Move))
{
// Block scroll
if(!IsScrollable)
return true;
}

// Try to find the item over the finger
View view = GetChildAtPosition(x, y);
if (view != null)
{
int tag = -1;
if (view.Tag != null)
tag = (int)view.Tag;

Console.WriteLine("CustomListView - DispatchTouchEvent - Found finger over view! tag: {0}", tag);
}
else
{
Console.WriteLine("CustomListView - DispatchTouchEvent - Did NOT find finger over view :-(");
}

return base.DispatchTouchEvent(e);
}

private View GetChildAtPosition(float x, float y)
{
View returnView = null;
int lastIndex = LastVisiblePosition - FirstVisiblePosition;
for (int a = 0; a < lastIndex; a++)
{
View view = GetChildAt(a);
if (view != null)
{
Rect rect = new Rect();
view.GetHitRect(rect);
bool isOverItem = y >= rect.Top && y <= rect.Bottom;
//Console.WriteLine("CustomListView - GetChildAtPosition - Finding rects - position: {0} hitRect(x,y): ({1},{2})", a, rect.Left, rect.Top);
if (isOverItem)
{
Console.WriteLine("CustomListView - GetChildAtPosition - FOUND CHILD - position: {0} hitRect(x,y): ({1},{2})", a, rect.Left, rect.Top);
returnView = view;
break;
}
}
}

return returnView;
}
}
}
15 changes: 7 additions & 8 deletions MPfm/MPfm.Android/Classes/Navigation/AndroidNavigationManager.cs
Expand Up @@ -244,9 +244,15 @@ protected override void CreateMarkerDetailsViewInternal(IBaseView sourceView, Ac
StartActivity(activity, typeof(MarkerDetailsActivity));
}

protected override void CreatePlaylistViewInternal(IBaseView sourceView, Action<IBaseView> onViewReady)
{
_onPlaylistViewReady = onViewReady;
var activity = GetActivityFromView(sourceView);
StartActivity(activity, typeof(PlaylistActivity));
}

protected override void CreateSyncViewInternal(Action<IBaseView> onViewReady)
{
// TODO: Add source view
_onSyncViewReady = onViewReady;
var intent = new Intent(MainActivity, typeof(SyncActivity));
MainActivity.StartActivity(intent);
Expand All @@ -273,13 +279,6 @@ protected override void CreateSyncDownloadViewInternal(Action<IBaseView> onViewR
MainActivity.StartActivity(intent);
}

protected override void CreatePlaylistViewInternal(Action<IBaseView> onViewReady)
{
_onPlaylistViewReady = onViewReady;
var intent = new Intent(MainActivity, typeof(PlaylistActivity));
MainActivity.StartActivity(intent);
}

public void SetAboutActivityInstance(AboutActivity activity)
{
if (_onAboutViewReady != null)
Expand Down
4 changes: 4 additions & 0 deletions MPfm/MPfm.Android/MPfm.Android.csproj
Expand Up @@ -500,6 +500,10 @@
<AndroidResource Include="Resources\Anim\flipper_changetab_in.xml">
<SubType>Designer</SubType>
</AndroidResource>
<AndroidResource Include="Resources\drawable-hdpi\icon_move.png" />
<AndroidResource Include="Resources\drawable-mdpi\icon_move.png" />
<AndroidResource Include="Resources\drawable-xhdpi\icon_move.png" />
<AndroidResource Include="Resources\drawable-xxhdpi\icon_move.png" />
</ItemGroup>
<ItemGroup>
<Folder Include="Lib\armeabi\" />
Expand Down
13 changes: 6 additions & 7 deletions MPfm/MPfm.Android/Resources/Layout/PlaylistCell.axml
Expand Up @@ -38,13 +38,6 @@
android:textColor="@color/list_secondarytext"
android:textSize="12dp" />
</LinearLayout>
<ImageView
android:id="@+id/playlistCell_imageMove"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="6dp"
android:background="@android:color/transparent"
android:src="@drawable/icon_trash" />
<ImageView
android:id="@+id/playlistCell_imagePlay"
android:layout_width="48dp"
Expand All @@ -69,4 +62,10 @@
android:visibility="gone"
android:background="@android:color/transparent"
android:src="@drawable/icon_speaker" />
<ImageView
android:id="@+id/playlistCell_imageMove"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@android:color/transparent"
android:src="@drawable/icon_move" />
</LinearLayout>

0 comments on commit 94debbf

Please sign in to comment.