Skip to content

Commit

Permalink
fix: moved detail screen to device widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Suraj Adhikari committed Dec 6, 2021
1 parent 91a9a0b commit 522bdef
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/providers/trait_based_device_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ class TraitBasedDeviceNotifier extends ChangeNotifier {
}

TraitBasedDeviceNotifier(Request request, String deviceId,
{GetDetailsFunction getDetails = DevicesRepository.getLockDetails}) {
{GetDetailsFunction getDetails = DevicesRepository.getDeviceDetails}) {
_request = request;
_deviceId = deviceId;
fetchData(getDetails: getDetails);
}

Future<Device?> fetchData(
{GetDetailsFunction getDetails =
DevicesRepository.getLockDetails}) async {
DevicesRepository.getDeviceDetails}) async {
loadingDetail = true;
notifyListeners();

_deviceDetail = await getDetails(_request, _deviceId);

print(_deviceDetail!.traits.length);
loadingDetail = false;
notifyListeners();

Expand Down
129 changes: 129 additions & 0 deletions lib/traits/detail_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:yonomi_device_widgets/providers/trait_based_device_notifier.dart';
import 'package:yonomi_platform_sdk/yonomi-sdk.dart';
import 'package:yonomi_platform_sdk/yonomi-sdk.dart' as yoSdk;

class DetailScreen extends StatelessWidget {
final Request request;
final String deviceId;

const DetailScreen({Key? key, required this.request, required this.deviceId})
: super(key: key);

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => TraitBasedDeviceNotifier(request, deviceId),
child: Consumer<TraitBasedDeviceNotifier>(
builder: (_, traitBasedDeviceNotifier, child) {
if (traitBasedDeviceNotifier.deviceDetail == null) {
return CircularProgressIndicator();
} else {
return Column(
children:
traitBasedDeviceNotifier.deviceDetail!.traits.map((element) {
return Row(
children: [Text(element.name)],
);
}).toList());
}
}),
);
}
}

// class isPowered extends yoSdk.State<bool> {
// isPowered(bool isOn) : super("Power", isOn);
// }

// class PowerTrait extends Trait {
// PowerTrait(String name, yoSdk.State state) : super(name, state);
// }

// class TraitWidgetsBuilder {
// static Widget build(Device deviceDetail) {
// List<Trait> dummyTraits = [
// LockTrait("whenLocked", IsLocked(true)),
// LockTrait("whenUnlocked", IsLocked(false)),
// PowerTrait("PowerTrait", isPowered(true)),
// UnknownTrait(),
// ];

// // TODO: L41: use deviceDetail.traits, not dummyTraits;
// return Column(
// children: [
// buildDeviceTitleRow(deviceDetail.displayName),
// ...buildTraitRows(dummyTraits),
// ],
// );
// }

// static Widget buildDeviceTitleRow(String deviceName) {
// return Row(
// children: [Text(deviceName)],
// );
// }

// static List<Row> buildTraitRows(List<Trait> traits) {
// List<Row> traitRows = [];
// for (var trait in traits) {
// traitRows.add(
// Row(
// children: [traitFactory(trait)],
// ),
// );
// }
// return traitRows;
// }

// static Widget traitFactory(Trait trait) {
// switch (trait.runtimeType) {
// case LockTrait:
// return LockTraitWidget(trait);
// case PowerTrait:
// return PowerTraitWidget(trait);
// default:
// return UnknownTraitWidget(trait);
// }
// }
// }

// class LockTraitWidget extends StatelessWidget {
// Trait trait;

// LockTraitWidget(this.trait);

// @override
// Widget build(BuildContext context) {
// return Text("Lock-based: ${this.trait.name}");
// }
// }

// class PowerTraitWidget extends StatelessWidget {
// Trait trait;

// PowerTraitWidget(this.trait);

// @override
// Widget build(BuildContext context) {
// return Text("Power Trait: ${this.trait.name}");
// // return Switch(
// // value: false,
// // onChanged: (bool newValue) {
// // print("Switched ${newValue}");
// // },
// // );
// }
// }

// class UnknownTraitWidget extends StatelessWidget {
// Trait trait;

// UnknownTraitWidget(this.trait);

// @override
// Widget build(BuildContext context) {
// return Text("Unknown Trait: ${this.trait.name}");
// }
// }
77 changes: 77 additions & 0 deletions lib/traits/lock.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:yonomi_device_widgets/assets/traits/device_item_icon.dart';
import 'package:yonomi_device_widgets/components/arc.dart';
import 'package:yonomi_device_widgets/providers/lock_provider.dart';
import 'package:yonomi_device_widgets/ui/widget_style_constants.dart';

class LockWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final lockProvider = Provider.of<LockProvider>(context, listen: true);

return lockProvider.loadingDetail
? Center(child: CircularProgressIndicator())
: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
children: <Widget>[
Text(
lockProvider.deviceDetail.displayName,
style: Theme.of(context).textTheme.headline6,
),
],
),
SizedBox(
height: 80,
),
Center(
child: Arc(
showThumb: false,
centerWidget: SizedBox(
width: 175,
height: 175,
child: GestureDetector(
child: getLockStateIcon(lockProvider),
onTap: () => _lockTap(lockProvider),
)),
color: lockProvider.isLocked
? WidgetStyleConstants.deviceDetailIconColorActive
: WidgetStyleConstants.deviceDetailIconColorInactive,
initialValue: 0.0,
maxValue: 1.0,
onFinalSetPoint: (double value) {},
),
),
SizedBox(
height: 80,
),
CupertinoSwitch(
onChanged: (bool value) {
_lockTap(lockProvider);
},
value: lockProvider.isLocked,
),
],
),
);
}

Widget getLockStateIcon(LockProvider lockProvider) {
return (lockProvider.loadingDetail || lockProvider.loadingAction)
? Center(child: CircularProgressIndicator())
: (lockProvider.isLocked)
? DeviceItemIcon.buildLockIcon(
175, WidgetStyleConstants.deviceDetailIconColorActive)
: DeviceItemIcon.buildUnlockIcon(
175, WidgetStyleConstants.deviceDetailIconColorInactive);
}

void _lockTap(LockProvider provider) {
bool setLock = !provider.isLocked;
provider.setLockUnlockAction(provider.deviceDetail.id, setLock);
}
}

0 comments on commit 522bdef

Please sign in to comment.