“虞渠成” f0a6f0f764 feat: 完成设备置顶、重命名、分享功能迭代
本次提交包含以下核心更新:
1. 新增设备置顶功能,支持将设备在列表中优先展示
2. 实现设备重命名、分享、取消共享功能
3. 优化首页设备列表滑动交互,重构设备项UI菜单逻辑
4. 新增is_top设备字段并完善设备列表排序逻辑
5. 调整部分UI布局与服务器接口地址
6. 补充多语言文案与新增提示文本
7. 修复绑定设备错误提示适配逻辑
2026-08-19 17:08:15 +08:00

209 lines
5.9 KiB
C#
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.

using System;
using System.Threading.Tasks;
using Kill.Managers;
using Kill.Network;
using UnityEngine;
using UnityEngine.UI;
using Kill.UI.Pages;
using Kill.UI.Components;
namespace Kill.UI.Pages
{
/// <summary>
/// 设备重命名页面
/// </summary>
public class RenamePage : MonoBehaviour
{
[Header("输入组件")]
public InputField nameInput;
[Header("按钮")]
public Button confirmBtn;
public Button cancelBtn;
private string currentName;
private Action<string> onConfirmCallback;
private int type = 0;//0设备重命名 1用户名重命名
private string deviceMac;// 待重命名设备的蓝牙MAC为空时取当前选中设备
/// <summary>
/// 初始化页面
/// </summary>
/// <param name="deviceName">当前名称</param>
/// <param name="onConfirm">确认回调,返回名称</param>
/// <param name="type">类型 0 重命名设备 1 用户名重命名</param>
/// <param name="deviceMac">设备蓝牙MAC重命名非选中设备时传入</param>
public void Init(string name, Action<string> onConfirm, int type = 0, string deviceMac = null)
{
currentName = name;
onConfirmCallback = onConfirm;
this.type = type;
this.deviceMac = deviceMac;
// 初始化输入框
if (nameInput != null)
{
nameInput.text = name;
}
// 绑定按钮事件
if (confirmBtn != null)
confirmBtn.onClick.AddListener(OnConfirmClick);
if (cancelBtn != null)
cancelBtn.onClick.AddListener(OnCancelClick);
// 注册返回事件
UIManager.Instance.RegisterBackAction(Back);
}
/// <summary>
/// 点击确认按钮
/// </summary>
private async void OnConfirmClick()
{
string newName = nameInput != null ? nameInput.text.Trim() : "";
// 如果为空或名称没有变化,直接返回
if (string.IsNullOrEmpty(newName) || newName == currentName)
{
Back();
return;
}
// 显示加载中
LoadingUI.Show();
if (type == 0)
{
// 调用服务器接口修改设备名称
UpdateDeviceNameAsync(newName);
}
else if (type == 1)
{
// 调用服务器接口修改用户名
UpdateUserNameAsync(newName);
}
}
/// <summary>
/// 调用服务器接口更新设备名称
/// </summary>
private async void UpdateDeviceNameAsync(string newName)
{
try
{
// 构建请求数据
var requestData = new DeviceNameRequest
{
device_name = newName,
user_id = DataManager.Instance.userInfo.id,
device_sn = string.IsNullOrEmpty(deviceMac)
? DataManager.Instance.selectedDevice.ble_mac
: deviceMac
};
// 发送PUT请求
var response = await HttpRequestManager.Instance.Put<string>("/api/v1/device/name", requestData);
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
LoadingUI.Hide();
// 回调新名称
onConfirmCallback?.Invoke(newName);
Back();
},
onError: null
);
}
catch (Exception ex)
{
Debug.LogError($"更新设备名称失败: {ex.Message}");
}
}
private async void UpdateUserNameAsync(string newName)
{
try
{
// 构建请求数据
var requestData = new UserNameRequest
{
username = newName
};
// 发送PUT请求
var response = await HttpRequestManager.Instance.Put<string>("/api/v1/user/username", requestData);
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
LoadingUI.Hide();
// 回调新名称
onConfirmCallback?.Invoke(newName);
Back();
},
onError: null
);
}
catch (Exception ex)
{
Debug.LogError($"更新设备名称失败: {ex.Message}");
}
}
/// <summary>
/// 点击取消按钮
/// </summary>
private void OnCancelClick()
{
Back();
}
/// <summary>
/// 返回/关闭页面
/// </summary>
private void Back()
{
UIManager.Instance.ClearBackAction();
Destroy(gameObject);
}
private void OnDestroy()
{
// 移除事件监听
if (confirmBtn != null)
confirmBtn.onClick.RemoveListener(OnConfirmClick);
if (cancelBtn != null)
cancelBtn.onClick.RemoveListener(OnCancelClick);
}
}
/// <summary>
/// 设备名称请求数据
/// </summary>
[Serializable]
public class DeviceNameRequest
{
public string device_name;
public string user_id;
public string device_sn;
}
/// <summary>
/// 更新用户名请求数据
/// </summary>
[Serializable]
public class UserNameRequest
{
public string username;
}
}