234 lines
8.4 KiB
Java
234 lines
8.4 KiB
Java
package com.example.livestreaming;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.view.View;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
|
|
import com.example.livestreaming.databinding.ActivityDynamicCommunityBinding;
|
|
import com.example.livestreaming.net.ApiClient;
|
|
import com.example.livestreaming.net.ApiResponse;
|
|
import com.example.livestreaming.net.ApiService;
|
|
import com.example.livestreaming.net.CommunityResponse;
|
|
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import retrofit2.Call;
|
|
import retrofit2.Callback;
|
|
import retrofit2.Response;
|
|
|
|
/**
|
|
* 动态社区页面 - 根据板块ID动态加载消息列表
|
|
*/
|
|
public class DynamicCommunityActivity extends AppCompatActivity {
|
|
|
|
private static final String EXTRA_CATEGORY_ID = "category_id";
|
|
private static final String EXTRA_CATEGORY_NAME = "category_name";
|
|
|
|
private ActivityDynamicCommunityBinding binding;
|
|
private ApiService apiService;
|
|
private PostAdapter postAdapter;
|
|
|
|
private int categoryId;
|
|
private String categoryName;
|
|
private int currentPage = 1;
|
|
private boolean isLoading = false;
|
|
private boolean hasMore = true;
|
|
|
|
public static void start(Context context, int categoryId, String categoryName) {
|
|
Intent intent = new Intent(context, DynamicCommunityActivity.class);
|
|
intent.putExtra(EXTRA_CATEGORY_ID, categoryId);
|
|
intent.putExtra(EXTRA_CATEGORY_NAME, categoryName);
|
|
context.startActivity(intent);
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
binding = ActivityDynamicCommunityBinding.inflate(getLayoutInflater());
|
|
setContentView(binding.getRoot());
|
|
|
|
categoryId = getIntent().getIntExtra(EXTRA_CATEGORY_ID, 0);
|
|
categoryName = getIntent().getStringExtra(EXTRA_CATEGORY_NAME);
|
|
|
|
apiService = ApiClient.getService(this);
|
|
|
|
setupUI();
|
|
setupRecyclerView();
|
|
setupSwipeRefresh();
|
|
setupBottomNav();
|
|
|
|
loadMessages(true);
|
|
}
|
|
|
|
private void setupUI() {
|
|
binding.titleText.setText(categoryName != null ? categoryName : "动态社区");
|
|
binding.backButton.setOnClickListener(v -> finish());
|
|
|
|
// 发布按钮
|
|
binding.fabPublish.setOnClickListener(v -> {
|
|
// TODO: 打开发布消息页面
|
|
Toast.makeText(this, "发布功能开发中", Toast.LENGTH_SHORT).show();
|
|
});
|
|
}
|
|
|
|
private void setupRecyclerView() {
|
|
postAdapter = new PostAdapter(this);
|
|
binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
|
binding.recyclerView.setAdapter(postAdapter);
|
|
|
|
postAdapter.setOnItemClickListener(post -> {
|
|
// 点击帖子查看详情
|
|
Toast.makeText(this, "查看: " + post.getContent(), Toast.LENGTH_SHORT).show();
|
|
});
|
|
|
|
// 加载更多
|
|
binding.recyclerView.addOnScrollListener(new androidx.recyclerview.widget.RecyclerView.OnScrollListener() {
|
|
@Override
|
|
public void onScrolled(@NonNull androidx.recyclerview.widget.RecyclerView recyclerView, int dx, int dy) {
|
|
super.onScrolled(recyclerView, dx, dy);
|
|
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
|
|
if (layoutManager != null) {
|
|
int totalItemCount = layoutManager.getItemCount();
|
|
int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
|
|
if (!isLoading && hasMore && lastVisibleItem >= totalItemCount - 3) {
|
|
loadMessages(false);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void setupSwipeRefresh() {
|
|
binding.swipeRefresh.setColorSchemeResources(R.color.purple_500, R.color.pink_500);
|
|
binding.swipeRefresh.setOnRefreshListener(() -> loadMessages(true));
|
|
}
|
|
|
|
private void setupBottomNav() {
|
|
BottomNavigationView bottomNav = binding.bottomNavInclude.bottomNavigation;
|
|
bottomNav.setSelectedItemId(R.id.nav_friends);
|
|
UnreadMessageManager.updateBadge(bottomNav);
|
|
|
|
bottomNav.setOnItemSelectedListener(item -> {
|
|
int id = item.getItemId();
|
|
if (id == R.id.nav_home) {
|
|
startActivity(new Intent(this, MainActivity.class));
|
|
finish();
|
|
return true;
|
|
}
|
|
if (id == R.id.nav_friends) {
|
|
startActivity(new Intent(this, FishPondWebViewActivity.class));
|
|
finish();
|
|
return true;
|
|
}
|
|
if (id == R.id.nav_wish_tree) {
|
|
WishTreeWebViewActivity.start(this);
|
|
finish();
|
|
return true;
|
|
}
|
|
if (id == R.id.nav_messages) {
|
|
MessagesActivity.start(this);
|
|
finish();
|
|
return true;
|
|
}
|
|
if (id == R.id.nav_profile) {
|
|
ProfileActivity.start(this);
|
|
finish();
|
|
return true;
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
|
|
private void loadMessages(boolean refresh) {
|
|
if (isLoading) return;
|
|
isLoading = true;
|
|
|
|
if (refresh) {
|
|
currentPage = 1;
|
|
hasMore = true;
|
|
}
|
|
|
|
apiService.getCommunityMessages(categoryId, currentPage, 20).enqueue(new Callback<ApiResponse<CommunityResponse.MessagePage>>() {
|
|
@Override
|
|
public void onResponse(@NonNull Call<ApiResponse<CommunityResponse.MessagePage>> call,
|
|
@NonNull Response<ApiResponse<CommunityResponse.MessagePage>> response) {
|
|
isLoading = false;
|
|
binding.swipeRefresh.setRefreshing(false);
|
|
|
|
if (response.isSuccessful() && response.body() != null && response.body().getData() != null) {
|
|
CommunityResponse.MessagePage page = response.body().getData();
|
|
List<Post> posts = convertToPosts(page.list);
|
|
|
|
if (refresh) {
|
|
postAdapter.setPosts(posts);
|
|
} else {
|
|
postAdapter.addPosts(posts);
|
|
}
|
|
|
|
hasMore = page.list != null && page.list.size() >= 20;
|
|
currentPage++;
|
|
|
|
updateEmptyState(postAdapter.getItemCount() == 0);
|
|
} else {
|
|
if (refresh) {
|
|
updateEmptyState(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(@NonNull Call<ApiResponse<CommunityResponse.MessagePage>> call, @NonNull Throwable t) {
|
|
isLoading = false;
|
|
binding.swipeRefresh.setRefreshing(false);
|
|
Toast.makeText(DynamicCommunityActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
|
|
}
|
|
});
|
|
}
|
|
|
|
private List<Post> convertToPosts(List<CommunityResponse.Message> messages) {
|
|
List<Post> posts = new ArrayList<>();
|
|
if (messages == null) return posts;
|
|
|
|
for (CommunityResponse.Message msg : messages) {
|
|
Post post = new Post();
|
|
post.setId(msg.id);
|
|
post.setContent(msg.content);
|
|
post.setAuthorName(msg.nickname);
|
|
post.setAuthorAvatar(msg.avatar);
|
|
post.setImagesList(msg.getImageList()); // 使用getImageList方法获取图片列表
|
|
post.setLikeCount(msg.likeCount);
|
|
post.setCommentCount(msg.commentCount);
|
|
post.setCreateTime(msg.createTime);
|
|
posts.add(post);
|
|
}
|
|
return posts;
|
|
}
|
|
|
|
private void updateEmptyState(boolean isEmpty) {
|
|
if (isEmpty) {
|
|
binding.emptyView.setVisibility(View.VISIBLE);
|
|
binding.recyclerView.setVisibility(View.GONE);
|
|
} else {
|
|
binding.emptyView.setVisibility(View.GONE);
|
|
binding.recyclerView.setVisibility(View.VISIBLE);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
if (binding != null) {
|
|
UnreadMessageManager.updateBadge(binding.bottomNavInclude.bottomNavigation);
|
|
}
|
|
}
|
|
}
|