diff options
Diffstat (limited to 'app/src/main/java/mygram/droid/HomeRows.java')
| -rw-r--r-- | app/src/main/java/mygram/droid/HomeRows.java | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/app/src/main/java/mygram/droid/HomeRows.java b/app/src/main/java/mygram/droid/HomeRows.java new file mode 100644 index 0000000..ed638b6 --- /dev/null +++ b/app/src/main/java/mygram/droid/HomeRows.java @@ -0,0 +1,119 @@ +/* + * mygram-droid: Android TV client for Foldergram. + * Copyright (C) 2026 Danilo M. <danix@danix.xyz> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * This program 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. + */ + + +package mygram.droid; + +import android.app.Activity; +import android.content.Context; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** Home: one row per folder, a random sample of its posts, then a Continue tile. */ +final class HomeRows extends RecyclerView.Adapter<HomeRows.Row> { + private static final int SAMPLE = 10; + + static final class Row extends RecyclerView.ViewHolder { + final TextView title; + final RecyclerView tiles; + + Row(LinearLayout v, TextView title, RecyclerView tiles) { + super(v); + this.title = title; + this.tiles = tiles; + } + } + + private final Activity activity; + private final List<Api.Folder> folders; + private final Map<String, List<Post>> samples = new HashMap<>(); + + HomeRows(Activity activity, List<Api.Folder> folders) { + this.activity = activity; + this.folders = folders; + } + + @Override + public Row onCreateViewHolder(ViewGroup parent, int type) { + Context c = parent.getContext(); + LinearLayout v = new LinearLayout(c); + v.setOrientation(LinearLayout.VERTICAL); + v.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT)); + TextView title = Ui.text(c, "", 18); + title.setPadding(Ui.dp(c, 36), Ui.dp(c, 16), 0, 0); + v.addView(title); + RecyclerView tiles = Ui.list(c, RecyclerView.HORIZONTAL); + v.addView(tiles, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + Ui.dp(c, Ui.POST_H + 24))); + return new Row(v, title, tiles); + } + + @Override + public void onBindViewHolder(Row row, int position) { + Api.Folder f = folders.get(position); + row.title.setText(f.name); + List<Post> posts = samples.get(f.slug); + if (posts == null) { + posts = List.of(); + samples.put(f.slug, posts); // marks it loading, so it is fetched once + Api.async(() -> Api.folderSample(f, SAMPLE), p -> { + samples.put(f.slug, p); + notifyItemChanged(folders.indexOf(f)); + }, e -> { + // the row keeps only its Continue tile + }); + } + List<TileAdapter.Tile> tiles = new ArrayList<>(); + for (Post p : posts) + tiles.add(TileAdapter.post(p, () -> open(f, p))); + tiles.add(new TileAdapter.Tile(null, "Continue", R.drawable.ic_arrow_forward, -1, + () -> FolderActivity.open(activity, f))); + row.tiles.setAdapter(new TileAdapter(tiles, Ui.POST_W, Ui.POST_H)); + } + + @Override + public int getItemCount() { + return folders.size(); + } + + /** Viewer at p, then the rest of the folder, shuffled. */ + private void open(Api.Folder f, Post p) { + Api.async(() -> Api.folderPosts(f.slug), all -> { + Collections.shuffle(all); + all.removeIf(x -> x.id == p.id); + all.add(0, p); + Playlist.open(activity, all, 0); + }, e -> Toast.makeText(activity, "Cannot load folder: " + e.getMessage(), Toast.LENGTH_LONG).show()); + } + + /** Rebinds visible tiles so hearts follow likes changed in the viewer. */ + void refreshHearts(RecyclerView list) { + for (int i = 0; i < list.getChildCount(); i++) { + RecyclerView.Adapter<?> a = ((Row) list.getChildViewHolder(list.getChildAt(i))).tiles.getAdapter(); + if (a != null) + a.notifyItemRangeChanged(0, a.getItemCount()); + } + } +} |
