Skip to content

Commit

Permalink
add bloc class; using RxDart streams
Browse files Browse the repository at this point in the history
  • Loading branch information
tastelessjolt committed Nov 28, 2018
1 parent 5dd5601 commit 9204d42
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 77 deletions.
27 changes: 27 additions & 0 deletions lib/src/ia_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:instiapp/src/api/apiclient.dart';
import 'package:instiapp/src/api/model/mess.dart';
import 'dart:collection';
import 'package:rxdart/rxdart.dart';
import 'package:http/io_client.dart';
import 'package:jaguar_retrofit/jaguar_retrofit.dart';

class InstiAppBloc {
Stream<UnmodifiableListView<Hostel>> get hostels => _hostelsSubject.stream;
final _hostelsSubject = BehaviorSubject<UnmodifiableListView<Hostel>>();

final client = InstiAppApi();

var _hostels = <Hostel>[];

InstiAppBloc() {
globalClient = IOClient();
_updateHostels().then((_) {
_hostelsSubject.add(UnmodifiableListView(_hostels));
});
}

Future<Null> _updateHostels() async {
final hostels = await client.getSortedHostelMess();
_hostels = hostels;
}
}
148 changes: 95 additions & 53 deletions lib/src/routes/homepage.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/io_client.dart';
import 'package:instiapp/src/api/apiclient.dart';
import 'package:jaguar_retrofit/jaguar_retrofit.dart';
import 'package:instiapp/src/ia_bloc.dart';

import 'package:url_launcher/url_launcher.dart';

import 'package:instiapp/src/api/model/mess.dart';
import 'package:instiapp/src/json_parsing.dart';

import 'dart:collection';

class BlocProvider extends InheritedWidget {
final InstiAppBloc bloc;

BlocProvider(this.bloc, {child}) : super(child: child);

@override
bool updateShouldNotify(InheritedWidget oldWidget) {
return false;
}

static BlocProvider of(BuildContext context) {
return context.inheritFromWidgetOfExactType(BlocProvider);
}
}

class MyHomePage extends StatefulWidget {
final String title = "InstiApp";
Expand All @@ -18,66 +32,94 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {
final client = InstiAppApi();

int hostelIndex;

int hostelIndex = 3;
InstiAppBloc _bloc;
@override
void initState() {
super.initState();
globalClient = IOClient();
hostelIndex = 3;
_bloc = InstiAppBloc();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: ImageIcon(AssetImage('assets/lotus.png'), color: Colors.white,),
),
title: Text("Mess",
style: Theme.of(context).textTheme.headline.copyWith(
fontFamily: "Bitter", color: Colors.white)),
bottom: PreferredSize(
preferredSize: const Size.fromHeight(48.0),
child: Container(
color: Colors.white,
height: 48.0,
padding: EdgeInsets.only(left: 16.0),
alignment: Alignment.centerLeft,
child: DropdownButton(
hint: Text("Hostel"),
items: [
DropdownMenuItem(
child: Text("H1"),
),
],
onChanged: (a) {},
return BlocProvider(
_bloc,
child: Scaffold(
appBar: AppBar(
leading: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: ImageIcon(
AssetImage('assets/lotus.png'),
color: Colors.white,
),
),
title: Text("Mess",
style: Theme.of(context)
.textTheme
.headline
.copyWith(fontFamily: "Bitter", color: Colors.white)),
bottom: PreferredSize(
preferredSize: const Size.fromHeight(48.0),
child: Container(
color: Colors.white,
height: 48.0,
padding: EdgeInsets.only(left: 16.0),
alignment: Alignment.centerLeft,
child: buildDropdownButton(),
),
),
),
body: StreamBuilder<UnmodifiableListView<Hostel>>(
stream: _bloc.hostels,
builder: (BuildContext context,
AsyncSnapshot<UnmodifiableListView<Hostel>> hostels) {
if (hostels.hasData) {
hostels.data[hostelIndex].mess.sort((h1, h2) => h1.compareTo(h2));
return ListView(
children: hostels.data[hostelIndex].mess
.map(_buildSingleDayMess)
.toList(),
// children: <Widget>[],
physics: BouncingScrollPhysics(),
);
} else {
return Center(
child: CircularProgressIndicator(
backgroundColor: Theme.of(context).accentColor,
),
);
}
},
),
),
body: FutureBuilder<List<Hostel>>(
builder: (BuildContext context, AsyncSnapshot<List<Hostel>> hostels) {
if (hostels.hasData) {
return ListView(
children: (hostels.data[hostelIndex].mess
..sort((h1, h2) => h1.compareTo(h2)))
.map(_buildSingleDayMess).toList(),
physics: BouncingScrollPhysics(),
);
} else {
return Center(
child: CircularProgressIndicator(
backgroundColor: Theme.of(context).accentColor,
),
);
}
},
future: client.getSortedHostelMess(),
),
);
}

Widget buildDropdownButton() {
return StreamBuilder<UnmodifiableListView<Hostel>>(
stream: _bloc.hostels,
builder: (context, snapshot) {
if (snapshot.hasData) {
return DropdownButton<int>(
value: hostelIndex,
items: snapshot.data
.asMap()
.entries
.map((entry) => DropdownMenuItem<int>(
child: Text(entry.value.name),
value: entry.key,
))
.toList(),
onChanged: (h) {
setState(() {
hostelIndex = h;
});
},
);
} else {
return Center(child: CircularProgressIndicator());
}
},
);
}

Expand Down
44 changes: 21 additions & 23 deletions lib/src/routes/loginpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:instiapp/src/api/apiclient.dart';

import 'package:instiapp/src/api/model/user.dart';


const String api = "https://api.insti.app/api";
const String authority = "api.insti.app";

Expand Down Expand Up @@ -55,27 +52,28 @@ class _LoginPageState extends State<LoginPage> {
@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Image(
color: Theme.of(context).accentColor,
image: AssetImage('assets/lotus.png'),
width: 250.0,
fit: BoxFit.scaleDown,
),
CircularProgressIndicator(
backgroundColor: Theme.of(context).accentColor,
),
Text(
"InstiApp",
style: Theme.of(context).textTheme.display1.copyWith(
fontFamily: "Bitter", color: Theme.of(context).accentColor),
),
],
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Image(
color: Theme.of(context).accentColor,
image: AssetImage('assets/lotus.png'),
width: 250.0,
fit: BoxFit.scaleDown,
),
CircularProgressIndicator(
backgroundColor: Theme.of(context).accentColor,
),
Text(
"InstiApp",
style: Theme.of(context).textTheme.display1.copyWith(
fontFamily: "Bitter", color: Theme.of(context).accentColor),
),
],
),
),
));
);
}

Future<void> startLoginPageServer() async {
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies:
http: ^0.12.0
jaguar_retrofit: ^2.5.12
jaguar_serializer: ^2.2.4
rxdart: ^0.19.0

dev_dependencies:
flutter_test:
Expand Down
2 changes: 1 addition & 1 deletion test/mess_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main() {
final hostels = parseMess(messJsonString);
expect(hostels[0].name, "Hostel 1");
expect(hostels[0].mess[0].day, 7);
});
}, timeout: Timeout.parse("10s"));
}

const messJsonString = r"""
Expand Down

0 comments on commit 9204d42

Please sign in to comment.