Skip to content

Commit 1e4d1fd

Browse files
authored
Merge pull request #2 from Radlet/card-list
minor array check and cards added
2 parents 2105b47 + 8e55b1b commit 1e4d1fd

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

lib/screens/Discovery/Discovery.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import './widgets/DeviceListItem.dart';
35
import '../../util/apis/discovery.dart';
@@ -6,11 +8,9 @@ import '../../util/data/Device.dart';
68
class Discovery extends StatefulWidget {
79
const Discovery({
810
Key key,
9-
this.placeholder = "No device found",
1011
this.child,
1112
}) : super(key: key);
1213

13-
final String placeholder;
1414
final Widget child;
1515

1616
@override
@@ -19,19 +19,29 @@ class Discovery extends StatefulWidget {
1919

2020
class DiscoveryState extends State<Discovery> {
2121
List<Device> devices;
22+
var timer;
2223

2324
@override
2425
void initState() {
2526
devices = new List<Device>();
2627
super.initState();
2728

28-
getDiscoveredList().then((fetchedDevices) {
29-
updateDeviceList(fetchedDevices);
30-
}).catchError((onError) {
31-
print("Error fetching device list");
29+
timer = Timer.periodic(const Duration(seconds: 3), (Timer timer) {
30+
getDiscoveredList().then((fetchedDevices) {
31+
updateDeviceList(fetchedDevices);
32+
}).catchError((onError) {
33+
print("Error fetching device list");
34+
});
35+
print("Fetch called");
3236
});
3337
}
3438

39+
@override
40+
void dispose() {
41+
super.dispose();
42+
timer.cancel();
43+
}
44+
3545
void updateDeviceList(List<Device> newDeviceList) {
3646
// TODO: Add a compasion mechanism
3747
setState(() {
@@ -55,7 +65,9 @@ class DiscoveryState extends State<Discovery> {
5565
child: new ListView.builder(
5666
itemCount: devices.length,
5767
itemBuilder: (BuildContext context, int index) {
58-
return new DeviceListItem(deviceID: devices[index].id);
68+
return new DeviceListItem(
69+
deviceID: devices[index].id,
70+
deviceName: devices[index].name);
5971
})),
6072
);
6173
}
Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
import 'package:flutter/material.dart';
22

33
class DeviceListItem extends StatelessWidget {
4-
DeviceListItem({this.deviceID});
4+
DeviceListItem({this.deviceID, this.deviceName});
55

66
final String deviceID;
7+
final String deviceName;
78

89
@override
910
Widget build(BuildContext context) {
10-
return Container(
11-
height: 50,
12-
color: Colors.amber[100],
13-
child: Center(child: new Text(deviceID)),
11+
return Center(
12+
child: Card(
13+
child: InkWell(
14+
onTap: () {
15+
print("Your card has been tapped");
16+
},
17+
child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
18+
ListTile(
19+
leading: Icon(Icons.album),
20+
title: Text(this.deviceName),
21+
subtitle: Text("Sample device description"))
22+
]),
23+
),
24+
),
1425
);
1526
}
1627
}

lib/util/apis/discovery.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@ import '../data/Device.dart';
66

77
Future<List<Device>> getDiscoveredList([Map<String, String> data = const {}]) {
88
return helper.makeGetRequest(urls.DISCOVERY_ENDPOINT, data).then((response) {
9-
if( response.statusCode != 200 ){
9+
if (response.statusCode != 200) {
1010
// Server responded with error code
1111
throw Exception();
1212
}
13-
13+
1414
var parsedJson = jsonDecode(response.body);
1515

1616
List<Device> devices = new List<Device>();
17-
for (var item in parsedJson["deviceArray"]) {
18-
devices.add(new Device(item["id"], item["name"]));
17+
if (parsedJson["deviceArray"] is List) {
18+
for (var item in parsedJson["deviceArray"]) {
19+
devices.add(new Device(item["id"], item["name"]));
20+
}
1921
}
22+
2023
return devices;
21-
}).catchError((onError){
22-
// Connection failed
24+
}).catchError((onError) {
2325
throw Exception();
2426
});
2527
}

lib/util/apis/helper.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Future makePostRequest(String endpoint, Map<String, String> data) {
77

88
Future makeGetRequest(String endpoint, Map<String, String> data) {
99
return http.get(new Uri.http(SERVER_URL,endpoint));
10+
// TODO: catch connection failed exception
1011
}

0 commit comments

Comments
 (0)