/
PlaylistListAdapter.cs
241 lines (207 loc) · 9.87 KB
/
PlaylistListAdapter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright © 2011-2013 Yanick Castonguay
//
// This file is part of MPfm.
//
// MPfm is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// MPfm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MPfm. If not, see <http://www.gnu.org/licenses/>.
using System;
using Android.Views;
using Android.Views.Animations;
using Android.Widget;
using MPfm.Sound.AudioFiles;
using MPfm.Sound.Playlists;
using org.sessionsapp.android;
namespace MPfm.Android.Classes.Adapters
{
public class PlaylistListAdapter : BaseAdapter<AudioFile>, View.IOnClickListener, View.IOnTouchListener
{
readonly PlaylistActivity _context;
readonly CustomListView _listView;
Playlist _playlist;
int _nowPlayingRowPosition;
int _editingRowPosition;
Guid _nowPlayingAudioFileId;
public bool IsEditingRow { get; private set; }
public PlaylistListAdapter(PlaylistActivity context, CustomListView listView, Playlist playlist)
{
_context = context;
_listView = listView;
_playlist = playlist;
}
public void SetData(Playlist playlist)
{
_playlist = playlist;
NotifyDataSetChanged();
}
public override long GetItemId(int position)
{
return position;
}
public override AudioFile this[int position]
{
get { return _playlist.Items[position].AudioFile; }
}
public override int Count
{
get { return _playlist.Items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = _playlist.Items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = _context.LayoutInflater.Inflate(Resource.Layout.PlaylistCell, null);
if (item == null)
return view;
view.Tag = position;
//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;
}
public void SetNowPlayingRow(int position, AudioFile audioFile)
{
int oldPosition = _nowPlayingRowPosition;
_nowPlayingAudioFileId = audioFile == null ? Guid.Empty : audioFile.Id;
_nowPlayingRowPosition = audioFile == null ? -1 : position;
var viewOldPosition = _listView.GetChildAt(oldPosition - _listView.FirstVisiblePosition);
if (viewOldPosition != null)
{
var imageNowPlaying = viewOldPosition.FindViewById<ImageView>(Resource.Id.playlistCell_imageNowPlaying);
imageNowPlaying.Visibility = ViewStates.Gone;
}
var view = _listView.GetChildAt(position - _listView.FirstVisiblePosition);
if (view == null)
return;
if (_playlist.Items[position].AudioFile != null && _playlist.Items[position].AudioFile.Id == _nowPlayingAudioFileId)
{
var imageNowPlaying = view.FindViewById<ImageView>(Resource.Id.playlistCell_imageNowPlaying);
imageNowPlaying.Visibility = ViewStates.Visible;
}
}
public void SetEditingRow(int position)
{
int visibleCellIndex = position - _listView.FirstVisiblePosition;
var view = _listView.GetChildAt(visibleCellIndex);
if (view == null)
return;
var imagePlay = view.FindViewById<ImageView>(Resource.Id.playlistCell_imagePlay);
var imageDelete = view.FindViewById<ImageView>(Resource.Id.playlistCell_imageDelete);
int oldPosition = _editingRowPosition;
_editingRowPosition = position;
if (IsEditingRow && oldPosition == position)
{
// Fade out the controls
Animation anim = AnimationUtils.LoadAnimation(_context, Resource.Animation.listviewoptions_fade_out);
anim.AnimationEnd += (sender, args) =>
{
imagePlay.Visibility = ViewStates.Gone;
imageDelete.Visibility = ViewStates.Gone;
};
imagePlay.StartAnimation(anim);
imageDelete.StartAnimation(anim);
_editingRowPosition = -1;
IsEditingRow = false;
}
else if (IsEditingRow && oldPosition >= 0)
{
// Fade in the new controls
imagePlay.Visibility = ViewStates.Visible;
imageDelete.Visibility = ViewStates.Visible;
Animation anim = AnimationUtils.LoadAnimation(_context, Resource.Animation.listviewoptions_fade_in);
imagePlay.StartAnimation(anim);
imageDelete.StartAnimation(anim);
// Fade out the older controls
int oldPositionVisibleCellIndex = oldPosition - _listView.FirstVisiblePosition;
var viewOldPosition = _listView.GetChildAt(oldPositionVisibleCellIndex);
if (viewOldPosition != null)
{
var imagePlayOld = viewOldPosition.FindViewById<ImageView>(Resource.Id.playlistCell_imagePlay);
var imageDeleteOld = viewOldPosition.FindViewById<ImageView>(Resource.Id.playlistCell_imageDelete);
// Fade out the controls
Animation animOld = AnimationUtils.LoadAnimation(_context, Resource.Animation.listviewoptions_fade_out);
animOld.AnimationEnd += (sender, args) =>
{
imagePlayOld.Visibility = ViewStates.Gone;
imageDeleteOld.Visibility = ViewStates.Gone;
};
imagePlayOld.StartAnimation(animOld);
imageDeleteOld.StartAnimation(animOld);
}
IsEditingRow = true;
}
else if (!IsEditingRow)
{
// Fade in the controls
imagePlay.Visibility = ViewStates.Visible;
imageDelete.Visibility = ViewStates.Visible;
Animation anim = AnimationUtils.LoadAnimation(_context, Resource.Animation.listviewoptions_fade_in);
imagePlay.StartAnimation(anim);
imageDelete.StartAnimation(anim);
IsEditingRow = true;
}
}
public void OnClick(View v)
{
int position = (int)v.Tag;
switch (v.Id)
{
case Resource.Id.playlistCell_imagePlay:
Console.WriteLine("PlaylistCell - Play - position: {0}", position);
//_fragment.OnPlayItem(position);
break;
case Resource.Id.playlistCell_imageDelete:
Console.WriteLine("PlaylistCell - Delete - position: {0}", position);
//AlertDialog ad = new AlertDialog.Builder(_context)
// .SetIconAttribute(global::Android.Resource.Attribute.AlertDialogIcon)
// .SetTitle("Delete confirmation")
// .SetMessage(string.Format("Are you sure you wish to delete {0}?", _items[position].Title))
// .SetCancelable(true)
// .SetPositiveButton("OK", (sender, args) => _fragment.OnDeleteItem(position))
// .SetNegativeButton("Cancel", (sender, args) => { })
// .Create();
//ad.Show();
break;
}
}
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);
return true;
}
}
}