Skip to content

Commit

Permalink
feat: expose Loading state when either retrieving detail or performin…
Browse files Browse the repository at this point in the history
…g action in Provider
  • Loading branch information
ses110 committed May 20, 2021
1 parent b67000c commit cd33d76
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
18 changes: 12 additions & 6 deletions lib/components/lock_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class LockWidget extends StatelessWidget {
Widget build(BuildContext context) {
final lockProvider = Provider.of<LockProvider>(context, listen: true);

return lockProvider.loading
return lockProvider.loadingDetail
? Center(child: CircularProgressIndicator())
: Column(mainAxisAlignment: MainAxisAlignment.start, children: [
Row(
Expand All @@ -27,11 +27,7 @@ class LockWidget extends StatelessWidget {
Center(
child: Arc(
centerWidget: InkWell(
child: (lockProvider.isLocked)
? DeviceItemIcon.getLockIcon(
175, WidgetStyleConstants.deviceDetailIconColorActive)
: DeviceItemIcon.getUnlockIcon(175,
WidgetStyleConstants.deviceDetailIconColorInactive),
child: getLockStateIcon(lockProvider),
onTap: () {
bool setLock = !lockProvider.isLocked;
lockProvider.setLockUnlockAction(
Expand All @@ -45,4 +41,14 @@ class LockWidget extends StatelessWidget {
),
]);
}

Widget getLockStateIcon(LockProvider lockProvider) {
return (lockProvider.loadingDetail || lockProvider.loadingAction)
? Center(child: CircularProgressIndicator())
: (lockProvider.isLocked)
? DeviceItemIcon.getLockIcon(
175, WidgetStyleConstants.deviceDetailIconColorActive)
: DeviceItemIcon.getUnlockIcon(
175, WidgetStyleConstants.deviceDetailIconColorInactive);
}
}
28 changes: 25 additions & 3 deletions lib/providers/lock_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'package:yonomi_platform_sdk/repository/devices/lock_repository.dart';
import 'package:yonomi_platform_sdk/request/request.dart';

class LockProvider extends ChangeNotifier {
bool loading = false;
bool loadingDetail = false;
bool loadingAction = false;

LockProvider(Request request, String deviceId) {
_request = request;
Expand All @@ -19,14 +20,35 @@ class LockProvider extends ChangeNotifier {
bool get isLocked => _deviceDetail?.traits?.first?.state?.value ?? false;

Future<void> getDeviceDetail(String deviceId) async {
loading = true;
loadingDetail = true;

notifyListeners();

_deviceDetail = await DevicesRepository.getLockDetails(_request, deviceId);

loading = false;
loadingDetail = false;

notifyListeners();
}

Future<void> setLockUnlockAction(String deviceId, bool setLock) async {
loadingAction = true;

notifyListeners();

await LockRepository.sendLockUnlockAction(_request, deviceId, setLock);

var maxRetries = 0;
while (_deviceDetail?.traits?.first?.state?.value != setLock &&
maxRetries < 10) {
// Wait more time
_deviceDetail =
await DevicesRepository.getLockDetails(_request, deviceId);
await Future.delayed(Duration(milliseconds: 750));
maxRetries++;
}
loadingAction = false;

notifyListeners();
}
}

0 comments on commit cd33d76

Please sign in to comment.