0
0
mirror of https://github.com/mediathekview/zapp.git synced 2024-09-20 20:23:04 +02:00

Add possibility to disable channels

This commit is contained in:
Christine Emrich 2019-08-07 22:28:24 +02:00
parent 9d43cc6648
commit d4095fd00e
8 changed files with 148 additions and 35 deletions

View File

@ -3,11 +3,6 @@ package de.christinecoenen.code.zapp.app.livestream.ui.list;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
@ -15,13 +10,19 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupMenu;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import de.christinecoenen.code.zapp.R;
import de.christinecoenen.code.zapp.app.livestream.ui.detail.ChannelDetailActivity;
import de.christinecoenen.code.zapp.model.ChannelModel;
import de.christinecoenen.code.zapp.model.ISortableChannelList;
import de.christinecoenen.code.zapp.model.json.SortableJsonChannelList;
import de.christinecoenen.code.zapp.model.json.SortableVisibleJsonChannelList;
import de.christinecoenen.code.zapp.utils.system.MultiWindowHelper;
import de.christinecoenen.code.zapp.utils.view.GridAutofitLayoutManager;
@ -51,7 +52,7 @@ public class ChannelListFragment extends Fragment implements ChannelListAdapter.
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
channelList = new SortableJsonChannelList(getContext());
channelList = new SortableVisibleJsonChannelList(getContext());
gridAdapter = new ChannelListAdapter(getContext(), channelList, this);
}

View File

@ -18,6 +18,7 @@ import de.christinecoenen.code.zapp.model.ChannelModel;
public class PreferenceChannelOrderHelper {
private static final String PREF_KEY_CHANNEL_ORDER = "PREF_KEY_CHANNEL_ORDER";
private static final String PREF_KEY_CHANNELS_NOT_VISIBLE = "PREF_KEY_CHANNELS_NOT_VISIBLE";
private final PreferenceHelper preferenceHelper;
@ -26,16 +27,16 @@ public class PreferenceChannelOrderHelper {
}
public void saveChannelOrder(List<ChannelModel> channels) {
List<String> sortedChannelIds = new ArrayList<>(channels.size());
for (ChannelModel channel : channels) {
sortedChannelIds.add(channel.getId());
}
preferenceHelper.saveList(PREF_KEY_CHANNEL_ORDER, sortedChannelIds);
saveOrder(channels);
saveVisibility(channels);
}
public List<ChannelModel> sortChannelList(List<ChannelModel> channels) {
public List<ChannelModel> sortChannelList(List<ChannelModel> channels, boolean removeDisabled) {
List<ChannelModel> sortedChannels = loadOrder(channels);
return loadVisibility(sortedChannels, removeDisabled);
}
private List<ChannelModel> loadOrder(List<ChannelModel> channels) {
List<String> sortedChannelIds = preferenceHelper.loadList(PREF_KEY_CHANNEL_ORDER);
if (sortedChannelIds == null) {
@ -65,4 +66,52 @@ public class PreferenceChannelOrderHelper {
return sortedChannelList;
}
private List<ChannelModel> loadVisibility(List<ChannelModel> channels, boolean removeDisabled) {
List<String> disabledChannelIds = preferenceHelper.loadList(PREF_KEY_CHANNELS_NOT_VISIBLE);
if (disabledChannelIds == null) {
// have never been saved before
return channels;
}
if (removeDisabled) {
List<ChannelModel> enabledChannels = new ArrayList<>();
for (ChannelModel channel : channels) {
boolean isDisabled = disabledChannelIds.contains(channel.getId());
if (!isDisabled) {
enabledChannels.add(channel);
}
}
return enabledChannels;
} else {
for (ChannelModel channel : channels) {
boolean isDisabled = disabledChannelIds.contains(channel.getId());
channel.setEnabled(!isDisabled);
}
return channels;
}
}
private void saveOrder(List<ChannelModel> channels) {
List<String> sortedChannelIds = new ArrayList<>(channels.size());
for (ChannelModel channel : channels) {
sortedChannelIds.add(channel.getId());
}
preferenceHelper.saveList(PREF_KEY_CHANNEL_ORDER, sortedChannelIds);
}
private void saveVisibility(List<ChannelModel> channels) {
List<String> disabledChannelIds = new ArrayList<>();
for (ChannelModel channel : channels) {
if (!channel.isEnabled()) {
disabledChannelIds.add(channel.getId());
}
}
preferenceHelper.saveList(PREF_KEY_CHANNELS_NOT_VISIBLE, disabledChannelIds);
}
}

View File

@ -20,6 +20,8 @@ public class ChannelSelectionActivity extends AppCompatActivity {
@BindView(R.id.draglist_channel_selection)
protected DragListView channelListView;
private ISortableChannelList channelList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -33,7 +35,7 @@ public class ChannelSelectionActivity extends AppCompatActivity {
}
// adapter
final ISortableChannelList channelList = new SortableJsonChannelList(this);
channelList = new SortableJsonChannelList(this);
final ChannelSelectionAdapter listAdapter = new ChannelSelectionAdapter(this);
listAdapter.setItemList(channelList.getList());
@ -52,4 +54,10 @@ public class ChannelSelectionActivity extends AppCompatActivity {
}
});
}
@Override
protected void onPause() {
super.onPause();
channelList.persistChannelOrder();
}
}

View File

@ -1,13 +1,14 @@
package de.christinecoenen.code.zapp.app.settings.ui;
import android.content.Context;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.woxthebox.draglistview.DragItemAdapter;
import butterknife.BindView;
@ -50,12 +51,17 @@ class ChannelSelectionAdapter extends DragItemAdapter<ChannelModel, ChannelSelec
@BindView(R.id.image_channel_logo) ImageView logoView;
@BindView(R.id.text_channel_subtitle) TextView subtitle;
private ChannelModel channel;
ViewHolder(final View itemView) {
super(itemView, R.id.image_handle, false);
ButterKnife.bind(this, itemView);
}
void setChannel(ChannelModel channel) {
this.channel = channel;
setVisibility();
logoView.setImageResource(channel.getDrawableId());
handleView.setContentDescription(channel.getName());
@ -66,5 +72,20 @@ class ChannelSelectionAdapter extends DragItemAdapter<ChannelModel, ChannelSelec
subtitle.setVisibility(View.VISIBLE);
}
}
@Override
public void onItemClicked(View view) {
channel.toggleIsEnabled();
setVisibility();
}
private void setVisibility() {
float alpha = channel.isEnabled() ? 1 : 0.25f;
logoView.setAlpha(alpha);
subtitle.setAlpha(alpha);
float handleAlpha = channel.isEnabled() ? 1 : 0.5f;
handleView.setAlpha(handleAlpha);
}
}
}

View File

@ -3,8 +3,6 @@ package de.christinecoenen.code.zapp.model;
import android.content.Intent;
import android.net.Uri;
import androidx.annotation.NonNull;
import java.io.Serializable;
public class ChannelModel implements Serializable {
@ -15,6 +13,7 @@ public class ChannelModel implements Serializable {
private String streamUrl;
private int drawableId;
private int color = 0;
private boolean isEnabled = true;
public String getId() {
return id;
@ -64,22 +63,34 @@ public class ChannelModel implements Serializable {
this.color = color;
}
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean enabled) {
isEnabled = enabled;
}
public void toggleIsEnabled() {
isEnabled = !isEnabled;
}
public Intent getVideoShareIntent() {
Intent videoIntent = new Intent(Intent.ACTION_VIEW);
videoIntent.setDataAndType(Uri.parse(streamUrl), "video/*");
return videoIntent;
}
@NonNull
@Override
public String toString() {
return "ChannelModel{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", subtitle='" + subtitle + '\'' +
", streamUrl='" + streamUrl + '\'' +
", drawableId=" + drawableId +
", color=" + color +
'}';
"id='" + id + '\'' +
", name='" + name + '\'' +
", subtitle='" + subtitle + '\'' +
", streamUrl='" + streamUrl + '\'' +
", drawableId=" + drawableId +
", color=" + color +
", isEnabled=" + isEnabled +
'}';
}
}

View File

@ -15,9 +15,9 @@ import de.christinecoenen.code.zapp.app.settings.helper.PreferenceChannelOrderHe
public class SortableJsonChannelList implements ISortableChannelList {
private final Context context;
private final PreferenceChannelOrderHelper channelOrderHelper;
private IChannelList channelList;
final Context context;
final PreferenceChannelOrderHelper channelOrderHelper;
IChannelList channelList;
public SortableJsonChannelList(Context context) {
this.context = context;
@ -27,7 +27,6 @@ public class SortableJsonChannelList implements ISortableChannelList {
@Override
public void reload() {
channelList = new JsonChannelList(context);
loadSortingFromDisk();
}
@ -72,8 +71,9 @@ public class SortableJsonChannelList implements ISortableChannelList {
channelOrderHelper.saveChannelOrder(channelList.getList());
}
private void loadSortingFromDisk() {
List<ChannelModel> sortedChannels = channelOrderHelper.sortChannelList(channelList.getList());
void loadSortingFromDisk() {
List<ChannelModel> listFromDisk = new JsonChannelList(context).getList();
List<ChannelModel> sortedChannels = channelOrderHelper.sortChannelList(listFromDisk, false);
channelList = new SimpleChannelList(sortedChannels);
}
}

View File

@ -0,0 +1,22 @@
package de.christinecoenen.code.zapp.model.json;
import android.content.Context;
import java.util.List;
import de.christinecoenen.code.zapp.model.ChannelModel;
import de.christinecoenen.code.zapp.model.SimpleChannelList;
public class SortableVisibleJsonChannelList extends SortableJsonChannelList {
public SortableVisibleJsonChannelList(Context context) {
super(context);
}
protected void loadSortingFromDisk() {
List<ChannelModel> listFromDisk = new JsonChannelList(context).getList();
List<ChannelModel> sortedChannels = channelOrderHelper.sortChannelList(listFromDisk, true);
channelList = new SimpleChannelList(sortedChannels);
}
}

View File

@ -17,7 +17,7 @@ import de.christinecoenen.code.zapp.app.livestream.api.model.ChannelInfo;
import de.christinecoenen.code.zapp.app.livestream.repository.ChannelInfoRepository;
import de.christinecoenen.code.zapp.model.ChannelModel;
import de.christinecoenen.code.zapp.model.ISortableChannelList;
import de.christinecoenen.code.zapp.model.json.SortableJsonChannelList;
import de.christinecoenen.code.zapp.model.json.SortableVisibleJsonChannelList;
import io.reactivex.Single;
import io.reactivex.disposables.Disposable;
import timber.log.Timber;
@ -33,7 +33,7 @@ public class ChannelRepository {
public ChannelRepository(Context context) {
this.context = context;
channelList = new SortableJsonChannelList(context);
channelList = new SortableVisibleJsonChannelList(context);
Disposable disposable = getChannelInfoListFromApi()
.onErrorReturn(t -> getChannelInfoListFromDisk())
@ -41,6 +41,7 @@ public class ChannelRepository {
}
public ISortableChannelList getChannelList() {
channelList.reload();
return channelList;
}