Skip to content

Commit

Permalink
Adapting to both android and iOS testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
archelangelo committed Dec 31, 2019
1 parent c8f43a9 commit 9b3e20e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 42 deletions.
78 changes: 39 additions & 39 deletions lib/core/networking.dart
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import 'dart:convert';
import 'dart:io';
// import 'dart:convert';
// import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'package:pal_finder/data/event.dart';
// import 'package:flutter/cupertino.dart';
// import 'package:http/http.dart' as http;
// import 'package:pal_finder/data/event.dart';

class EventFetcher {
EventFetcher({
@required this.lat,
@required this.lng,
@required this.dist,
})
: nextUrl='http://10.0.2.2:8000/apis/events/nearby/?lng=${lng.toStringAsFixed(6)}&lat=${lat.toStringAsFixed(6)}&dist=${dist.toStringAsFixed(3)}';
// class EventFetcher {
// EventFetcher({
// @required this.lat,
// @required this.lng,
// @required this.dist,
// })
// : nextUrl='http://10.0.2.2:8000/apis/events/nearby/?lng=${lng.toStringAsFixed(6)}&lat=${lat.toStringAsFixed(6)}&dist=${dist.toStringAsFixed(3)}';

final double lat;
final double lng;
final double dist;
String nextUrl;
// final double lat;
// final double lng;
// final double dist;
// String nextUrl;

Future<List<EventData>> fetchNextPage() async {
final eventList = <EventData>[];
if (nextUrl != null) {
final response = await http.get(
nextUrl,
headers: {
HttpHeaders.authorizationHeader: 'Token b0998f43d3cb06e03f9dce4dd4e7816bc12eaa74'
},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
final list = data['results'] as List;
list.forEach((eventMap) {
eventList.add(EventData.fromMap(eventMap));
});
nextUrl = data['next'];
} else {
throw Exception('Fail to load events.');
}
}
return eventList;
}
}
// Future<List<EventData>> fetchNextPage() async {
// final eventList = <EventData>[];
// if (nextUrl != null) {
// final response = await http.get(
// nextUrl,
// headers: {
// HttpHeaders.authorizationHeader: 'Token b0998f43d3cb06e03f9dce4dd4e7816bc12eaa74'
// },
// );
// if (response.statusCode == 200) {
// final data = jsonDecode(response.body) as Map<String, dynamic>;
// final list = data['results'] as List;
// list.forEach((eventMap) {
// eventList.add(EventData.fromMap(eventMap));
// });
// nextUrl = data['next'];
// } else {
// throw Exception('Fail to load events.');
// }
// }
// return eventList;
// }
// }
49 changes: 46 additions & 3 deletions lib/screens/event_list.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'package:pal_finder/widgets/event_card.dart';
import 'package:pal_finder/data/event.dart';
import 'package:pal_finder/core/networking.dart';
// import 'package:pal_finder/core/networking.dart';

class EventList extends StatefulWidget {
@override
Expand All @@ -18,7 +20,6 @@ class _EventListState extends State<EventList> {
dist: 0.5,
);


// @override
// void initState() {
// // TODO: implement initState
Expand Down Expand Up @@ -62,4 +63,46 @@ class _EventListState extends State<EventList> {
),
);
}
}
}

class EventFetcher {
EventFetcher({
@required this.lat,
@required this.lng,
@required this.dist,
}) {
if (Platform.isIOS) {
nextUrl='http://localhost:8000/apis/events/nearby/?lng=${lng.toStringAsFixed(6)}&lat=${lat.toStringAsFixed(6)}&dist=${dist.toStringAsFixed(3)}';
} else if (Platform.isAndroid) {
nextUrl='http://10.0.2.2:8000/apis/events/nearby/?lng=${lng.toStringAsFixed(6)}&lat=${lat.toStringAsFixed(6)}&dist=${dist.toStringAsFixed(3)}';
}
}

final double lat;
final double lng;
final double dist;
String nextUrl;

Future<List<EventData>> fetchNextPage() async {
final eventList = <EventData>[];
if (nextUrl != null) {
final response = await http.get(
nextUrl,
headers: {
HttpHeaders.authorizationHeader: 'Token fc27c2274f456cf0e7bd00663035a8c94ada0025'
},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
final list = data['results'] as List;
list.forEach((eventMap) {
eventList.add(EventData.fromMap(eventMap));
});
nextUrl = data['next'];
} else {
throw Exception('Fail to load events.');
}
}
return eventList;
}
}

0 comments on commit 9b3e20e

Please sign in to comment.