replace codec class with an enum
This commit is contained in:
parent
37eb40e79a
commit
85eedab1c4
6 changed files with 59 additions and 90 deletions
|
|
@ -10,15 +10,19 @@ import androidx.annotation.NonNull;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
import com.dkanada.gramophone.R;
|
import com.dkanada.gramophone.R;
|
||||||
import com.dkanada.gramophone.model.DirectPlayCodec;
|
import com.dkanada.gramophone.model.Codec;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class DirectPlayCodecAdapter extends RecyclerView.Adapter<DirectPlayCodecAdapter.ViewHolder> {
|
public class DirectPlayCodecAdapter extends RecyclerView.Adapter<DirectPlayCodecAdapter.ViewHolder> {
|
||||||
private final List<DirectPlayCodec> directPlayCodecs;
|
private final List<Codec> codecs;
|
||||||
|
|
||||||
public DirectPlayCodecAdapter(List<DirectPlayCodec> directPlayCodecs) {
|
public DirectPlayCodecAdapter(List<Codec> codecs) {
|
||||||
this.directPlayCodecs = directPlayCodecs;
|
this.codecs = Arrays.stream(Codec.values())
|
||||||
|
.peek(codec -> codec.select = codecs.contains(codec))
|
||||||
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
|
@ -31,25 +35,25 @@ public class DirectPlayCodecAdapter extends RecyclerView.Adapter<DirectPlayCodec
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(@NonNull DirectPlayCodecAdapter.ViewHolder holder, int position) {
|
public void onBindViewHolder(@NonNull DirectPlayCodecAdapter.ViewHolder holder, int position) {
|
||||||
DirectPlayCodec directPlayCodec = directPlayCodecs.get(position);
|
Codec codec = codecs.get(position);
|
||||||
|
|
||||||
holder.checkbox.setChecked(directPlayCodec.selected);
|
holder.checkbox.setChecked(codec.select);
|
||||||
holder.container.setText(directPlayCodec.codec.container);
|
holder.container.setText(codec.container);
|
||||||
holder.codec.setText(directPlayCodec.codec.codec);
|
holder.codec.setText(codec.codec);
|
||||||
|
|
||||||
holder.itemView.setOnClickListener(v -> {
|
holder.itemView.setOnClickListener(v -> {
|
||||||
directPlayCodec.selected = !directPlayCodec.selected;
|
codec.select = !codec.select;
|
||||||
holder.checkbox.setChecked(directPlayCodec.selected);
|
holder.checkbox.setChecked(codec.select);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
return directPlayCodecs.size();
|
return codecs.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<DirectPlayCodec> getDirectPlayCodecs() {
|
public List<Codec> getCodecs() {
|
||||||
return directPlayCodecs;
|
return codecs.stream().filter(codec -> codec.select).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ViewHolder extends RecyclerView.ViewHolder {
|
public static class ViewHolder extends RecyclerView.ViewHolder {
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ public class DirectPlayPreferenceDialog extends DialogFragment {
|
||||||
.autoDismiss(false)
|
.autoDismiss(false)
|
||||||
.onNegative((dialog, action) -> dismiss())
|
.onNegative((dialog, action) -> dismiss())
|
||||||
.onPositive((dialog, action) -> {
|
.onPositive((dialog, action) -> {
|
||||||
PreferenceUtil.getInstance(getContext()).setDirectPlayCodecs(adapter.getDirectPlayCodecs());
|
PreferenceUtil.getInstance(getContext()).setDirectPlayCodecs(adapter.getCodecs());
|
||||||
dismiss();
|
dismiss();
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
|
||||||
23
app/src/main/java/com/dkanada/gramophone/model/Codec.java
Normal file
23
app/src/main/java/com/dkanada/gramophone/model/Codec.java
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.dkanada.gramophone.model;
|
||||||
|
|
||||||
|
public enum Codec {
|
||||||
|
FLAC("FLAC", "FLAC", "flac|flac"),
|
||||||
|
MP3("MP3", "MP3", "mp3|mp3"),
|
||||||
|
OPUS("Opus", "Opus", "opus|opus"),
|
||||||
|
AAC("M4A", "AAC", "m4a|aac"),
|
||||||
|
VORBIS("OGG", "Vorbis", "ogg|vorbis"),
|
||||||
|
OGG("OGG", "Opus", "ogg|opus"),
|
||||||
|
MKA("MKA", "Opus", "mka|opus");
|
||||||
|
|
||||||
|
public final String container;
|
||||||
|
public final String codec;
|
||||||
|
public final String value;
|
||||||
|
|
||||||
|
public boolean select;
|
||||||
|
|
||||||
|
Codec(String container, String codec, String value) {
|
||||||
|
this.container = container;
|
||||||
|
this.codec = codec;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
package com.dkanada.gramophone.model;
|
|
||||||
|
|
||||||
public class DirectPlayCodec {
|
|
||||||
public Codec codec;
|
|
||||||
public boolean selected;
|
|
||||||
|
|
||||||
public DirectPlayCodec(Codec codec, boolean selected) {
|
|
||||||
this.codec = codec;
|
|
||||||
this.selected = selected;
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum Codec {
|
|
||||||
FLAC("FLAC", "FLAC", "flac|flac"),
|
|
||||||
MP3("MP3", "MP3", "mp3|mp3"),
|
|
||||||
OPUS("Opus", "Opus", "opus|opus"),
|
|
||||||
AAC("M4A", "AAC", "m4a|aac"),
|
|
||||||
VORBIS("OGG", "Vorbis", "ogg|vorbis"),
|
|
||||||
OGG("OGG", "Opus", "ogg|opus"),
|
|
||||||
MKA("MKA", "Opus", "mka|opus");
|
|
||||||
|
|
||||||
public final String container;
|
|
||||||
public final String codec;
|
|
||||||
public final String value;
|
|
||||||
|
|
||||||
Codec(String container, String codec, String value) {
|
|
||||||
this.container = container;
|
|
||||||
this.codec = codec;
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -13,7 +13,7 @@ import com.dkanada.gramophone.App;
|
||||||
import com.dkanada.gramophone.R;
|
import com.dkanada.gramophone.R;
|
||||||
import com.dkanada.gramophone.model.Album;
|
import com.dkanada.gramophone.model.Album;
|
||||||
import com.dkanada.gramophone.model.Artist;
|
import com.dkanada.gramophone.model.Artist;
|
||||||
import com.dkanada.gramophone.model.DirectPlayCodec;
|
import com.dkanada.gramophone.model.Codec;
|
||||||
import com.dkanada.gramophone.model.Genre;
|
import com.dkanada.gramophone.model.Genre;
|
||||||
import com.dkanada.gramophone.model.Song;
|
import com.dkanada.gramophone.model.Song;
|
||||||
|
|
||||||
|
|
@ -24,6 +24,8 @@ import org.jellyfin.apiclient.model.dto.UserItemDataDto;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class MusicUtil {
|
public class MusicUtil {
|
||||||
public static String getTranscodeUri(Song song) {
|
public static String getTranscodeUri(Song song) {
|
||||||
|
|
@ -42,21 +44,11 @@ public class MusicUtil {
|
||||||
// web client maximum is 12444445 and 320kbps is 320000
|
// web client maximum is 12444445 and 320kbps is 320000
|
||||||
builder.append("&MaxStreamingBitrate=").append(preferenceUtil.getMaximumBitrate());
|
builder.append("&MaxStreamingBitrate=").append(preferenceUtil.getMaximumBitrate());
|
||||||
|
|
||||||
boolean containerAdded = false;
|
List<Codec> codecs = preferenceUtil.getDirectPlayCodecs();
|
||||||
for (DirectPlayCodec directPlayCodec : preferenceUtil.getDirectPlayCodecs()) {
|
Stream<String> values = codecs.stream().map(codec -> codec.value);
|
||||||
if (directPlayCodec.selected) {
|
|
||||||
if (!containerAdded) {
|
|
||||||
builder.append("&Container=");
|
|
||||||
containerAdded = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.append(directPlayCodec.codec.value).append(',');
|
if (codecs.size() != 0) {
|
||||||
}
|
builder.append("&Container=").append(values.collect(Collectors.joining(",")));
|
||||||
}
|
|
||||||
|
|
||||||
if (containerAdded) {
|
|
||||||
// remove last comma
|
|
||||||
builder.deleteCharAt(builder.length() - 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.append("&TranscodingContainer=ts");
|
builder.append("&TranscodingContainer=ts");
|
||||||
|
|
|
||||||
|
|
@ -14,16 +14,17 @@ import com.dkanada.gramophone.R;
|
||||||
import com.dkanada.gramophone.helper.sort.SortMethod;
|
import com.dkanada.gramophone.helper.sort.SortMethod;
|
||||||
import com.dkanada.gramophone.helper.sort.SortOrder;
|
import com.dkanada.gramophone.helper.sort.SortOrder;
|
||||||
import com.dkanada.gramophone.model.CategoryInfo;
|
import com.dkanada.gramophone.model.CategoryInfo;
|
||||||
import com.dkanada.gramophone.model.DirectPlayCodec;
|
import com.dkanada.gramophone.model.Codec;
|
||||||
import com.dkanada.gramophone.interfaces.base.PreferenceMigration;
|
import com.dkanada.gramophone.interfaces.base.PreferenceMigration;
|
||||||
import com.dkanada.gramophone.fragments.player.NowPlayingScreen;
|
import com.dkanada.gramophone.fragments.player.NowPlayingScreen;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@SuppressLint("ApplySharedPref")
|
@SuppressLint("ApplySharedPref")
|
||||||
public final class PreferenceUtil {
|
public final class PreferenceUtil {
|
||||||
|
|
@ -451,38 +452,18 @@ public final class PreferenceUtil {
|
||||||
return defaultCategories;
|
return defaultCategories;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<DirectPlayCodec> getDirectPlayCodecs() {
|
@SuppressWarnings("ConstantConditions")
|
||||||
DirectPlayCodec.Codec[] codecs = DirectPlayCodec.Codec.values();
|
public List<Codec> getDirectPlayCodecs() {
|
||||||
|
Set<String> defaultValues = Arrays.stream(Codec.values()).map(Enum::toString).collect(Collectors.toSet());
|
||||||
|
Set<String> values = mPreferences.getStringSet(DIRECT_PLAY_CODECS, defaultValues);
|
||||||
|
|
||||||
Set<String> selectedCodecNames = new HashSet<>();
|
return values.stream().map(Codec::valueOf).collect(Collectors.toList());
|
||||||
for (DirectPlayCodec.Codec codec : codecs) {
|
|
||||||
// this will be the default value
|
|
||||||
selectedCodecNames.add(codec.name());
|
|
||||||
}
|
|
||||||
|
|
||||||
selectedCodecNames = mPreferences.getStringSet(DIRECT_PLAY_CODECS, selectedCodecNames);
|
|
||||||
|
|
||||||
ArrayList<DirectPlayCodec> directPlayCodecs = new ArrayList<>();
|
|
||||||
for (DirectPlayCodec.Codec codec : codecs) {
|
|
||||||
String name = codec.name();
|
|
||||||
boolean selected = selectedCodecNames.contains(name);
|
|
||||||
directPlayCodecs.add(new DirectPlayCodec(codec, selected));
|
|
||||||
}
|
|
||||||
|
|
||||||
return directPlayCodecs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDirectPlayCodecs(List<DirectPlayCodec> directPlayCodecs) {
|
public void setDirectPlayCodecs(List<Codec> codecs) {
|
||||||
Set<String> codecNames = new HashSet<>();
|
Set<String> values = codecs.stream().map(Enum::toString).collect(Collectors.toSet());
|
||||||
for (DirectPlayCodec directPlayCodec : directPlayCodecs) {
|
|
||||||
if (directPlayCodec.selected) {
|
|
||||||
codecNames.add(directPlayCodec.codec.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final SharedPreferences.Editor editor = mPreferences.edit();
|
mPreferences.edit().putStringSet(DIRECT_PLAY_CODECS, values).apply();
|
||||||
editor.putStringSet(DIRECT_PLAY_CODECS, codecNames);
|
|
||||||
editor.apply();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getServer() {
|
public String getServer() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue