226 lines
8.2 KiB
Java
226 lines
8.2 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.appcompat.app.AppCompatActivity;
|
||
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
||
|
|
|
||
|
|
import com.bumptech.glide.Glide;
|
||
|
|
import com.example.livestreaming.databinding.ActivityStreamerCenterBinding;
|
||
|
|
import com.example.livestreaming.net.ApiClient;
|
||
|
|
import com.example.livestreaming.net.ApiResponse;
|
||
|
|
import com.example.livestreaming.net.AuthStore;
|
||
|
|
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
import retrofit2.Call;
|
||
|
|
import retrofit2.Callback;
|
||
|
|
import retrofit2.Response;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 主播中心页面
|
||
|
|
* 显示主播的直播间、粉丝、收到的礼物等信息
|
||
|
|
*/
|
||
|
|
public class StreamerCenterActivity extends AppCompatActivity {
|
||
|
|
|
||
|
|
private ActivityStreamerCenterBinding binding;
|
||
|
|
|
||
|
|
public static void start(Context context) {
|
||
|
|
Intent intent = new Intent(context, StreamerCenterActivity.class);
|
||
|
|
context.startActivity(intent);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
protected void onCreate(Bundle savedInstanceState) {
|
||
|
|
super.onCreate(savedInstanceState);
|
||
|
|
binding = ActivityStreamerCenterBinding.inflate(getLayoutInflater());
|
||
|
|
setContentView(binding.getRoot());
|
||
|
|
|
||
|
|
setupToolbar();
|
||
|
|
setupClickListeners();
|
||
|
|
loadStreamerData();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void setupToolbar() {
|
||
|
|
binding.toolbar.setNavigationOnClickListener(v -> finish());
|
||
|
|
}
|
||
|
|
|
||
|
|
private void setupClickListeners() {
|
||
|
|
// 开始直播
|
||
|
|
binding.btnStartLive.setOnClickListener(v -> {
|
||
|
|
Intent intent = new Intent(this, BroadcastActivity.class);
|
||
|
|
startActivity(intent);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 粉丝列表
|
||
|
|
binding.layoutFans.setOnClickListener(v -> {
|
||
|
|
Intent intent = new Intent(this, FansListActivity.class);
|
||
|
|
startActivity(intent);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 获赞列表
|
||
|
|
binding.layoutLikes.setOnClickListener(v -> {
|
||
|
|
Intent intent = new Intent(this, LikesListActivity.class);
|
||
|
|
startActivity(intent);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 礼物列表
|
||
|
|
binding.layoutGifts.setOnClickListener(v -> {
|
||
|
|
// TODO: 跳转到礼物详情页
|
||
|
|
Toast.makeText(this, "礼物详情功能开发中", Toast.LENGTH_SHORT).show();
|
||
|
|
});
|
||
|
|
|
||
|
|
// 收益
|
||
|
|
binding.layoutIncome.setOnClickListener(v -> {
|
||
|
|
// TODO: 跳转到收益页面
|
||
|
|
Toast.makeText(this, "收益详情功能开发中", Toast.LENGTH_SHORT).show();
|
||
|
|
});
|
||
|
|
|
||
|
|
// 查看全部礼物
|
||
|
|
binding.tvViewAllGifts.setOnClickListener(v -> {
|
||
|
|
// TODO: 跳转到礼物列表页
|
||
|
|
Toast.makeText(this, "礼物列表功能开发中", Toast.LENGTH_SHORT).show();
|
||
|
|
});
|
||
|
|
|
||
|
|
// 我的直播间
|
||
|
|
binding.cardMyRoom.setOnClickListener(v -> {
|
||
|
|
// TODO: 跳转到直播间管理页
|
||
|
|
Toast.makeText(this, "直播间管理功能开发中", Toast.LENGTH_SHORT).show();
|
||
|
|
});
|
||
|
|
|
||
|
|
// 切换回普通用户
|
||
|
|
binding.btnSwitchToUser.setOnClickListener(v -> {
|
||
|
|
AuthStore.setStreamerMode(this, false);
|
||
|
|
Toast.makeText(this, "已切换回普通用户模式", Toast.LENGTH_SHORT).show();
|
||
|
|
finish();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private void loadStreamerData() {
|
||
|
|
binding.progressBar.setVisibility(View.VISIBLE);
|
||
|
|
|
||
|
|
// 设置基本信息
|
||
|
|
String nickname = AuthStore.getNickname(this);
|
||
|
|
binding.tvNickname.setText(nickname);
|
||
|
|
|
||
|
|
// 加载主播数据
|
||
|
|
loadStreamerStats();
|
||
|
|
loadReceivedGifts();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void loadStreamerStats() {
|
||
|
|
ApiClient.getService(this).getStreamerStats()
|
||
|
|
.enqueue(new Callback<ApiResponse<Map<String, Object>>>() {
|
||
|
|
@Override
|
||
|
|
public void onResponse(Call<ApiResponse<Map<String, Object>>> call,
|
||
|
|
Response<ApiResponse<Map<String, Object>>> response) {
|
||
|
|
binding.progressBar.setVisibility(View.GONE);
|
||
|
|
|
||
|
|
if (response.isSuccessful() && response.body() != null && response.body().isOk()) {
|
||
|
|
Map<String, Object> data = response.body().getData();
|
||
|
|
updateStats(data);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onFailure(Call<ApiResponse<Map<String, Object>>> call, Throwable t) {
|
||
|
|
binding.progressBar.setVisibility(View.GONE);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private void updateStats(Map<String, Object> data) {
|
||
|
|
if (data == null) return;
|
||
|
|
|
||
|
|
// 粉丝数
|
||
|
|
Object fansObj = data.get("fansCount");
|
||
|
|
if (fansObj != null) {
|
||
|
|
binding.tvFansCount.setText(formatCount(((Number) fansObj).intValue()));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 获赞数
|
||
|
|
Object likesObj = data.get("likesCount");
|
||
|
|
if (likesObj != null) {
|
||
|
|
binding.tvLikesCount.setText(formatCount(((Number) likesObj).intValue()));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 礼物数
|
||
|
|
Object giftsObj = data.get("giftsCount");
|
||
|
|
if (giftsObj != null) {
|
||
|
|
binding.tvGiftsCount.setText(formatCount(((Number) giftsObj).intValue()));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 收益
|
||
|
|
Object incomeObj = data.get("totalIncome");
|
||
|
|
if (incomeObj != null) {
|
||
|
|
binding.tvIncomeCount.setText(formatCount(((Number) incomeObj).intValue()));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 主播等级
|
||
|
|
Object levelObj = data.get("streamerLevel");
|
||
|
|
if (levelObj != null) {
|
||
|
|
binding.tvStreamerLevel.setText("主播等级: Lv." + ((Number) levelObj).intValue());
|
||
|
|
}
|
||
|
|
|
||
|
|
// 头像
|
||
|
|
Object avatarObj = data.get("avatar");
|
||
|
|
if (avatarObj != null && !avatarObj.toString().isEmpty()) {
|
||
|
|
Glide.with(this)
|
||
|
|
.load(avatarObj.toString())
|
||
|
|
.placeholder(R.drawable.ic_person_24)
|
||
|
|
.into(binding.ivAvatar);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 直播间状态
|
||
|
|
Object roomStatusObj = data.get("hasActiveRoom");
|
||
|
|
if (roomStatusObj != null && (Boolean) roomStatusObj) {
|
||
|
|
binding.tvRoomStatus.setText("直播间已创建");
|
||
|
|
} else {
|
||
|
|
binding.tvRoomStatus.setText("暂无直播间,点击开始直播创建");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void loadReceivedGifts() {
|
||
|
|
ApiClient.getService(this).getReceivedGifts(1, 10)
|
||
|
|
.enqueue(new Callback<ApiResponse<List<Map<String, Object>>>>() {
|
||
|
|
@Override
|
||
|
|
public void onResponse(Call<ApiResponse<List<Map<String, Object>>>> call,
|
||
|
|
Response<ApiResponse<List<Map<String, Object>>>> response) {
|
||
|
|
if (response.isSuccessful() && response.body() != null && response.body().isOk()) {
|
||
|
|
List<Map<String, Object>> gifts = response.body().getData();
|
||
|
|
if (gifts != null && !gifts.isEmpty()) {
|
||
|
|
binding.tvNoGifts.setVisibility(View.GONE);
|
||
|
|
binding.rvGifts.setVisibility(View.VISIBLE);
|
||
|
|
// TODO: 设置礼物列表适配器
|
||
|
|
} else {
|
||
|
|
binding.tvNoGifts.setVisibility(View.VISIBLE);
|
||
|
|
binding.rvGifts.setVisibility(View.GONE);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
binding.tvNoGifts.setVisibility(View.VISIBLE);
|
||
|
|
binding.rvGifts.setVisibility(View.GONE);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onFailure(Call<ApiResponse<List<Map<String, Object>>>> call, Throwable t) {
|
||
|
|
binding.tvNoGifts.setVisibility(View.VISIBLE);
|
||
|
|
binding.rvGifts.setVisibility(View.GONE);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private String formatCount(int count) {
|
||
|
|
if (count >= 10000) {
|
||
|
|
return String.format("%.1fw", count / 10000.0);
|
||
|
|
} else if (count >= 1000) {
|
||
|
|
return String.format("%.1fk", count / 1000.0);
|
||
|
|
}
|
||
|
|
return String.valueOf(count);
|
||
|
|
}
|
||
|
|
}
|