Skip to content

Commit

Permalink
add session stream; add log in/out button;
Browse files Browse the repository at this point in the history
  • Loading branch information
tastelessjolt committed Dec 8, 2018
1 parent e92b73f commit 6a14c32
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 40 deletions.
9 changes: 6 additions & 3 deletions lib/src/ia_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ class InstiAppBloc {

final client = InstiAppApi();

Session session;
Stream<Session> get session => _sessionSubject.stream;
final _sessionSubject = BehaviorSubject<Session>();

var loggedIn = false;

var _hostels = <Hostel>[];
Session currSession;


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

Future<Null> _updateHostels() async {
Expand Down
103 changes: 69 additions & 34 deletions lib/src/routes/homepage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {
int hostelIndex = 0;
String currHostel = '1';
InstiAppBloc _bloc;
Session session;

Expand All @@ -46,7 +46,7 @@ class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
_bloc = InstiAppBloc(session: session);
_bloc = InstiAppBloc(currSession: session);
}

@override
Expand All @@ -58,7 +58,10 @@ class _MyHomePageState extends State<MyHomePage> {
key: _scaffoldKey,
appBar: AppBar(
leading: IconButton(
icon: Icon(OMIcons.menu, color: Colors.white,),
icon: Icon(
OMIcons.menu,
color: Colors.white,
),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
},
Expand Down Expand Up @@ -93,12 +96,16 @@ class _MyHomePageState extends State<MyHomePage> {
stream: _bloc.hostels,
builder: (BuildContext context,
AsyncSnapshot<UnmodifiableListView<Hostel>> hostels) {
currHostel = _bloc.currSession.sessionid == null
? "1"
: _bloc.currSession.profile.hostel;
if (hostels.hasData) {
hostels.data[hostelIndex].mess.sort((h1, h2) => h1.compareTo(h2));
var currMess = hostels.data
.firstWhere((h) => h.shortName == currHostel)
.mess
..sort((h1, h2) => h1.compareTo(h2));
return ListView(
children: hostels.data[hostelIndex].mess
.map(_buildSingleDayMess)
.toList(),
children: currMess.map(_buildSingleDayMess).toList(),
// children: <Widget>[],
physics: BouncingScrollPhysics(),
);
Expand All @@ -115,24 +122,35 @@ class _MyHomePageState extends State<MyHomePage> {
);
}

Drawer buildDrawer(BuildContext context) {
var theme = Theme.of(context);
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
StreamBuilder<Session> buildDrawer(BuildContext context) {
return StreamBuilder<Session>(
stream: _bloc.session,
initialData: Session(),
builder: (BuildContext context, AsyncSnapshot<Session> snapshot) {
var theme = Theme.of(context);
var textTheme =
theme.textTheme.subhead.copyWith(fontWeight: FontWeight.bold);
var navList = <Widget>[
UserAccountsDrawerHeader(
currentAccountPicture: CircleAvatar(
child: Icon(OMIcons.person),
),
accountName: Text(
_bloc.session.sessionid != null ? _bloc.session.profile.name : 'Not Logged in',
style: theme.textTheme.body1.copyWith(color: Colors.white, fontWeight: FontWeight.bold),
),
accountEmail: Text(
_bloc.session.sessionid != null ? _bloc.session.profile.rollNo : "",
style: theme.textTheme.body1.copyWith(color: Colors.white),
snapshot.data.sessionid != null
? snapshot.data.profile.name
: 'Not Logged in',
style: theme.textTheme.body1
.copyWith(color: Colors.white, fontWeight: FontWeight.bold),
),
accountEmail: snapshot.data.sessionid != null
? Text(snapshot.data.profile.rollNo,
style: theme.textTheme.body1.copyWith(color: Colors.white))
: RaisedButton(
child: Text("Log in"),
onPressed: () {
Navigator.of(context).pushReplacementNamed('/');
},
),
decoration: BoxDecoration(
color: theme.accentColor,
),
Expand All @@ -141,92 +159,109 @@ class _MyHomePageState extends State<MyHomePage> {
leading: Icon(OMIcons.dashboard),
title: Text(
"Feed",
style: theme.textTheme.title,
style: textTheme,
),
onTap: () {},
),
ListTile(
leading: Icon(OMIcons.rssFeed),
title: Text(
"News",
style: theme.textTheme.title,
style: textTheme,
),
onTap: () {},
),
ListTile(
leading: Icon(OMIcons.search),
title: Text(
"Explore",
style: theme.textTheme.title,
style: textTheme,
),
onTap: () {},
),
ListTile(
selected: true,
leading: Icon(OMIcons.restaurant),
title: Text(
"Mess Menu",
style: theme.textTheme.title,
style: textTheme,
),
onTap: () {},
),
ListTile(
leading: Icon(OMIcons.workOutline),
title: Text(
"Placement Blog",
style: theme.textTheme.title,
style: textTheme,
),
onTap: () {},
),
ListTile(
leading: Icon(OMIcons.workOutline),
title: Text(
"Internship Blog",
style: theme.textTheme.title,
style: textTheme,
),
onTap: () {},
),
ListTile(
leading: Icon(OMIcons.dateRange),
title: Text(
"Calender",
style: theme.textTheme.title,
style: textTheme,
),
onTap: () {},
),
ListTile(
leading: Icon(OMIcons.map),
title: Text(
"Map",
style: theme.textTheme.title,
style: textTheme,
),
onTap: () {},
),
ListTile(
leading: Icon(OMIcons.feedback),
title: Text(
"Complaints/Suggestions",
style: theme.textTheme.title,
style: textTheme,
),
onTap: () {},
),
ListTile(
leading: Icon(OMIcons.link),
title: Text(
"Quick Links",
style: theme.textTheme.title,
style: textTheme,
),
onTap: () {},
),
ListTile(
leading: Icon(OMIcons.settings),
title: Text(
"Settings",
style: theme.textTheme.title,
style: textTheme,
),
onTap: () {},
),
],
),
];
if (snapshot.data.sessionid != null) {
navList.add(ListTile(
leading: Icon(OMIcons.exitToApp),
title: Text(
"Logout",
style: textTheme,
),
onTap: () {},
));
}
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: navList,
),
);
},
);
}

Expand All @@ -236,7 +271,7 @@ class _MyHomePageState extends State<MyHomePage> {
builder: (context, snapshot) {
if (snapshot.hasData) {
return DropdownButton<int>(
value: hostelIndex,
value: snapshot.data.indexWhere((h) => h.shortName == currHostel),
items: snapshot.data
.asMap()
.entries
Expand All @@ -247,7 +282,7 @@ class _MyHomePageState extends State<MyHomePage> {
.toList(),
onChanged: (h) {
setState(() {
hostelIndex = h;
currHostel = snapshot.data[h].shortName;
});
},
);
Expand Down
8 changes: 5 additions & 3 deletions lib/src/routes/loginpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,20 @@ class _LoginPageState extends State<LoginPage> {
}

Future<void> startLoginPageServer() async {
server = await HttpServer.bind(InternetAddress.loopbackIPv4, 9399);
var defAssets = DefaultAssetBundle.of(context);
server = await HttpServer.bind(InternetAddress.loopbackIPv4, 9399, shared: true);
server.listen((HttpRequest request) async {
print("URI: ${request.uri}");
if (request.uri.toString() == '/') {
var html = await DefaultAssetBundle.of(context)
var html = await defAssets
.loadString('assets/login.html');
request.response
..statusCode = 200
..headers.set("Content-Type", ContentType.html.mimeType)
..write(html);
} else if (request.uri.toString().contains('lotus')) {
var binary =
await DefaultAssetBundle.of(context).load('assets/lotus.png');
await defAssets.load('assets/lotus.png');
request.response
..statusCode = 200
..headers.set("Content-Type", "image/png")
Expand All @@ -99,6 +100,7 @@ class _LoginPageState extends State<LoginPage> {
request.response..statusCode = 404;
}
await request.response.close();
print("Served");
// await server.close(force: true);
});
}
Expand Down

0 comments on commit 6a14c32

Please sign in to comment.