babincc / flutter_workshop

This repo houses add-ons, plug-ins, and helpful code to make Flutter programming easier.
5 stars 1 forks source link

Link object in network #6

Closed babincc closed 1 year ago

babincc commented 1 year ago

Right now, the HueNetwork object just contains a bunch of objects. They aren't really connected in any simple way. Putting them together is clunky and leaves room for error. There needs to be a simple way to connect objects.

Example:

Rather than doing all of this to get a GroupedLight from a Room:

// Figure out which group is the one you want
Relative myRelative = myRoom
    .services
    .firstWhere((service) => 
        service.type == ResourceType.groupedLight);

// Get the group who's ID was just found
GroupedLight myGroupedLight = myHueNetwork.groupedLights.firstWhere(
  (groupedLight) => groupedLight.id == myRelative.id,
);

It should look something more like this:

GroupedLight myGroupedLight = myRoom
        .groupedLights
        .first;
babincc commented 1 year ago

Added in update 1.2.0

Now, instead of

// Figure out which group is the one you want
// Get network from example 3
Relative myRelative = myHueNetwork
    .rooms
    .first
    .services
    .firstWhere((service) => 
        service.type == ResourceType.groupedLight);

// Get the group whose ID was just found
GroupedLight myGroupedLight = myHueNetwork.groupedLights.firstWhere(
    (groupedLight) => groupedLight.id == myRelative.id,
);

// Toggle the on/off state
myGroupedLight.on.isOn = !myGroupedLight.on.isOn;

// PUT the new state
myBridge.put(myGroupedLight);

you can instead do

// Get the grouped light from the room
GroupedLight myGroupedLight = myHueNetwork!.rooms.first.servicesAsResources
        .firstWhere((resource) => resource is GroupedLight) as GroupedLight;

// Toggle the on/off state
myGroupedLight.on.isOn = !myGroupedLight.on.isOn;

// PUT the new state
myBridge!.put(myGroupedLight);