Skip to content

Commit

Permalink
Remember last selected source
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBoomDeveloper committed May 12, 2024
1 parent af36b11 commit 1788f37
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 34 deletions.
13 changes: 13 additions & 0 deletions app/src/main/assets/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@
}, {
"key": "media_columns_port", "type": "int", "from": 0,
"title": "Media columns count at Portrait", "description": "0 is for auto"
}, {
"key": "episodes", "type": "screen", "show_if": "never",
"title": "Episodes list",
"items": [
{
"key": "mode", "type": "select", "string_value": "LIST",
"title": "View mode", "description": "${VALUE_TITLE}",
"items": [
{ "key": "LIST", "title": "List" },
{ "key": "GRID", "title": "Grid" }
]
}
]
}
]
}, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ private SettingsItem findDirect(String key) {

public SettingsItem find(@NonNull String query) {
return switch(type) {
case BOOLEAN, INT, STRING, COLOR, SELECT, SELECT_INT, MULTISELECT -> query.equals(getFullKey()) ? this : null;
case BOOLEAN, INT, STRING, COLOR, SELECT, SELECT_INT, MULTISELECT ->
query.equals(getFullKey()) ? this : null;

case SCREEN, SCREEN_BOOLEAN -> {
if(query.equals(getFullKey())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
if(path != null) {
item = AwerySettings.getCached(path);

if(item != null && path.startsWith("ext_") && item.getItems().size() == 1) {
item = item.getItems().get(0);
if(item != null) {
if(path.startsWith("ext_") && item.getItems().size() == 1) {
item = item.getItems().get(0);
}
} else {
item = AwerySettings.getSettingsMap(this).find(path);
}

if(item == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.mrboomdev.awery.extensions.data.CatalogMediaProgress;
import com.mrboomdev.awery.sdk.util.UniqueIdGenerator;
import com.mrboomdev.awery.ui.activity.MediaActivity;
import com.mrboomdev.awery.ui.fragments.MediaPlayFragment;
import com.mrboomdev.awery.util.MediaUtils;
import com.mrboomdev.awery.util.exceptions.UnimplementedException;

Expand Down Expand Up @@ -84,6 +85,11 @@ public void setItems(CatalogMedia media, Collection<? extends CatalogEpisode> it
}).start();
}

@Override
public int getItemViewType(int position) {
return MediaPlayFragment.VIEW_TYPE_EPISODE;
}

public void setOnEpisodeSelectedListener(OnEpisodeSelectedListener listener) {
this.onEpisodeSelectedListener = listener;
}
Expand All @@ -103,7 +109,12 @@ public long getItemId(int position) {
return id;
}

private void changeWatchedState(CatalogEpisode episode, long episodeProgress, @NonNull ViewHolder holder) {
private void changeWatchedState(
CatalogEpisode episode,
long episodeProgress,
@NonNull ViewHolder holder,
Runnable callback
) {
progresses.put(episode, episodeProgress);
holder.updateProgress();

Expand All @@ -115,6 +126,10 @@ private void changeWatchedState(CatalogEpisode episode, long episodeProgress, @N

progress.progresses.put(episode.getNumber(), episodeProgress);
progressDao.insert(progress);

if(callback != null) {
callback.run();
}
}).start();
}

Expand Down Expand Up @@ -143,7 +158,7 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

menu.setOnMenuItemClickListener(item -> switch(item.getItemId()) {
case 0 -> {
changeWatchedState(holder.getItem(), progress != 0 ? 0 : -1L, holder);
changeWatchedState(holder.getItem(), progress != 0 ? 0 : -1L, holder, null);
yield true;
}

Expand Down Expand Up @@ -172,8 +187,8 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
var item = holder.getItem();
if(onEpisodeSelectedListener == null) return;

changeWatchedState(holder.getItem(), -1, holder);
onEpisodeSelectedListener.onEpisodeSelected(item, items);
changeWatchedState(holder.getItem(), -1, holder, () ->
runOnUiThread(() -> onEpisodeSelectedListener.onEpisodeSelected(item, items)));
});

return holder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.mrboomdev.awery.ui.fragments;

import static com.mrboomdev.awery.app.AweryApp.getDatabase;
import static com.mrboomdev.awery.app.AweryApp.isLandscape;
import static com.mrboomdev.awery.app.AweryApp.toast;
import static com.mrboomdev.awery.app.AweryLifecycle.runOnUiThread;
import static com.mrboomdev.awery.data.Constants.alwaysTrue;
import static com.mrboomdev.awery.util.NiceUtils.stream;
import static com.mrboomdev.awery.util.ui.ViewUtil.dpPx;
import static com.mrboomdev.awery.util.ui.ViewUtil.setBottomPadding;
Expand All @@ -24,12 +27,14 @@
import androidx.core.widget.ImageViewCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mrboomdev.awery.R;
import com.mrboomdev.awery.app.AweryLifecycle;
import com.mrboomdev.awery.app.CrashHandler;
import com.mrboomdev.awery.data.settings.AwerySettings;
import com.mrboomdev.awery.databinding.ItemListDropdownBinding;
import com.mrboomdev.awery.databinding.LayoutLoadingBinding;
import com.mrboomdev.awery.databinding.LayoutWatchVariantsBinding;
Expand All @@ -38,10 +43,13 @@
import com.mrboomdev.awery.extensions.ExtensionsFactory;
import com.mrboomdev.awery.extensions.data.CatalogEpisode;
import com.mrboomdev.awery.extensions.data.CatalogMedia;
import com.mrboomdev.awery.extensions.data.CatalogMediaProgress;
import com.mrboomdev.awery.extensions.data.CatalogSearchResults;
import com.mrboomdev.awery.sdk.data.CatalogFilter;
import com.mrboomdev.awery.sdk.util.StringUtils;
import com.mrboomdev.awery.ui.activity.SearchActivity;
import com.mrboomdev.awery.ui.activity.player.PlayerActivity;
import com.mrboomdev.awery.ui.activity.settings.SettingsActivity;
import com.mrboomdev.awery.ui.adapter.MediaPlayEpisodesAdapter;
import com.mrboomdev.awery.util.MediaUtils;
import com.mrboomdev.awery.util.NiceUtils;
Expand All @@ -63,22 +71,31 @@
import java.util.concurrent.atomic.AtomicReference;

public class MediaPlayFragment extends Fragment implements MediaPlayEpisodesAdapter.OnEpisodeSelectedListener {
private final String TAG = "MediaPlayFragment";
public static final int VIEW_TYPE_VARIANTS = 1;
public static final int VIEW_TYPE_ERROR = 2;
public static final int VIEW_TYPE_EPISODE = 3;
private static final String TAG = "MediaPlayFragment";
private final Map<ExtensionProvider, ExtensionStatus> sourceStatuses = new HashMap<>();
private final CatalogFilter queryFilter = new CatalogFilter(CatalogFilter.Type.STRING, "query");
private final List<CatalogFilter> filters = List.of(queryFilter,
new CatalogFilter(CatalogFilter.Type.NUMBER, "page", 0));
private SingleViewAdapter.BindingSingleViewAdapter<LayoutLoadingBinding> placeholderAdapter;
private SingleViewAdapter.BindingSingleViewAdapter<LayoutWatchVariantsBinding> variantsAdapter;
private ArrayListAdapter<ExtensionProvider> sourcesDropdownAdapter;
private ConcatAdapter concatAdapter;
private List<? extends CatalogEpisode> templateEpisodes;
private List<ExtensionProvider> providers;
private MediaPlayEpisodesAdapter episodesAdapter;
private RecyclerView recycler;
private ExtensionProvider selectedSource;
private ViewMode viewMode;
private CatalogMedia media;
private boolean autoChangeSource = true;
private boolean autoChangeSource = true, changeSettings = true;
private int currentSourceIndex = 0;
private long loadId;

public enum ViewMode { GRID, LIST }

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Expand Down Expand Up @@ -110,6 +127,16 @@ public void onEpisodeSelected(@NonNull CatalogEpisode episode, @NonNull ArrayLis
intent.putExtra("episode", episode);
intent.putParcelableArrayListExtra("episodes", episodes);
startActivity(intent);

new Thread(() -> {
var dao = getDatabase().getMediaProgressDao();

var progress = dao.get(media.globalId);
if(progress == null) progress = new CatalogMediaProgress(media.globalId);

progress.lastWatchSource = selectedSource.getId();
dao.insert(progress);
}).start();
}

private enum ExtensionStatus {
Expand Down Expand Up @@ -152,23 +179,85 @@ public void setMedia(CatalogMedia media) {
return;
}

var mediaSource = ExtensionsFactory.getExtensionProvider(0, media.globalId);
new Thread(() -> {
var progress = getDatabase().getMediaProgressDao().get(media.globalId);
var mediaSource = ExtensionsFactory.getExtensionProvider(0, media.globalId);

if(mediaSource != null) {
mediaSource.getEpisodes(0, media, new ExtensionProvider.ResponseCallback<>() {
@Override
public void onSuccess(List<? extends CatalogEpisode> catalogEpisodes) {
templateEpisodes = catalogEpisodes;
runOnUiThread(() -> selectProvider(providers.get(0)));
}
if(progress != null) {
providers = new ArrayList<>(providers);

@Override
public void onFailure(Throwable e) {
// Don't merge any data. Just load original data
runOnUiThread(() -> selectProvider(providers.get(0)));
}
});
providers.sort((a, b) -> {
if(a.getId().equals(progress.lastWatchSource)) return -1;
if(b.getId().equals(progress.lastWatchSource)) return 1;
return 0;
});

sourcesDropdownAdapter.setItems(providers);
}

if(mediaSource != null) {
mediaSource.getEpisodes(0, media, new ExtensionProvider.ResponseCallback<>() {
@Override
public void onSuccess(List<? extends CatalogEpisode> catalogEpisodes) {
templateEpisodes = catalogEpisodes;
runOnUiThread(() -> selectProvider(providers.get(0)));
}

@Override
public void onFailure(Throwable e) {
// Don't merge any data. Just load original data
runOnUiThread(() -> selectProvider(providers.get(0)));
}
});
}
}).start();
}

@Override
public void onResume() {
super.onResume();
if(alwaysTrue()) return;

if(changeSettings) {
var prefs = AwerySettings.getInstance(requireContext());
var viewMode = StringUtils.parseEnum(prefs.getString("settings_ui_episodes_mode"), ViewMode.LIST);

if(viewMode != this.viewMode) {
this.viewMode = viewMode;

recycler.setLayoutManager(switch(viewMode) {
case LIST -> new LinearLayoutManager(requireContext());

case GRID -> {
var columnsCount = new AtomicInteger(3);
var layoutManager = new GridLayoutManager(requireContext(), columnsCount.get());

ViewUtil.setOnApplyUiInsetsListener(recycler, insets -> {
var padding = ViewUtil.dpPx(8);
ViewUtil.setVerticalPadding(recycler, padding + padding * 2);
ViewUtil.setHorizontalPadding(recycler, insets.left + padding, insets.right + padding);

float columnSize = ViewUtil.dpPx(80);
float freeSpace = getResources().getDisplayMetrics().widthPixels - (padding * 2) - insets.left - insets.right;
columnsCount.set((int)(freeSpace / columnSize));
layoutManager.setSpanCount(columnsCount.get());
});

layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
/* Don't ask. I don't know how it is working, so please don't ask about it. */
return (concatAdapter.getItemViewType(position) == VIEW_TYPE_EPISODE) ? 1 : columnsCount.get();
}
});

yield layoutManager;
}
});
}
}

changeSettings = false;
}

@Override
Expand All @@ -178,7 +267,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
.flatMap(NiceUtils::stream)
.sorted().toList();

var sourcesDropdownAdapter = new ArrayListAdapter<>((item, recycled, parent) -> {
sourcesDropdownAdapter = new ArrayListAdapter<>((item, recycled, parent) -> {
if(recycled == null) {
var inflater = LayoutInflater.from(parent.getContext());
var binding = ItemListDropdownBinding.inflate(inflater, parent, false);
Expand Down Expand Up @@ -298,6 +387,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
selectProvider(selectedSource);
}
});

/*binding.options.setOnClickListener(v -> {
var intent = new Intent(requireContext(), SettingsActivity.class);
intent.putExtra("path", "settings_ui_episodes");
startActivity(intent);
changeSettings = true;
});*/
});

setMedia(media);
Expand Down Expand Up @@ -516,22 +612,23 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
var layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

variantsAdapter = SingleViewAdapter.fromBindingDynamic(parent ->
LayoutWatchVariantsBinding.inflate(inflater, parent, false));
LayoutWatchVariantsBinding.inflate(inflater, parent, false), VIEW_TYPE_VARIANTS);

placeholderAdapter = SingleViewAdapter.fromBindingDynamic(parent ->
LayoutLoadingBinding.inflate(inflater, parent, false));
LayoutLoadingBinding.inflate(inflater, parent, false), VIEW_TYPE_ERROR);

episodesAdapter = new MediaPlayEpisodesAdapter();
episodesAdapter.setOnEpisodeSelectedListener(this);

var config = new ConcatAdapter.Config.Builder()
.setStableIdMode(ConcatAdapter.Config.StableIdMode.ISOLATED_STABLE_IDS)
.setIsolateViewTypes(false)
.build();

var concatAdapter = new ConcatAdapter(config,
concatAdapter = new ConcatAdapter(config,
variantsAdapter, episodesAdapter, placeholderAdapter);

var recycler = new RecyclerView(inflater.getContext());
recycler = new RecyclerView(inflater.getContext());
recycler.setLayoutManager(layoutManager);
recycler.setAdapter(concatAdapter);

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M124,714.38L124,656L836,656L836,714.38L124,714.38ZM124,508.69L124,450.31L836,450.31L836,508.69L124,508.69ZM124,303L124,244.62L836,244.62L836,303L124,303Z"/>
</vector>
Loading

0 comments on commit 1788f37

Please sign in to comment.