恢复WorkDetailActivity中的编辑删除功能
- 添加setupActionButton方法,控制编辑删除菜单显示 - 添加showActionMenu方法,显示编辑删除选项对话框 - 添加editWork方法,跳转到编辑页面并传递作品数据 - 添加deleteWork方法,调用后端API删除作品 - 确保只有作品作者才能看到编辑删除菜单 - 修复因Git同步问题丢失的编辑删除核心功能
This commit is contained in:
parent
c0126d9744
commit
a61c50e455
|
|
@ -1354,6 +1354,153 @@ public class WorkDetailActivity extends AppCompatActivity {
|
|||
.show();
|
||||
}
|
||||
|
||||
private void setupActionButton() {
|
||||
// 检查是否应该显示菜单
|
||||
boolean showMenu = getIntent().getBooleanExtra(EXTRA_SHOW_MENU, false);
|
||||
if (!showMenu) {
|
||||
binding.actionButton.setVisibility(View.GONE);
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否是当前用户的作品
|
||||
String currentUserIdStr = AuthStore.getUserId(this);
|
||||
if (currentUserIdStr == null || workItem == null) {
|
||||
binding.actionButton.setVisibility(View.GONE);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
int currentUserId = Integer.parseInt(currentUserIdStr);
|
||||
int authorId = workItem.getAuthorId();
|
||||
|
||||
if (currentUserId != authorId) {
|
||||
// 不是自己的作品,隐藏菜单
|
||||
binding.actionButton.setVisibility(View.GONE);
|
||||
return;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
binding.actionButton.setVisibility(View.GONE);
|
||||
return;
|
||||
}
|
||||
|
||||
// 是自己的作品且需要显示菜单,设置点击事件
|
||||
binding.actionButton.setVisibility(View.VISIBLE);
|
||||
binding.actionButton.setOnClickListener(new DebounceClickListener() {
|
||||
@Override
|
||||
public void onDebouncedClick(View v) {
|
||||
showActionMenu();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void showActionMenu() {
|
||||
// 检查是否是当前用户的作品
|
||||
String currentUserIdStr = AuthStore.getUserId(this);
|
||||
if (currentUserIdStr == null || workItem == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
int currentUserId = Integer.parseInt(currentUserIdStr);
|
||||
int authorId = workItem.getAuthorId();
|
||||
|
||||
if (currentUserId != authorId) {
|
||||
Toast.makeText(this, "只能编辑自己的作品", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return;
|
||||
}
|
||||
|
||||
String[] options = {"编辑", "删除"};
|
||||
new AlertDialog.Builder(this)
|
||||
.setItems(options, (dialog, which) -> {
|
||||
if (which == 0) {
|
||||
// 编辑
|
||||
editWork();
|
||||
} else if (which == 1) {
|
||||
// 删除
|
||||
deleteWork();
|
||||
}
|
||||
})
|
||||
.show();
|
||||
}
|
||||
|
||||
private void editWork() {
|
||||
// 检查登录状态
|
||||
if (!AuthHelper.requireLogin(this, "编辑作品需要登录")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (workItem == null) {
|
||||
Toast.makeText(this, "作品信息不完整", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
// 跳转到编辑页面(复用PublishWorkActivity)
|
||||
Intent intent = new Intent(this, PublishWorkActivity.class);
|
||||
intent.putExtra("edit_mode", true);
|
||||
intent.putExtra("work_id", workItem.getId());
|
||||
intent.putExtra("work_title", workItem.getTitle());
|
||||
intent.putExtra("work_description", workItem.getDescription());
|
||||
intent.putExtra("work_type", workItem.getType().name());
|
||||
intent.putExtra("work_cover_url", workItem.getCoverUrl());
|
||||
|
||||
if (workItem.getType() == WorkItem.WorkType.VIDEO) {
|
||||
intent.putExtra("work_video_url", workItem.getVideoUrl());
|
||||
} else if (workItem.getImageUrls() != null) {
|
||||
intent.putStringArrayListExtra("work_image_urls", new ArrayList<>(workItem.getImageUrls()));
|
||||
}
|
||||
|
||||
startActivityForResult(intent, 1001);
|
||||
}
|
||||
|
||||
private void deleteWork() {
|
||||
new AlertDialog.Builder(this)
|
||||
.setTitle("删除作品")
|
||||
.setMessage("确定要删除这个作品吗?")
|
||||
.setPositiveButton("删除", (dialog, which) -> {
|
||||
// 检查登录状态
|
||||
if (!AuthHelper.requireLogin(this, "删除作品需要登录")) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
long worksId = Long.parseLong(workItem.getId());
|
||||
ApiService apiService = ApiClient.getService(this);
|
||||
Call<ApiResponse<Boolean>> call = apiService.deleteWork(worksId);
|
||||
|
||||
call.enqueue(new retrofit2.Callback<ApiResponse<Boolean>>() {
|
||||
@Override
|
||||
public void onResponse(Call<ApiResponse<Boolean>> call, retrofit2.Response<ApiResponse<Boolean>> response) {
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
ApiResponse<Boolean> apiResponse = response.body();
|
||||
if (apiResponse.getCode() == 200 && Boolean.TRUE.equals(apiResponse.getData())) {
|
||||
Toast.makeText(WorkDetailActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
|
||||
finish();
|
||||
} else {
|
||||
Toast.makeText(WorkDetailActivity.this,
|
||||
apiResponse.getMessage() != null ? apiResponse.getMessage() : "删除失败",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(WorkDetailActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<ApiResponse<Boolean>> call, Throwable t) {
|
||||
Toast.makeText(WorkDetailActivity.this, "网络错误: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
} catch (NumberFormatException e) {
|
||||
Toast.makeText(this, "作品ID格式错误", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
})
|
||||
.setNegativeButton("取消", null)
|
||||
.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user