Skip to content

fix: emit all relevant roles on bluetooth device state change#2903

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
wyu71:master
Dec 25, 2025
Merged

fix: emit all relevant roles on bluetooth device state change#2903
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
wyu71:master

Conversation

@wyu71

@wyu71 wyu71 commented Dec 25, 2025

Copy link
Copy Markdown
Contributor
  • Added ConnectStatus, ConnectStatusText, Battery, and BatteryIconPath roles to dataChanged signal.

Log: emit all relevant roles on bluetooth device state change
pms: BUG-343983

Summary by Sourcery

Bug Fixes:

  • Ensure Bluetooth device state changes notify views about connection and battery-related role updates.

- Added ConnectStatus, ConnectStatusText, Battery, and BatteryIconPath roles to dataChanged signal.

Log: emit all relevant roles on bluetooth device state change
pms: BUG-343983
@sourcery-ai

sourcery-ai Bot commented Dec 25, 2025

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensure that when a Bluetooth device’s state changes and the model emits dataChanged, all relevant display and status roles (name, connection, battery) are included so views update correctly.

Sequence diagram for updated bluetooth device state change notification

sequenceDiagram
    actor User
    participant BluetoothDevice
    participant BluetoothDeviceModel
    participant BluetoothView

    User->>BluetoothDevice: Change connection state
    BluetoothDevice-->>BluetoothDeviceModel: notifyStateChanged
    BluetoothDeviceModel->>BluetoothDeviceModel: updateData(device)
    alt device moved to top
        BluetoothDeviceModel->>BluetoothDeviceModel: moveToTop(deviceId)
        BluetoothDeviceModel->>BluetoothDeviceModel: createIndex(0, 0)
        BluetoothDeviceModel-->>BluetoothView: dataChanged(index, index, Name, ConnectStatus, ConnectStatusText, Battery, BatteryIconPath)
    else device reordered but not moved to top
        BluetoothDeviceModel->>BluetoothDeviceModel: createIndex(index, 0)
        BluetoothDeviceModel-->>BluetoothView: dataChanged(index, index, Name, ConnectStatus, ConnectStatusText, Battery, BatteryIconPath)
    end
    BluetoothView->>BluetoothView: Update name, connect status, battery, battery icon
Loading

Class diagram for BluetoothDeviceModel dataChanged roles update

classDiagram
    class BluetoothDevice {
        +QString id
        +QString name
        +bool isConnected
        +int batteryLevel
    }

    class BluetoothDeviceModel {
        +updateData(device)
        +moveToTop(deviceId)
        +reorderDevices()
        +createIndex(row, column)
        +dataChanged(indexStart, indexEnd, roles)
    }

    class BluetoothDeviceRoles {
        <<enumeration>>
        Name
        ConnectStatus
        ConnectStatusText
        Battery
        BatteryIconPath
    }

    BluetoothDeviceModel --> BluetoothDevice
    BluetoothDeviceModel --> BluetoothDeviceRoles
Loading

File-Level Changes

Change Details Files
Expand the roles emitted with dataChanged when a Bluetooth device entry is updated so all relevant UI fields refresh.
  • When a connected device is moved to the top of the list and its model index is recreated, emit dataChanged including Name, ConnectStatus, ConnectStatusText, Battery, and BatteryIconPath roles instead of only Name.
  • When an existing device remains in place but its state changes, emit dataChanged with the same expanded role set rather than just Name.
src/plugin-bluetooth/operation/bluetoothdevicemodel.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

这段代码是蓝牙设备模型中更新设备数据的函数。让我对修改后的代码进行分析:

  1. 代码逻辑改进:
    原代码在 dataChanged 信号中只发送了 Name 角色的更新,这会导致其他属性(如连接状态、电池信息等)更新时界面无法及时刷新。修改后的代码增加了更多角色的更新通知,这是正确的改进。

  2. 代码质量建议:

  • 建议将这些角色名称定义为常量,避免硬编码字符串。例如:
static const QVector<int> DeviceUpdateRoles = {
    Name,
    ConnectStatus,
    ConnectStatusText,
    Battery,
    BatteryIconPath
};
  • 可以考虑将重复的 dataChanged 信号发送抽取为一个函数,减少代码重复。
  1. 性能优化:
  • 目前的实现在设备状态变化时会触发多个角色的更新,这是合理的,因为这些属性通常需要同时更新显示。
  • 如果某些属性实际上没有变化,可以考虑只发送真正变化的角色,但这会增加代码复杂度,可能得不偿失。
  1. 安全性考虑:
  • 代码中使用了 device->id() 作为设备标识,需要确保这个 ID 的唯一性和稳定性。
  • 建议在调用 device 的任何方法之前检查 device 指针的有效性。
  1. 具体改进建议:
void BluetoothDeviceModel::updateData(BluetoothDevice *device)
{
    if (!device) {
        return;
    }

    const int index = findDevice(device->id());
    if (index < 0) {
        return;
    }

    const bool isConnected = device->isConnected();
    const static QVector<int> updateRoles = {
        Name,
        ConnectStatus,
        ConnectStatusText,
        Battery,
        BatteryIconPath
    };

    if (isConnected && index > 0) {
        moveToTop(device->id());
        const QModelIndex modelIndex = createIndex(0, 0);
        emit dataChanged(modelIndex, modelIndex, updateRoles);
    } else if (!isConnected) {
        reorderDevices();
    } else {
        const QModelIndex modelIndex = createIndex(index, 0);
        emit dataChanged(modelIndex, modelIndex, updateRoles);
    }
}

这些改进提高了代码的可维护性和健壮性,同时保持了原有的功能。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The list of roles {Name, ConnectStatus, ConnectStatusText, Battery, BatteryIconPath} is duplicated; consider extracting it into a small helper or constant to avoid divergence if roles change later.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The list of roles `{Name, ConnectStatus, ConnectStatusText, Battery, BatteryIconPath}` is duplicated; consider extracting it into a small helper or constant to avoid divergence if roles change later.

## Individual Comments

### Comment 1
<location> `src/plugin-bluetooth/operation/bluetoothdevicemodel.cpp:56` </location>
<code_context>
                 moveToTop(device->id());
                 QModelIndex modelIndex = createIndex(0, 0);
-                emit dataChanged(modelIndex, modelIndex, {Name});
+                emit dataChanged(modelIndex, modelIndex, {Name, ConnectStatus, ConnectStatusText, Battery, BatteryIconPath});
             } else if (!isConnected) {
                 reorderDevices();
</code_context>

<issue_to_address>
**suggestion:** Avoid duplicating the same role list literal in multiple dataChanged() calls.

The same role set is now used in multiple branches. To avoid duplication and keep them in sync, factor the roles into a shared constexpr/`static const` container (e.g. `kDeviceStatusRoles`) or a helper that returns the list, so future changes only need to be made in one place.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

moveToTop(device->id());
QModelIndex modelIndex = createIndex(0, 0);
emit dataChanged(modelIndex, modelIndex, {Name});
emit dataChanged(modelIndex, modelIndex, {Name, ConnectStatus, ConnectStatusText, Battery, BatteryIconPath});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Avoid duplicating the same role list literal in multiple dataChanged() calls.

The same role set is now used in multiple branches. To avoid duplication and keep them in sync, factor the roles into a shared constexpr/static const container (e.g. kDeviceStatusRoles) or a helper that returns the list, so future changes only need to be made in one place.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, caixr23, wyu71

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@wyu71

wyu71 commented Dec 25, 2025

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Dec 25, 2025

Copy link
Copy Markdown

This pr force merged! (status: blocked)

@deepin-bot deepin-bot Bot merged commit 7b1e8b6 into linuxdeepin:master Dec 25, 2025
16 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants