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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
package github.daneren2005.dsub.fragments;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import github.daneren2005.dsub.R;
import github.daneren2005.dsub.adapter.ArtistAdapter;
import github.daneren2005.dsub.adapter.SectionAdapter;
import github.daneren2005.dsub.domain.Artist;
import github.daneren2005.dsub.domain.Indexes;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.domain.MusicDirectory.Entry;
import github.daneren2005.dsub.domain.MusicFolder;
import github.daneren2005.dsub.domain.ServerInfo;
import github.daneren2005.dsub.service.MusicService;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.ProgressListener;
import github.daneren2005.dsub.util.Util;
import github.daneren2005.dsub.view.UpdateView;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class SelectArtistFragment extends SelectRecyclerFragment<Serializable> implements ArtistAdapter.OnMusicFolderChanged {
private static final String TAG = SelectArtistFragment.class.getSimpleName();
private List<MusicFolder> musicFolders = null;
private List<Entry> entries;
private String groupId;
private String groupName;
public SelectArtistFragment() {
super();
}
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if(bundle != null) {
musicFolders = (List<MusicFolder>) bundle.getSerializable(Constants.FRAGMENT_LIST2);
}
artist = true;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(Constants.FRAGMENT_LIST2, (Serializable) musicFolders);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
Bundle args = getArguments();
if(args != null) {
if(args.getBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, false)) {
groupId = args.getString(Constants.INTENT_EXTRA_NAME_ID);
groupName = args.getString(Constants.INTENT_EXTRA_NAME_NAME);
if (groupName != null) {
setTitle(groupName);
context.invalidateOptionsMenu();
}
}
}
super.onCreateView(inflater, container, bundle);
return rootView;
}
@Override
public void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<Serializable> updateView, Serializable item) {
onCreateContextMenuSupport(menu, menuInflater, updateView, item);
recreateContextMenu(menu);
}
@Override
public boolean onContextItemSelected(MenuItem menuItem, UpdateView<Serializable> updateView, Serializable item) {
return onContextItemSelected(menuItem, item);
}
@Override
public void onItemClicked(UpdateView<Serializable> updateView, Serializable item) {
SubsonicFragment fragment;
if(item instanceof Artist) {
Artist artist = (Artist) item;
if ((Util.isFirstLevelArtist(context) || Util.isOffline(context) || Util.isTagBrowsing(context)) || groupId != null) {
fragment = new SelectDirectoryFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName());
if (ServerInfo.checkServerVersion(context, "1.13") && !Util.isOffline(context)) {
args.putSerializable(Constants.INTENT_EXTRA_NAME_DIRECTORY, new Entry(artist));
}
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
fragment.setArguments(args);
} else {
fragment = new SelectArtistFragment();
Bundle args = new Bundle();
args.putString(Constants.INTENT_EXTRA_NAME_ID, artist.getId());
args.putString(Constants.INTENT_EXTRA_NAME_NAME, artist.getName());
args.putBoolean(Constants.INTENT_EXTRA_NAME_ARTIST, true);
if (ServerInfo.checkServerVersion(context, "1.13") && !Util.isOffline(context)) {
args.putSerializable(Constants.INTENT_EXTRA_NAME_DIRECTORY, new Entry(artist));
}
fragment.setArguments(args);
}
replaceFragment(fragment);
} else {
Entry entry = (Entry) item;
if (entry.isVideo()) {
playVideo(entry);
} else {
List<Entry> songs = new ArrayList<Entry>();
if (Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_PLAY_NOW_AFTER, true)) {
for (Entry song : entries) {
if (!song.isDirectory() && !song.isVideo()) {
songs.add(song);
}
}
playNow(songs, entry, 0);
} else {
songs.add(entry);
playNow(songs);
}
}
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
super.onCreateOptionsMenu(menu, menuInflater);
if(Util.isOffline(context) || Util.isTagBrowsing(context) || groupId != null) {
menu.removeItem(R.id.menu_first_level_artist);
} else {
if (Util.isFirstLevelArtist(context)) {
menu.findItem(R.id.menu_first_level_artist).setChecked(true);
}
}
}
@Override
public int getOptionsMenu() {
return R.menu.select_artist;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(super.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.menu_first_level_artist:
toggleFirstLevelArtist();
break;
}
return false;
}
@Override
public SectionAdapter getAdapter(List<Serializable> objects) {
return new ArtistAdapter(context, objects, musicFolders, this, this);
}
@Override
public List<Serializable> getObjects(MusicService musicService, boolean refresh, ProgressListener listener) throws Exception {
List<Serializable> items;
if(groupId == null) {
if (!Util.isOffline(context) && (!Util.isTagBrowsing(context) || ServerInfo.checkServerVersion(context, "1.14"))) {
musicFolders = musicService.getMusicFolders(refresh, context, listener);
// Hide folders option if there is only one
if (musicFolders.size() == 1) {
musicFolders = null;
Util.setSelectedMusicFolderId(context, null);
}
} else {
musicFolders = null;
}
String musicFolderId = Util.getSelectedMusicFolderId(context);
Indexes indexes = musicService.getIndexes(musicFolderId, refresh, context, listener);
indexes.sortChildren(context);
items = new ArrayList<>(indexes.getShortcuts().size() + indexes.getArtists().size());
items.addAll(indexes.getShortcuts());
items.addAll(indexes.getArtists());
entries = indexes.getEntries();
items.addAll(entries);
} else {
List<Artist> artists = new ArrayList<>();
items = new ArrayList<>();
MusicDirectory dir = musicService.getMusicDirectory(groupId, groupName, refresh, context, listener);
for(Entry entry: dir.getChildren(true, false)) {
Artist artist = new Artist();
artist.setId(entry.getId());
artist.setName(entry.getTitle());
artist.setStarred(entry.isStarred());
artists.add(artist);
}
Indexes indexes = new Indexes(0, artists, new ArrayList<Artist>());
indexes.sortChildren(context);
items.addAll(indexes.getArtists());
entries = dir.getChildren(false, true);
for(Entry entry: entries) {
items.add(entry);
}
}
return items;
}
@Override
public int getTitleResource() {
return groupId == null ? R.string.button_bar_browse : 0;
}
@Override
public void setEmpty(boolean empty) {
super.setEmpty(empty);
if(empty && !Util.isOffline(context)) {
objects.clear();
recyclerView.setAdapter(new ArtistAdapter(context, objects, musicFolders, this, this));
recyclerView.setVisibility(View.VISIBLE);
View view = rootView.findViewById(R.id.tab_progress);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.height = 0;
params.weight = 5;
view.setLayoutParams(params);
}
}
private void toggleFirstLevelArtist() {
Util.toggleFirstLevelArtist(context);
context.invalidateOptionsMenu();
}
@Override
public void onMusicFolderChanged(MusicFolder selectedFolder) {
String startMusicFolderId = Util.getSelectedMusicFolderId(context);
String musicFolderId = selectedFolder == null ? null : selectedFolder.getId();
if(!Util.equals(startMusicFolderId, musicFolderId)) {
Util.setSelectedMusicFolderId(context, musicFolderId);
context.invalidate();
}
}
}
|