[settings][bt]Improve Bluetooth settings sorting behavior

Newly discovered Bluetooth devices are added to the bottom of the list
Existing devices that change signal strength are updated in-place
Devices are sorted by signal strength every 5 seconds

SY-879 #done
Test: ran bluetooth settings and verified behavior was as described
Change-Id: I68776a4b7aceac120f3954f03e7d9ad5e2c3ca91
diff --git a/bin/bluetooth_settings/lib/src/bluetooth_model.dart b/bin/bluetooth_settings/lib/src/bluetooth_model.dart
index 894ed06..25b1ab3 100644
--- a/bin/bluetooth_settings/lib/src/bluetooth_model.dart
+++ b/bin/bluetooth_settings/lib/src/bluetooth_model.dart
@@ -2,11 +2,14 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
+import 'dart:async';
 import 'package:fidl_fuchsia_bluetooth/fidl.dart';
 import 'package:fidl_fuchsia_bluetooth_control/fidl.dart';
 import 'package:lib.app.dart/app.dart';
 import 'package:lib.widgets/model.dart';
 
+const Duration _deviceListRefreshInterval = Duration(seconds: 5);
+
 /// Model containing state needed for the bluetooth settings app.
 class BluetoothSettingsModel extends Model implements PairingDelegate {
   /// Bluetooth controller proxy.
@@ -15,6 +18,7 @@
   List<AdapterInfo> _adapters;
   AdapterInfo _activeAdapter;
   final List<RemoteDevice> _remoteDevices = [];
+  Timer _sortListTimer;
   bool _discoverable = true;
 
   PairingStatus pairingStatus;
@@ -62,6 +66,13 @@
     connectToService(startupContext.environmentServices, _control.ctrl);
     _refresh();
 
+    // Sort the list by signal strength every few seconds.
+    _sortListTimer = Timer.periodic(_deviceListRefreshInterval, (_) {
+      _remoteDevices
+          .sort((a, b) => (b.rssi?.value ?? 0).compareTo(a.rssi?.value ?? 0));
+      _refresh();
+    });
+
     // Just for first draft purposes, refresh whenever there are any changes.
     // TODO: handle errors, refresh more gracefully
     _control
@@ -75,10 +86,15 @@
         _refresh();
       }
       ..onDeviceUpdated = (device) {
-        _removeDeviceFromList(device.identifier);
-        _remoteDevices
-          ..add(device)
-          ..sort((a, b) => (b.rssi?.value ?? 0).compareTo(a.rssi?.value ?? 0));
+        int index =
+            _remoteDevices.indexWhere((d) => d.identifier == device.identifier);
+        if (index != -1) {
+          // Existing device, just update in-place.
+          _remoteDevices[index] = device;
+        } else {
+          // New device, add to bottom of list.
+          _remoteDevices.add(device);
+        }
         notifyListeners();
       }
       ..onDeviceRemoved = (deviceId) {
@@ -113,6 +129,7 @@
   /// scanning.
   void dispose() {
     _control.ctrl.close();
+    _sortListTimer.cancel();
   }
 
   @override