aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/mygram/droid/FolderActivity.java
blob: 10a99401536ed7d1699e82830b14cdb0b928c21d (plain)
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
/*
 * 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.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;

/** One folder as a grid, shuffled once per open. */
public final class FolderActivity extends Activity {
	private FrameLayout content;
	private RecyclerView grid;

	static void open(Context c, Api.Folder f) {
		c.startActivity(new Intent(c, FolderActivity.class).putExtra("slug", f.slug).putExtra("name", f.name));
	}

	@Override
	protected void onCreate(Bundle state) {
		super.onCreate(state);
		LinearLayout column = new LinearLayout(this);
		column.setOrientation(LinearLayout.VERTICAL);
		TextView title = Ui.text(this, getIntent().getStringExtra("name"), 24);
		title.setPadding(Ui.dp(this, 36), Ui.dp(this, 24), 0, 0);
		column.addView(title);
		content = new FrameLayout(this);
		column.addView(content, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1));
		setContentView(column);
		show(Ui.text(this, "Loading…", 18));
		Api.async(() -> Api.folderPosts(getIntent().getStringExtra("slug")), posts -> {
			Collections.shuffle(posts);
			grid = Ui.grid(this, Ui.postTiles(this, posts), Ui.POST_W, Ui.POST_H, 6);
			show(grid);
			grid.post(grid::requestFocus); // after layout
		}, e -> show(Ui.text(this, "Cannot load folder: " + e.getMessage(), 18)));
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (grid != null && grid.getAdapter() != null)
			grid.getAdapter().notifyItemRangeChanged(0, grid.getAdapter().getItemCount());
	}

	private void show(android.view.View v) {
		content.removeAllViews();
		content.addView(v);
	}
}