zhibo/android-app/app/src/main/java/com/example/livestreaming/NotificationSettingsActivity.java
2025-12-23 18:09:56 +08:00

163 lines
7.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.example.livestreaming;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.livestreaming.databinding.ActivityNotificationSettingsBinding;
import java.util.ArrayList;
import java.util.List;
/**
* 通知设置页面
* 支持各类通知开关和免打扰设置
*/
public class NotificationSettingsActivity extends AppCompatActivity {
private static final String PREFS_NAME = "notification_settings";
private static final String KEY_SYSTEM_NOTIFICATIONS = "system_notifications";
private static final String KEY_FOLLOW_NOTIFICATIONS = "follow_notifications";
private static final String KEY_COMMENT_NOTIFICATIONS = "comment_notifications";
private static final String KEY_MESSAGE_NOTIFICATIONS = "message_notifications";
private static final String KEY_LIVE_NOTIFICATIONS = "live_notifications";
private static final String KEY_DND_ENABLED = "dnd_enabled";
private static final String KEY_DND_START_HOUR = "dnd_start_hour";
private static final String KEY_DND_END_HOUR = "dnd_end_hour";
private ActivityNotificationSettingsBinding binding;
private MoreAdapter adapter;
private SharedPreferences prefs;
public static void start(Context context) {
Intent intent = new Intent(context, NotificationSettingsActivity.class);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityNotificationSettingsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
binding.backButton.setOnClickListener(v -> finish());
binding.titleText.setText("通知设置");
adapter = new MoreAdapter(item -> {
if (item == null) return;
if (item.getType() != MoreItem.Type.ROW) return;
handleItemClick(item);
});
binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
binding.recyclerView.setAdapter(adapter);
refreshItems();
}
private void handleItemClick(MoreItem item) {
String title = item.getTitle() != null ? item.getTitle() : "";
if ("免打扰".equals(title)) {
showDoNotDisturbDialog();
}
}
private void refreshItems() {
// TODO: 接入后端接口 - 获取用户通知设置
// 接口路径: GET /api/users/{userId}/notification/settings
// 请求参数:
// - userId: 用户ID从token中获取
// 返回数据格式: ApiResponse<NotificationSettings>
// NotificationSettings对象应包含: systemEnabled, followEnabled, commentEnabled,
// messageEnabled, liveEnabled, dndEnabled, dndStartHour, dndEndHour等字段
// 首次加载时从接口获取,后续可从本地缓存读取
List<MoreItem> items = new ArrayList<>();
// 系统通知开关
items.add(MoreItem.section("系统通知"));
boolean systemEnabled = prefs.getBoolean(KEY_SYSTEM_NOTIFICATIONS, true);
items.add(MoreItem.row("系统通知", systemEnabled ? "已开启" : "已关闭", R.drawable.ic_notifications_24));
// 消息提醒
items.add(MoreItem.section("消息提醒"));
boolean followEnabled = prefs.getBoolean(KEY_FOLLOW_NOTIFICATIONS, true);
boolean commentEnabled = prefs.getBoolean(KEY_COMMENT_NOTIFICATIONS, true);
boolean messageEnabled = prefs.getBoolean(KEY_MESSAGE_NOTIFICATIONS, true);
boolean liveEnabled = prefs.getBoolean(KEY_LIVE_NOTIFICATIONS, true);
items.add(MoreItem.row("关注提醒", followEnabled ? "已开启" : "已关闭", R.drawable.ic_people_24));
items.add(MoreItem.row("评论提醒", commentEnabled ? "已开启" : "已关闭", R.drawable.ic_chat_24));
items.add(MoreItem.row("私信提醒", messageEnabled ? "已开启" : "已关闭", R.drawable.ic_chat_24));
items.add(MoreItem.row("开播提醒", liveEnabled ? "已开启" : "已关闭", R.drawable.ic_notifications_24));
// 免打扰
items.add(MoreItem.section("免打扰"));
boolean dndEnabled = prefs.getBoolean(KEY_DND_ENABLED, false);
if (dndEnabled) {
int startHour = prefs.getInt(KEY_DND_START_HOUR, 22);
int endHour = prefs.getInt(KEY_DND_END_HOUR, 8);
items.add(MoreItem.row("免打扰", String.format("%02d:00 - %02d:00", startHour, endHour), R.drawable.ic_notifications_24));
} else {
items.add(MoreItem.row("免打扰", "未开启", R.drawable.ic_notifications_24));
}
adapter.submitList(items);
}
private void showDoNotDisturbDialog() {
// 简单的免打扰设置对话框
boolean currentEnabled = prefs.getBoolean(KEY_DND_ENABLED, false);
int startHour = prefs.getInt(KEY_DND_START_HOUR, 22);
int endHour = prefs.getInt(KEY_DND_END_HOUR, 8);
String message = "免打扰功能说明:\n\n" +
"• 开启后,在指定时间段内不会收到通知\n" +
"• 当前设置:" + (currentEnabled ?
String.format("已开启 (%02d:00 - %02d:00)", startHour, endHour) : "未开启") + "\n" +
"• 紧急通知仍会显示\n\n" +
"(此功能待完善)";
new android.app.AlertDialog.Builder(this)
.setTitle("免打扰设置")
.setMessage(message)
.setPositiveButton("开启", (dialog, which) -> {
// TODO: 接入后端接口 - 更新通知设置
// 接口路径: PUT /api/users/{userId}/notification/settings
// 请求参数:
// - userId: 用户ID从token中获取
// - systemEnabled (可选): 系统通知开关
// - followEnabled (可选): 关注提醒开关
// - commentEnabled (可选): 评论提醒开关
// - messageEnabled (可选): 私信提醒开关
// - liveEnabled (可选): 开播提醒开关
// - dndEnabled (可选): 免打扰开关
// - dndStartHour (可选): 免打扰开始时间小时0-23
// - dndEndHour (可选): 免打扰结束时间小时0-23
// 返回数据格式: ApiResponse<{success: boolean}>
// 更新成功后,同步更新本地缓存和界面显示
prefs.edit().putBoolean(KEY_DND_ENABLED, true).apply();
refreshItems();
Toast.makeText(this, "免打扰已开启", Toast.LENGTH_SHORT).show();
})
.setNeutralButton(currentEnabled ? "关闭" : "取消", (dialog, which) -> {
if (currentEnabled) {
prefs.edit().putBoolean(KEY_DND_ENABLED, false).apply();
refreshItems();
Toast.makeText(this, "免打扰已关闭", Toast.LENGTH_SHORT).show();
}
})
.show();
}
}