From b681dd5dcfd4ea6df4ab5bdb1bef64a307cc977b Mon Sep 17 00:00:00 2001 From: Harshith Goka Date: Wed, 19 Dec 2018 17:11:57 +0530 Subject: [PATCH] [App] add EventPage; [Event] move subTitle calc to Event; [EventPage] implemented; --- lib/main.dart | 36 +++++++-- lib/src/api/model/event.dart | 41 ++++++++++ lib/src/blocs/ia_bloc.dart | 26 +++++++ lib/src/routes/eventpage.dart | 138 ++++++++++++++++++++++++++++++++++ lib/src/routes/feedpage.dart | 70 +++++------------ pubspec.yaml | 1 + 6 files changed, 254 insertions(+), 58 deletions(-) create mode 100644 lib/src/routes/eventpage.dart diff --git a/lib/main.dart b/lib/main.dart index 55623fed..80a192f0 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:InstiApp/src/routes/eventpage.dart'; import 'package:InstiApp/src/routes/feedpage.dart'; import 'package:InstiApp/src/routes/trainingblogpage.dart'; import 'package:flutter/cupertino.dart'; @@ -39,14 +40,37 @@ class MyAppState extends State { fontFamily: "SourceSansPro", primarySwatch: Colors.deepPurple, ), - routes: { - "/": (_) => LoginPage(), - "/mess": (_) => MessPage(), - "/placeblog": (_) => PlacementBlogPage(), - "/trainblog": (_) => TrainingBlogPage(), - "/feed": (_) => FeedPage(), + // routes: { + // "/": (_) => LoginPage(), + // "/mess": (_) => MessPage(), + // "/placeblog": (_) => PlacementBlogPage(), + // "/trainblog": (_) => TrainingBlogPage(), + // "/feed": (_) => FeedPage(), + // "/event": (_) => EventPage(), + // }, + onGenerateRoute: (RouteSettings settings) { + if (settings.name.startsWith("/event/") ) { + return _buildRoute(settings, EventPage(_bloc.getEvent(settings.name.split("/event/")[1]))); + } + else { + switch (settings.name) { + case "/": return _buildRoute(settings, LoginPage()); + case "/mess": return _buildRoute(settings, MessPage()); + case "/placeblog": return _buildRoute(settings, PlacementBlogPage()); + case "/trainblog": return _buildRoute(settings, TrainingBlogPage()); + case "/feed": return _buildRoute(settings, FeedPage()); + } + } + return _buildRoute(settings, MessPage()); }, ), ); } + + MaterialPageRoute _buildRoute(RouteSettings settings, Widget builder){ + return new MaterialPageRoute( + settings: settings, + builder: (BuildContext context) => builder, + ); + } } diff --git a/lib/src/api/model/event.dart b/lib/src/api/model/event.dart index 59545548..b76b3dbe 100644 --- a/lib/src/api/model/event.dart +++ b/lib/src/api/model/event.dart @@ -2,6 +2,7 @@ import 'package:InstiApp/src/api/model/body.dart'; import 'package:InstiApp/src/api/model/user.dart'; import 'package:InstiApp/src/api/model/venue.dart'; import 'package:jaguar_serializer/jaguar_serializer.dart'; +import 'package:date_format/date_format.dart'; part 'event.jser.dart'; @@ -55,6 +56,46 @@ class Event { int eventUserUes; bool eventBigImage = false; + + String getSubTitle() { + String subtitle = ""; + + DateTime startTime = DateTime.parse(eventStartTime); + DateTime endTime = DateTime.parse(eventEndTime); + DateTime timeNow = DateTime.now(); + bool eventStarted = timeNow.compareTo(startTime) > 0; + bool eventEnded = timeNow.compareTo(endTime) > 0; + + if (eventEnded) + subtitle += "Ended | "; + else if (eventStarted) { + int difference = + endTime.millisecondsSinceEpoch - timeNow.millisecondsSinceEpoch; + int minutes = difference ~/ (60 * 1000) % 60; + int hours = difference ~/ (60 * 60 * 1000) % 24; + int days = difference ~/ (24 * 60 * 60 * 1000); + String timeDiff = ""; + if (days > 0) timeDiff += "${days}D "; + if (hours > 0) timeDiff += "${hours}H "; + + timeDiff += "${minutes}M"; + + subtitle += "Ends in $timeDiff | "; + } + + if (startTime != null) { + subtitle += formatDate(startTime.toLocal(), [dd, " ", M, " | ", HH, ":", nn]); + } + String eventVenueName = ""; + for (var venue in eventVenues) { + eventVenueName += ", ${venue.venueShortName}"; + } + if (eventVenueName != "") { + subtitle += " | ${eventVenueName.substring(2)}"; + } + + return subtitle; + } } @GenSerializer() diff --git a/lib/src/blocs/ia_bloc.dart b/lib/src/blocs/ia_bloc.dart index ff185d34..518c3480 100644 --- a/lib/src/blocs/ia_bloc.dart +++ b/lib/src/blocs/ia_bloc.dart @@ -66,12 +66,38 @@ class InstiAppBloc { _eventsSubject.add(UnmodifiableListView(_events)); } + Event getEvent(String uuid) { + return _events?.firstWhere((event) => event.eventID == uuid); + } + void updateSession(Session sess) { currSession = sess; _sessionSubject.add(sess); _persistSession(sess); } + Future updateUesEvent(Event e, int ues) async { + try { + print("updating Ues from ${e.eventUserUes} to $ues"); + await client.updateUserEventStatus(getSessionIdHeader(), e.eventID, ues); + if (e.eventUserUes == 2) { + e.eventGoingCount--; + } + if (e.eventUserUes == 1) { + e.eventInterestedCount--; + } + if (ues == 1) { + e.eventInterestedCount++; + } else if (ues == 2) { + e.eventGoingCount++; + } + e.eventUserUes = ues; + print("updated Ues from ${e.eventUserUes} to $ues"); + } catch (ex) { + print(ex); + } + } + void _persistSession(Session sess) async { SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setString("session", standardSerializers.encode(sess)); diff --git a/lib/src/routes/eventpage.dart b/lib/src/routes/eventpage.dart new file mode 100644 index 00000000..02358476 --- /dev/null +++ b/lib/src/routes/eventpage.dart @@ -0,0 +1,138 @@ +import 'package:InstiApp/src/api/model/event.dart'; +import 'package:InstiApp/src/bloc_provider.dart'; +import 'package:InstiApp/src/blocs/ia_bloc.dart'; +import 'package:InstiApp/src/drawer.dart'; +import 'package:flutter/material.dart'; +import 'package:outline_material_icons/outline_material_icons.dart'; + +class EventPage extends StatefulWidget { + final Event event; + + EventPage(this.event); + + @override + _EventPageState createState() => _EventPageState(); +} + +class _EventPageState extends State { + GlobalKey _scaffoldKey = GlobalKey(); + int loadingUes = 0; + + @override + Widget build(BuildContext context) { + var theme = Theme.of(context); + var bloc = BlocProvider.of(context).bloc; + + return Scaffold( + key: _scaffoldKey, + drawer: DrawerOnly(), + body: CustomScrollView( + slivers: [ + SliverAppBar( + leading: Container( + decoration: ShapeDecoration( + shape: BeveledRectangleBorder( + borderRadius: + BorderRadius.only(bottomRight: Radius.circular(15))), + color: theme.accentColor), + child: IconButton( + icon: Icon( + OMIcons.menu, + color: Colors.white, + ), + onPressed: () { + _scaffoldKey.currentState.openDrawer(); + }, + ), + ), + expandedHeight: 300, + floating: false, + pinned: true, + flexibleSpace: FlexibleSpaceBar( + centerTitle: true, + title: Container( + width: MediaQuery.of(context).size.width * 0.65, + padding: + const EdgeInsets.symmetric(vertical: 8, horizontal: 8), + color: Color.fromRGBO(0, 0, 0, 0.2), + child: Text( + widget.event?.eventName, + )), + background: Image.network( + widget.event?.eventImageURL ?? + widget.event?.eventBodies[0].imageUrl, + fit: BoxFit.fitWidth), + ), + ), + SliverToBoxAdapter( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + ButtonBar( + mainAxisSize: MainAxisSize.max, + alignment: MainAxisAlignment.center, + children: [ + buildUserStatusButton("Going", 2, theme, bloc), + buildUserStatusButton("Interested", 1, theme, bloc), + ], + ), + SizedBox(height: 16.0,), + + ], + ), + ) + ], + ), + ); + } + + RaisedButton buildUserStatusButton(String name, int id, ThemeData theme, InstiAppBloc bloc) { + return RaisedButton( + color: widget.event?.eventUserUes == id ? theme.accentColor : Colors.white, + textColor: widget.event?.eventUserUes == id ? Colors.white : null, + shape: RoundedRectangleBorder( + side: BorderSide( + color: theme.accentColor, + ), + borderRadius: BorderRadius.all(Radius.circular(4))), + child: Row(children: () { + var rowChildren = [ + Text(name), + SizedBox( + width: 8.0, + ), + Text("${id == 1 ? widget.event?.eventInterestedCount : widget.event?.eventGoingCount}"), + ]; + if (loadingUes == id) { + rowChildren.insertAll(0, [ + SizedBox( + height: 18, + width: 18, + child: CircularProgressIndicator( + valueColor: new AlwaysStoppedAnimation( + widget.event?.eventUserUes == id + ? Colors.white + : theme.accentColor), + strokeWidth: 2, + )), + SizedBox( + width: 8.0, + ) + ]); + } + return rowChildren; + }()), + onPressed: () async { + setState(() { + loadingUes = id; + }); + await bloc.updateUesEvent( + widget.event, widget.event.eventUserUes == id ? 0 : id); + setState(() { + loadingUes = 0; + // event has changes + }); + }, + ); + } +} diff --git a/lib/src/routes/feedpage.dart b/lib/src/routes/feedpage.dart index 62402fc5..f92645f0 100644 --- a/lib/src/routes/feedpage.dart +++ b/lib/src/routes/feedpage.dart @@ -87,64 +87,24 @@ class _FeedPageState extends State { ); } - String _formSubTitle(Event event) { - String subtitle = ""; - - DateTime startTime = DateTime.parse(event.eventStartTime); - DateTime endTime = DateTime.parse(event.eventEndTime); - DateTime timeNow = DateTime.now(); - bool eventStarted = timeNow.compareTo(startTime) > 0; - bool eventEnded = timeNow.compareTo(endTime) > 0; - - if (eventEnded) - subtitle += "Ended | "; - else if (eventStarted) { - int difference = - endTime.millisecondsSinceEpoch - timeNow.millisecondsSinceEpoch; - int minutes = difference ~/ (60 * 1000) % 60; - int hours = difference ~/ (60 * 60 * 1000) % 24; - int days = difference ~/ (24 * 60 * 60 * 1000); - String timeDiff = ""; - if (days > 0) timeDiff += "${days}D "; - if (hours > 0) timeDiff += "${hours}H "; - - timeDiff += "${minutes}M"; - - subtitle += "Ends in $timeDiff | "; - } - - if (startTime != null) { - // TODO: Time Date Formatting - subtitle += startTime.toIso8601String(); - // SimpleDateFormat simpleDateFormatDate = new SimpleDateFormat("dd MMM"); - // SimpleDateFormat simpleDateFormatTime = new SimpleDateFormat("HH:mm"); - - // subtitle += simpleDateFormatDate.format(DateTime) + " | " + simpleDateFormatTime.format(DateTime); - } - String eventVenueName = ""; - for (var venue in event.eventVenues) { - eventVenueName += ", ${venue.venueShortName}"; - } - if (eventVenueName != "") { - subtitle += " | ${eventVenueName.substring(2)}"; - } - - return subtitle; - } - Widget _buildEvent(Event event) { var theme = Theme.of(context); if (event.eventBigImage ?? false) { return InkWell( - onTap: () {}, + onTap: () { + _openEventPage(event); + }, child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ - Image.network( - event.eventImageURL ?? event.eventBodies[0].imageUrl, - fit: BoxFit.cover, + Ink.image( + child: Container(), + image: NetworkImage( + event.eventImageURL ?? event.eventBodies[0].imageUrl, + ), height: 200, + fit: BoxFit.cover, ), ListTile( contentPadding: @@ -154,7 +114,7 @@ class _FeedPageState extends State { style: theme.textTheme.title, ), enabled: true, - subtitle: Text(_formSubTitle(event)), + subtitle: Text(event.getSubTitle()), ) ], ), @@ -171,9 +131,15 @@ class _FeedPageState extends State { backgroundImage: NetworkImage( event.eventImageURL ?? event.eventBodies[0].imageUrl), ), - subtitle: Text(_formSubTitle(event)), - onTap: () {}, + subtitle: Text(event.getSubTitle()), + onTap: () { + _openEventPage(event); + }, ); } } + + _openEventPage(Event event) { + Navigator.of(context).pushNamed("/event/${event.eventID}"); + } } diff --git a/pubspec.yaml b/pubspec.yaml index 44bc3408..e314d6d1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,6 +31,7 @@ dependencies: flutter_html: ^0.8.2 markdown: ^2.0.2 shared_preferences: ^0.4.3 + date_format: ^1.0.5 dev_dependencies: flutter_test: