From bf319064f127e3cf97a46d2c172bdacdc8f8a783 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Thu, 22 Oct 2026 11:15:44 +0200 Subject: Add main screen with top bar, Home rows, Liked and Folders tabs Co-Authored-By: Claude Opus 5.5 --- app/src/main/java/mygram/droid/MainActivity.java | 220 +++++++++++++++++++++++ 1 file changed, 220 insertions(+) (limited to 'app/src/main/java/mygram/droid/MainActivity.java') diff --git a/app/src/main/java/mygram/droid/MainActivity.java b/app/src/main/java/mygram/droid/MainActivity.java index 767dbb1..6f8d76d 100644 --- a/app/src/main/java/mygram/droid/MainActivity.java +++ b/app/src/main/java/mygram/droid/MainActivity.java @@ -12,9 +12,229 @@ * 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 backgrounds = new ArrayList<>(); + private RecyclerView homeList; + private HomeRows homeRows; + private int tab = HOME; + private boolean ready; + + @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(); + } + + /** [logo] [Reels] [Liked] [Folders] [Settings] */ + 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); + LinearLayout logo = Ui.iconButton(this, R.drawable.ic_home, "Home", v -> show(HOME)); + // Foldergram's logo is AGPLv3: loaded from the instance, never bundled. + Glide.with(this).load(Api.resolve(Prefs.server(), "/icon-192.png")).error(R.drawable.ic_home) + .into((ImageView) logo.getChildAt(0)); + bar.addView(logo); + 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))); + bar.addView(Ui.iconButton(this, R.drawable.ic_favorite, "Liked", v -> show(LIKED))); + bar.addView(Ui.iconButton(this, R.drawable.ic_folder, "Folders", v -> show(FOLDERS))); + 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() { + message("Connecting…"); + Api.async(() -> { + if (!Prefs.password().isEmpty()) + Api.login(Prefs.server(), Prefs.password()); + Likes.load(); + return Api.randomFeed(); + }, posts -> { + ready = true; + backgrounds = posts; + handler.removeCallbacks(rotate); + rotateBackground(); + show(tab); + }, this::failed); + } + + 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; + if (t == HOME) { + if (homeList != null) { + setContent(homeList); + return; + } + message("Loading…"); + Api.async(Api::folders, folders -> { + homeRows = new HomeRows(this, folders); + homeList = Ui.list(this, RecyclerView.VERTICAL); + homeList.setAdapter(homeRows); + if (tab == HOME) + setContent(homeList); + }, this::failed); + } 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)); + }, this::failed); + } else { + message("Loading…"); + Api.async(Api::folders, folders -> { + if (tab != FOLDERS) + return; + List 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)); + }, this::failed); + } + } + + @Override + protected void onResume() { + super.onResume(); + if (!ready) + 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(); + handler.removeCallbacks(rotate); + } + + 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.setAlpha(0f); + back.bringToFront(); + Glide.with(this).load(p.media.get(0).thumbnailUrl).listener(new RequestListener() { + @Override + public boolean onLoadFailed(GlideException e, Object model, Target target, boolean first) { + return false; + } + + @Override + public boolean onResourceReady(Drawable d, Object model, Target target, DataSource source, + boolean first) { + back.animate().alpha(1f).setDuration(1500).withEndAction(() -> Glide.with(top).clear(top)).start(); + return false; + } + }).into(back); + front = 1 - front; + 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); + } } -- cgit v1.2.3