401 lines
16 KiB
Java
401 lines
16 KiB
Java
|
|
package com.example.livestreaming;
|
||
|
|
|
||
|
|
import android.content.Context;
|
||
|
|
import android.content.Intent;
|
||
|
|
import android.os.Bundle;
|
||
|
|
import android.view.LayoutInflater;
|
||
|
|
import android.view.View;
|
||
|
|
import android.view.ViewGroup;
|
||
|
|
import android.widget.ImageView;
|
||
|
|
import android.widget.TextView;
|
||
|
|
import android.widget.Toast;
|
||
|
|
|
||
|
|
import androidx.annotation.NonNull;
|
||
|
|
import androidx.annotation.Nullable;
|
||
|
|
import androidx.appcompat.app.AppCompatActivity;
|
||
|
|
import androidx.fragment.app.Fragment;
|
||
|
|
import androidx.fragment.app.FragmentActivity;
|
||
|
|
import androidx.recyclerview.widget.GridLayoutManager;
|
||
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||
|
|
import androidx.recyclerview.widget.RecyclerView;
|
||
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
||
|
|
import androidx.viewpager2.adapter.FragmentStateAdapter;
|
||
|
|
import androidx.viewpager2.widget.ViewPager2;
|
||
|
|
|
||
|
|
import com.example.livestreaming.net.ApiClient;
|
||
|
|
import com.example.livestreaming.net.ApiResponse;
|
||
|
|
import com.example.livestreaming.net.PageResponse;
|
||
|
|
import com.example.livestreaming.net.Room;
|
||
|
|
import com.example.livestreaming.net.WorksResponse;
|
||
|
|
import com.google.android.material.tabs.TabLayout;
|
||
|
|
import com.google.android.material.tabs.TabLayoutMediator;
|
||
|
|
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
import retrofit2.Call;
|
||
|
|
import retrofit2.Callback;
|
||
|
|
import retrofit2.Response;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 我的点赞页面 - 分类显示作品点赞和直播间点赞
|
||
|
|
*/
|
||
|
|
public class MyLikesActivity extends AppCompatActivity {
|
||
|
|
|
||
|
|
private TabLayout tabLayout;
|
||
|
|
private ViewPager2 viewPager;
|
||
|
|
|
||
|
|
public static void start(Context context) {
|
||
|
|
Intent intent = new Intent(context, MyLikesActivity.class);
|
||
|
|
context.startActivity(intent);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
protected void onCreate(Bundle savedInstanceState) {
|
||
|
|
super.onCreate(savedInstanceState);
|
||
|
|
setContentView(R.layout.activity_my_likes);
|
||
|
|
|
||
|
|
setupToolbar();
|
||
|
|
setupViewPager();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void setupToolbar() {
|
||
|
|
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
|
||
|
|
if (toolbar != null) {
|
||
|
|
setSupportActionBar(toolbar);
|
||
|
|
if (getSupportActionBar() != null) {
|
||
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||
|
|
}
|
||
|
|
toolbar.setNavigationOnClickListener(v -> finish());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void setupViewPager() {
|
||
|
|
tabLayout = findViewById(R.id.tabLayout);
|
||
|
|
viewPager = findViewById(R.id.viewPager);
|
||
|
|
|
||
|
|
LikesPagerAdapter adapter = new LikesPagerAdapter(this);
|
||
|
|
viewPager.setAdapter(adapter);
|
||
|
|
|
||
|
|
new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
|
||
|
|
switch (position) {
|
||
|
|
case 0:
|
||
|
|
tab.setText("作品");
|
||
|
|
break;
|
||
|
|
case 1:
|
||
|
|
tab.setText("直播间");
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}).attach();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* ViewPager适配器
|
||
|
|
*/
|
||
|
|
private static class LikesPagerAdapter extends FragmentStateAdapter {
|
||
|
|
public LikesPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
|
||
|
|
super(fragmentActivity);
|
||
|
|
}
|
||
|
|
|
||
|
|
@NonNull
|
||
|
|
@Override
|
||
|
|
public Fragment createFragment(int position) {
|
||
|
|
if (position == 0) {
|
||
|
|
return new LikedWorksFragment();
|
||
|
|
} else {
|
||
|
|
return new LikedRoomsFragment();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public int getItemCount() {
|
||
|
|
return 2;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 点赞的作品Fragment
|
||
|
|
*/
|
||
|
|
public static class LikedWorksFragment extends Fragment {
|
||
|
|
private RecyclerView recyclerView;
|
||
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
||
|
|
private View emptyView;
|
||
|
|
private View loadingView;
|
||
|
|
private WorksAdapter adapter;
|
||
|
|
private final List<WorksResponse> likedWorks = new ArrayList<>();
|
||
|
|
private int currentPage = 1;
|
||
|
|
private boolean isLoading = false;
|
||
|
|
private boolean hasMore = true;
|
||
|
|
|
||
|
|
@Nullable
|
||
|
|
@Override
|
||
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||
|
|
return inflater.inflate(R.layout.fragment_list_content, container, false);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||
|
|
super.onViewCreated(view, savedInstanceState);
|
||
|
|
initViews(view);
|
||
|
|
setupRecyclerView();
|
||
|
|
loadData();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void initViews(View view) {
|
||
|
|
recyclerView = view.findViewById(R.id.recyclerView);
|
||
|
|
swipeRefreshLayout = view.findViewById(R.id.swipeRefreshLayout);
|
||
|
|
emptyView = view.findViewById(R.id.emptyView);
|
||
|
|
loadingView = view.findViewById(R.id.loadingView);
|
||
|
|
|
||
|
|
ImageView emptyIcon = view.findViewById(R.id.emptyIcon);
|
||
|
|
TextView emptyText = view.findViewById(R.id.emptyText);
|
||
|
|
if (emptyIcon != null) emptyIcon.setImageResource(R.drawable.ic_like_filled_24);
|
||
|
|
if (emptyText != null) emptyText.setText("还没有点赞过作品");
|
||
|
|
|
||
|
|
swipeRefreshLayout.setOnRefreshListener(() -> {
|
||
|
|
currentPage = 1;
|
||
|
|
hasMore = true;
|
||
|
|
loadData();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private void setupRecyclerView() {
|
||
|
|
adapter = new WorksAdapter(work -> {
|
||
|
|
if (work != null && getActivity() != null) {
|
||
|
|
WorkDetailActivity.start(getActivity(), String.valueOf(work.getId()));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
|
||
|
|
recyclerView.setAdapter(adapter);
|
||
|
|
|
||
|
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||
|
|
@Override
|
||
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||
|
|
if (dy > 0 && !isLoading && hasMore) {
|
||
|
|
GridLayoutManager lm = (GridLayoutManager) recyclerView.getLayoutManager();
|
||
|
|
if (lm != null) {
|
||
|
|
int total = lm.getItemCount();
|
||
|
|
int lastVisible = lm.findLastVisibleItemPosition();
|
||
|
|
if (lastVisible >= total - 4) {
|
||
|
|
currentPage++;
|
||
|
|
loadData();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private void loadData() {
|
||
|
|
if (isLoading || getContext() == null) return;
|
||
|
|
isLoading = true;
|
||
|
|
if (currentPage == 1) showLoading();
|
||
|
|
|
||
|
|
ApiClient.getService(getContext())
|
||
|
|
.getMyLikedWorks(currentPage, 20)
|
||
|
|
.enqueue(new Callback<ApiResponse<PageResponse<WorksResponse>>>() {
|
||
|
|
@Override
|
||
|
|
public void onResponse(Call<ApiResponse<PageResponse<WorksResponse>>> call,
|
||
|
|
Response<ApiResponse<PageResponse<WorksResponse>>> response) {
|
||
|
|
isLoading = false;
|
||
|
|
hideLoading();
|
||
|
|
swipeRefreshLayout.setRefreshing(false);
|
||
|
|
|
||
|
|
if (response.isSuccessful() && response.body() != null && response.body().isOk()) {
|
||
|
|
PageResponse<WorksResponse> pageData = response.body().getData();
|
||
|
|
if (pageData != null && pageData.getList() != null) {
|
||
|
|
if (currentPage == 1) likedWorks.clear();
|
||
|
|
likedWorks.addAll(pageData.getList());
|
||
|
|
adapter.submitList(new ArrayList<>(likedWorks));
|
||
|
|
hasMore = pageData.getList().size() >= 20;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
updateEmptyState();
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onFailure(Call<ApiResponse<PageResponse<WorksResponse>>> call, Throwable t) {
|
||
|
|
isLoading = false;
|
||
|
|
hideLoading();
|
||
|
|
swipeRefreshLayout.setRefreshing(false);
|
||
|
|
if (getContext() != null) {
|
||
|
|
Toast.makeText(getContext(), "加载失败", Toast.LENGTH_SHORT).show();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private void showLoading() {
|
||
|
|
if (loadingView != null) loadingView.setVisibility(View.VISIBLE);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void hideLoading() {
|
||
|
|
if (loadingView != null) loadingView.setVisibility(View.GONE);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void updateEmptyState() {
|
||
|
|
if (likedWorks.isEmpty()) {
|
||
|
|
emptyView.setVisibility(View.VISIBLE);
|
||
|
|
recyclerView.setVisibility(View.GONE);
|
||
|
|
} else {
|
||
|
|
emptyView.setVisibility(View.GONE);
|
||
|
|
recyclerView.setVisibility(View.VISIBLE);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 点赞的直播间Fragment
|
||
|
|
*/
|
||
|
|
public static class LikedRoomsFragment extends Fragment {
|
||
|
|
private RecyclerView recyclerView;
|
||
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
||
|
|
private View emptyView;
|
||
|
|
private View loadingView;
|
||
|
|
private RoomsAdapter adapter;
|
||
|
|
private final List<Room> likedRooms = new ArrayList<>();
|
||
|
|
private int currentPage = 1;
|
||
|
|
private boolean isLoading = false;
|
||
|
|
private boolean hasMore = true;
|
||
|
|
|
||
|
|
@Nullable
|
||
|
|
@Override
|
||
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||
|
|
return inflater.inflate(R.layout.fragment_list_content, container, false);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||
|
|
super.onViewCreated(view, savedInstanceState);
|
||
|
|
initViews(view);
|
||
|
|
setupRecyclerView();
|
||
|
|
loadData();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void initViews(View view) {
|
||
|
|
recyclerView = view.findViewById(R.id.recyclerView);
|
||
|
|
swipeRefreshLayout = view.findViewById(R.id.swipeRefreshLayout);
|
||
|
|
emptyView = view.findViewById(R.id.emptyView);
|
||
|
|
loadingView = view.findViewById(R.id.loadingView);
|
||
|
|
|
||
|
|
ImageView emptyIcon = view.findViewById(R.id.emptyIcon);
|
||
|
|
TextView emptyText = view.findViewById(R.id.emptyText);
|
||
|
|
if (emptyIcon != null) emptyIcon.setImageResource(R.drawable.ic_live_24);
|
||
|
|
if (emptyText != null) emptyText.setText("还没有点赞过直播间");
|
||
|
|
|
||
|
|
swipeRefreshLayout.setOnRefreshListener(() -> {
|
||
|
|
currentPage = 1;
|
||
|
|
hasMore = true;
|
||
|
|
loadData();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private void setupRecyclerView() {
|
||
|
|
adapter = new RoomsAdapter(room -> {
|
||
|
|
if (room != null && getActivity() != null) {
|
||
|
|
Intent intent = new Intent(getActivity(), RoomDetailActivity.class);
|
||
|
|
intent.putExtra(RoomDetailActivity.EXTRA_ROOM_ID, room.getId());
|
||
|
|
startActivity(intent);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||
|
|
recyclerView.setAdapter(adapter);
|
||
|
|
|
||
|
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||
|
|
@Override
|
||
|
|
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||
|
|
if (dy > 0 && !isLoading && hasMore) {
|
||
|
|
LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();
|
||
|
|
if (lm != null) {
|
||
|
|
int total = lm.getItemCount();
|
||
|
|
int lastVisible = lm.findLastVisibleItemPosition();
|
||
|
|
if (lastVisible >= total - 2) {
|
||
|
|
currentPage++;
|
||
|
|
loadData();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private void loadData() {
|
||
|
|
if (isLoading || getContext() == null) return;
|
||
|
|
isLoading = true;
|
||
|
|
if (currentPage == 1) showLoading();
|
||
|
|
|
||
|
|
ApiClient.getService(getContext())
|
||
|
|
.getMyLikedRooms(currentPage, 20)
|
||
|
|
.enqueue(new Callback<ApiResponse<PageResponse<Map<String, Object>>>>() {
|
||
|
|
@Override
|
||
|
|
public void onResponse(Call<ApiResponse<PageResponse<Map<String, Object>>>> call,
|
||
|
|
Response<ApiResponse<PageResponse<Map<String, Object>>>> response) {
|
||
|
|
isLoading = false;
|
||
|
|
hideLoading();
|
||
|
|
swipeRefreshLayout.setRefreshing(false);
|
||
|
|
|
||
|
|
if (response.isSuccessful() && response.body() != null && response.body().isOk()) {
|
||
|
|
PageResponse<Map<String, Object>> pageData = response.body().getData();
|
||
|
|
if (pageData != null && pageData.getList() != null) {
|
||
|
|
if (currentPage == 1) likedRooms.clear();
|
||
|
|
likedRooms.addAll(convertToRooms(pageData.getList()));
|
||
|
|
adapter.submitList(new ArrayList<>(likedRooms));
|
||
|
|
hasMore = pageData.getList().size() >= 20;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
updateEmptyState();
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onFailure(Call<ApiResponse<PageResponse<Map<String, Object>>>> call, Throwable t) {
|
||
|
|
isLoading = false;
|
||
|
|
hideLoading();
|
||
|
|
swipeRefreshLayout.setRefreshing(false);
|
||
|
|
if (getContext() != null) {
|
||
|
|
Toast.makeText(getContext(), "加载失败", Toast.LENGTH_SHORT).show();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private List<Room> convertToRooms(List<Map<String, Object>> roomMaps) {
|
||
|
|
List<Room> rooms = new ArrayList<>();
|
||
|
|
for (Map<String, Object> map : roomMaps) {
|
||
|
|
Room room = new Room();
|
||
|
|
if (map.containsKey("roomId")) room.setId(map.get("roomId"));
|
||
|
|
if (map.containsKey("roomTitle")) room.setTitle((String) map.get("roomTitle"));
|
||
|
|
if (map.containsKey("streamerName")) room.setStreamerName((String) map.get("streamerName"));
|
||
|
|
if (map.containsKey("streamerId")) room.setStreamerId(((Number) map.get("streamerId")).intValue());
|
||
|
|
if (map.containsKey("coverImage")) room.setCoverImage((String) map.get("coverImage"));
|
||
|
|
if (map.containsKey("isLive")) {
|
||
|
|
Object isLive = map.get("isLive");
|
||
|
|
if (isLive instanceof Number) room.setLive(((Number) isLive).intValue() == 1);
|
||
|
|
else if (isLive instanceof Boolean) room.setLive((Boolean) isLive);
|
||
|
|
}
|
||
|
|
rooms.add(room);
|
||
|
|
}
|
||
|
|
return rooms;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void showLoading() {
|
||
|
|
if (loadingView != null) loadingView.setVisibility(View.VISIBLE);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void hideLoading() {
|
||
|
|
if (loadingView != null) loadingView.setVisibility(View.GONE);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void updateEmptyState() {
|
||
|
|
if (likedRooms.isEmpty()) {
|
||
|
|
emptyView.setVisibility(View.VISIBLE);
|
||
|
|
recyclerView.setVisibility(View.GONE);
|
||
|
|
} else {
|
||
|
|
emptyView.setVisibility(View.GONE);
|
||
|
|
recyclerView.setVisibility(View.VISIBLE);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|