aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/mygram/droid/Playlist.java
blob: b8853979bd06d1b6274182aac8223713f841805b (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
/*
 * 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.content.Context;
import android.content.Intent;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ThreadLocalRandom;

/** Hands a post list to ViewerActivity; intents cannot carry hundreds of posts. */
final class Playlist {
	/** Shared with the caller (e.g. a folder grid), not copied, so both see appended pages. */
	static List<Post> posts = new ArrayList<>();
	static int start;
	/** Fetches the next page of posts; null when there is nothing more. */
	static Callable<List<Post>> more;

	private Playlist() {
	}

	static void open(Context c, List<Post> list, int startIndex, Callable<List<Post>> pager) {
		posts = list;
		start = startIndex;
		more = pager;
		c.startActivity(new Intent(c, ViewerActivity.class));
	}

	static void openReels(Context c) {
		open(c, new ArrayList<>(), 0, Api.reelsPages(1 + ThreadLocalRandom.current().nextInt(1_000_000)));
	}

	/** Appends the posts not already in the list; returns how many were added. */
	static int append(List<Post> page) {
		Set<Integer> ids = new HashSet<>();
		for (Post p : posts)
			ids.add(p.id);
		int old = posts.size();
		for (Post p : page)
			if (ids.add(p.id))
				posts.add(p);
		return posts.size() - old;
	}
}