aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/mygram/droid/MainActivity.java
blob: 7c7f1948974d3eddf00e897217ba1fbedcac8fe8 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * 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.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/** Top bar, rotating blurred background, and the Home, Liked and Folders tabs. */
public final class MainActivity extends Activity {
	private static final int HOME = 0, LIKED = 1, FOLDERS = 2;
	private static final ViewGroup.LayoutParams FILL = new ViewGroup.LayoutParams(
			ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

	private final Handler handler = new Handler(Looper.getMainLooper());
	private final Runnable rotate = this::rotateBackground;
	private FrameLayout content;
	/** Two layers that swap, so a new background fades in over the old one. */
	private final ImageView[] layers = new ImageView[2];
	private int front;
	private List<Post> backgrounds = new ArrayList<>();
	private RecyclerView homeList;
	private HomeRows homeRows;
	private int tab = HOME;
	private boolean ready, resumed, homeLoading, connecting;
	private LinearLayout homeButton, likedButton, foldersButton;

	@Override
	protected void onCreate(Bundle state) {
		super.onCreate(state);
		if (Prefs.server() == null) {
			startActivity(new Intent(this, SetupActivity.class));
			finish();
			return;
		}
		FrameLayout root = new FrameLayout(this);
		root.setBackgroundColor(Color.BLACK);
		FrameLayout layerBox = new FrameLayout(this); // bringToFront() stays inside it
		for (int i = 0; i < 2; i++) {
			layers[i] = new ImageView(this);
			layers[i].setScaleType(ImageView.ScaleType.CENTER_CROP);
			Ui.blur(layers[i], 60);
			layerBox.addView(layers[i], FILL);
		}
		root.addView(layerBox, FILL);
		View dim = new View(this);
		dim.setBackgroundColor(0xAA000000);
		root.addView(dim, FILL);
		LinearLayout column = new LinearLayout(this);
		column.setOrientation(LinearLayout.VERTICAL);
		column.addView(topBar());
		content = new FrameLayout(this);
		column.addView(content, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1));
		root.addView(column, FILL);
		setContentView(root);
		// connect() runs from onResume once the splash has let the user in.
	}

	/** [logo]  [Reels] [Liked] [Folders]  [Settings]; the current view is marked. */
	private View topBar() {
		LinearLayout bar = new LinearLayout(this);
		bar.setGravity(Gravity.CENTER_VERTICAL);
		int p = Ui.dp(this, 16);
		bar.setPadding(p * 2, p, p * 2, 0);
		homeButton = Ui.iconButton(this, R.drawable.ic_launcher, "Home", v -> show(HOME));
		bar.addView(homeButton);
		bar.addView(new Space(this), new LinearLayout.LayoutParams(0, 1, 1));
		bar.addView(Ui.iconButton(this, R.drawable.ic_reels, "Reels", v -> Playlist.openReels(this)));
		likedButton = Ui.iconButton(this, R.drawable.ic_favorite, "Liked", v -> show(LIKED));
		bar.addView(likedButton);
		foldersButton = Ui.iconButton(this, R.drawable.ic_folder, "Folders", v -> show(FOLDERS));
		bar.addView(foldersButton);
		bar.addView(new Space(this), new LinearLayout.LayoutParams(0, 1, 1));
		bar.addView(Ui.iconButton(this, R.drawable.ic_settings, "Settings",
				v -> startActivity(new Intent(this, SettingsActivity.class))));
		return bar;
	}

	private void connect() {
		if (connecting)
			return;
		message("Connecting…");
		connecting = true;
		Api.async(() -> {
			if (!Prefs.password().isEmpty())
				Api.login(Prefs.server(), Prefs.password());
			Likes.load();
			return Api.randomFeed();
		}, posts -> {
			connecting = false;
			if (isDestroyed())
				return;
			ready = true;
			backgrounds = posts;
			handler.removeCallbacks(rotate);
			rotateBackground();
			show(tab);
		}, e -> {
			connecting = false;
			if (!isDestroyed())
				failed(e);
		});
	}

	private void failed(Exception e) {
		LinearLayout box = Ui.column(this);
		box.addView(Ui.text(this, "Cannot reach the instance: " + e.getMessage(), 18));
		LinearLayout row = Ui.row(this);
		row.addView(Ui.iconButton(this, R.drawable.ic_refresh, "Retry", v -> connect()));
		row.addView(Ui.iconButton(this, R.drawable.ic_swap, "Change instance",
				v -> startActivity(new Intent(this, SetupActivity.class))));
		box.addView(row);
		setContent(box);
		row.getChildAt(0).requestFocus();
	}

	private void show(int t) {
		if (!ready)
			return;
		tab = t;
		Ui.markCurrent(homeButton, t == HOME);
		Ui.markCurrent(likedButton, t == LIKED);
		Ui.markCurrent(foldersButton, t == FOLDERS);
		if (t == HOME) {
			if (homeList != null) {
				setContent(homeList);
				return;
			}
			message("Loading…");
			if (homeLoading)
				return; // the request in flight shows the list when it lands
			homeLoading = true;
			Api.async(Api::folders, folders -> {
				homeLoading = false;
				homeRows = new HomeRows(this, folders);
				homeList = Ui.list(this, RecyclerView.VERTICAL);
				homeList.setAdapter(homeRows);
				if (tab == HOME)
					setContent(homeList);
			}, e -> {
				homeLoading = false;
				if (tab == t)
					failed(e);
			});
		} else if (t == LIKED) {
			message("Loading…");
			Api.async(Api::likes, posts -> {
				if (tab == LIKED)
					setContent(posts.isEmpty() ? centered("No liked posts yet")
							: Ui.grid(this, Ui.postTiles(this, posts), Ui.POST_W, Ui.POST_H, 6));
			}, e -> {
				if (tab == t)
					failed(e);
			});
		} else {
			message("Loading…");
			Api.async(Api::folders, folders -> {
				if (tab != FOLDERS)
					return;
				List<TileAdapter.Tile> tiles = new ArrayList<>();
				for (Api.Folder f : folders)
					tiles.add(new TileAdapter.Tile(f.avatarUrl, f.name, R.drawable.ic_folder, -1,
							() -> FolderActivity.open(this, f)));
				setContent(Ui.grid(this, tiles, 160, 160, 5));
			}, e -> {
				if (tab == t)
					failed(e);
			});
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		resumed = true;
		if (!ready) {
			if (!connecting && !App.locked)
				connect(); // also retries after an error, e.g. back from Settings
			return;
		}
		handler.removeCallbacks(rotate);
		handler.postDelayed(rotate, Prefs.backgroundSeconds() * 1000L);
		if (tab == LIKED)
			show(LIKED); // unliked posts drop out
		else if (homeRows != null)
			homeRows.refreshHearts(homeList);
	}

	@Override
	protected void onPause() {
		super.onPause();
		resumed = false;
		handler.removeCallbacks(rotate);
	}

	/** Back from Liked or Folders returns to Home; from Home it leaves the app. */
	@Override
	@SuppressWarnings("deprecation") // no predictive back on this TV; onBackPressed still fires
	public void onBackPressed() {
		if (ready && tab != HOME)
			show(HOME);
		else
			super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		handler.removeCallbacks(rotate);
		super.onDestroy();
	}

	private void rotateBackground() {
		if (backgrounds.isEmpty())
			return;
		Post p = backgrounds.get(ThreadLocalRandom.current().nextInt(backgrounds.size()));
		// Never pass the old drawable as a Glide placeholder: Glide may recycle its bitmap.
		ImageView back = layers[1 - front], top = layers[front];
		back.animate().cancel();
		back.setAlpha(0f);
		back.bringToFront();
		Glide.with(back).load(p.media.get(0).thumbnailUrl).listener(new RequestListener<Drawable>() {
			@Override
			public boolean onLoadFailed(GlideException e, Object model, Target<Drawable> target, boolean first) {
				return false;
			}

			@Override
			public boolean onResourceReady(Drawable d, Object model, Target<Drawable> target, DataSource source,
					boolean first) {
				front = 1 - front; // only now, so a failed load leaves the old layer on top
				back.animate().alpha(1f).setDuration(1500).withEndAction(() -> {
					if (!isDestroyed())
						Glide.with(top).clear(top);
				}).start();
				return false;
			}
		}).into(back);
		if (resumed)
			handler.postDelayed(rotate, Prefs.backgroundSeconds() * 1000L);
	}

	private void message(String s) {
		setContent(centered(s));
	}

	private TextView centered(String s) {
		TextView t = Ui.text(this, s, 18);
		t.setGravity(Gravity.CENTER);
		return t;
	}

	private void setContent(View v) {
		content.removeAllViews();
		content.addView(v, FILL);
	}
}