fixed refresh on rate and auto refresh, fixed some button designs and username in post title

This commit is contained in:
David Katrinka 2026-01-15 20:25:09 +01:00
parent 2cbd885b62
commit e3c7675c6a
4 changed files with 60 additions and 24 deletions

View File

@ -2,6 +2,7 @@ package com.example.gallery;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.ImageButton; import android.widget.ImageButton;
@ -36,6 +37,16 @@ public class MainActivity extends AppCompatActivity {
private TextView menuLogout, menuNewPost; private TextView menuLogout, menuNewPost;
private ImageButton filterBtnToolbar; private ImageButton filterBtnToolbar;
private final int REFRESH_INTERVAL = 30000; // 30 seconds
private final Handler autoRefreshHandler = new Handler();
private final Runnable autoRefreshRunnable = new Runnable() {
@Override
public void run() {
loadPublications(); // reload posts
autoRefreshHandler.postDelayed(this, REFRESH_INTERVAL); // schedule next refresh
}
};
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -199,5 +210,14 @@ public class MainActivity extends AppCompatActivity {
super.onResume(); super.onResume();
updateAuthUI(); updateAuthUI();
loadPublications(); loadPublications();
autoRefreshHandler.postDelayed(autoRefreshRunnable, REFRESH_INTERVAL);
}
@Override
protected void onPause() {
super.onPause();
autoRefreshHandler.removeCallbacks(autoRefreshRunnable);
} }
} }

View File

@ -16,6 +16,7 @@ import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.example.gallery.MainActivity;
import com.example.gallery.R; import com.example.gallery.R;
import com.example.gallery.VideoPlayerActivity; import com.example.gallery.VideoPlayerActivity;
import com.example.gallery.api.ApiClient; import com.example.gallery.api.ApiClient;
@ -117,28 +118,31 @@ public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.PostViewHold
// ---------- TEXT ---------- // ---------- TEXT ----------
holder.postDescription.setText(post.description == null ? "" : post.description); holder.postDescription.setText(post.description == null ? "" : post.description);
holder.postUserName.setText(post.username == null ? "User" : post.username); holder.postUserName.setText(post.user_detail == null ? "User" : post.getUsername());
holder.mediaTypeBadge.setText( holder.mediaTypeBadge.setText(
post.content_type == null ? "MEDIA" : post.content_type.toUpperCase()); post.content_type == null ? "MEDIA" : post.content_type.toUpperCase());
// ---------- MEDIA ---------- // ---------- MEDIA ----------
if ("video".equalsIgnoreCase(post.content_type)) { if ("video".equalsIgnoreCase(post.content_type)) {
// Load video thumbnail
Glide.with(context).asBitmap().load(post.video).into(holder.postImage); Glide.with(context).asBitmap().load(post.video).into(holder.postImage);
holder.videoPlayButton.setVisibility(View.VISIBLE);
View.OnClickListener play = v -> { // Make the thumbnail clickable to play video
holder.postImage.setOnClickListener(v -> {
Intent i = new Intent(context, VideoPlayerActivity.class); Intent i = new Intent(context, VideoPlayerActivity.class);
i.putExtra("videoUrl", post.video); i.putExtra("videoUrl", post.video);
context.startActivity(i); context.startActivity(i);
}; });
holder.postImage.setOnClickListener(play);
holder.videoPlayButton.setOnClickListener(play);
} else { } else {
holder.videoPlayButton.setVisibility(View.GONE); // Load image for normal posts
Glide.with(context).load(post.image).into(holder.postImage); Glide.with(context).load(post.image).into(holder.postImage);
} }
// ---------- AVERAGE ---------- // ---------- AVERAGE ----------
holder.txtAverageValue.setText(String.format("%.1f", post.average_score)); holder.txtAverageValue.setText(String.format("%.1f", post.average_score));
holder.averageRatingBar.setRating(post.average_score); holder.averageRatingBar.setRating(post.average_score);
@ -175,6 +179,7 @@ public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.PostViewHold
current.user_score = score; current.user_score = score;
if (body.average_score != null) { if (body.average_score != null) {
current.average_score = body.average_score; current.average_score = body.average_score;
} }
@ -193,7 +198,11 @@ public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.PostViewHold
String.format("%.1f", current.average_score) String.format("%.1f", current.average_score)
); );
notifyItemChanged(adapterPosition); if (context instanceof MainActivity) {
((MainActivity) context).runOnUiThread(() -> {
((MainActivity) context).loadPublications();
});
}
} }
@Override @Override
@ -215,7 +224,7 @@ public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.PostViewHold
RatingBar averageRatingBar, userRatingBar; RatingBar averageRatingBar, userRatingBar;
TextView txtAverageValue, yourRating, postDescription, postUserName, mediaTypeBadge; TextView txtAverageValue, yourRating, postDescription, postUserName, mediaTypeBadge;
ImageView postImage, videoPlayButton; ImageView postImage;
PostViewHolder(View itemView) { PostViewHolder(View itemView) {
super(itemView); super(itemView);
@ -229,16 +238,8 @@ public class PostsAdapter extends RecyclerView.Adapter<PostsAdapter.PostViewHold
mediaTypeBadge = itemView.findViewById(R.id.mediaTypeBadge); mediaTypeBadge = itemView.findViewById(R.id.mediaTypeBadge);
postImage = itemView.findViewById(R.id.postImage); postImage = itemView.findViewById(R.id.postImage);
FrameLayout parent = (FrameLayout) postImage.getParent();
videoPlayButton = new ImageView(itemView.getContext());
videoPlayButton.setImageResource(R.drawable.ic_launcher_background);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT
);
params.gravity = Gravity.CENTER;
parent.addView(videoPlayButton);
videoPlayButton.setVisibility(View.GONE);
} }
} }
} }

View File

@ -1,5 +1,6 @@
package com.example.gallery.models; package com.example.gallery.models;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable; import java.io.Serializable;
public class Publication implements Serializable { public class Publication implements Serializable {
@ -9,12 +10,20 @@ public class Publication implements Serializable {
public String image; public String image;
public String video; public String video;
public String content_type; public String content_type;
public boolean is_pinned; public boolean is_pinned;
public Float average_score; public Float average_score;
public Integer user_score; public Integer user_score;
public String username; @SerializedName("user_detail")
public UserDetail user_detail;
public String time_created; public String time_created;
public String getUsername() {
return user_detail != null ? user_detail.username : "User";
}
public static class UserDetail implements Serializable {
public String username;
}
} }

View File

@ -7,6 +7,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/bgColor" android:background="@color/bgColor"
android:fitsSystemWindows="true"
tools:context=".MainActivity"> tools:context=".MainActivity">
<!-- Toolbar --> <!-- Toolbar -->
@ -147,20 +148,25 @@
android:id="@+id/btnLogout" android:id="@+id/btnLogout"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/button_background"
android:text="Logout" android:text="Logout"
android:layout_margin="16dp"
android:visibility="gone" android:visibility="gone"
android:textColor="@color/white"
app:layout_constraintTop_toTopOf="@id/toolbar" app:layout_constraintTop_toTopOf="@id/toolbar"
app:layout_constraintStart_toStartOf="parent"/> app:layout_constraintBottom_toBottomOf="@id/toolbar"
app:layout_constraintStart_toEndOf="@id/burgerMenuBtn"
android:layout_marginStart="8dp"/>
<!-- New Post Button --> <!-- New Post Button -->
<Button <Button
android:id="@+id/newPostBtn" android:id="@+id/newPostBtn"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/button_background"
android:text="New Post" android:text="New Post"
android:layout_margin="16dp" android:layout_margin="16dp"
android:visibility="gone" android:visibility="gone"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/> app:layout_constraintEnd_toEndOf="parent"/>