home.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:trpp/data/data.dart';
  4. import 'package:trpp/data/theme.dart';
  5. import 'package:font_awesome_flutter/font_awesome_flutter.dart';
  6. import 'package:trpp/widgets/custom_alert_dialog.dart';
  7. import 'package:trpp/widgets/toolbar.dart';
  8. import 'note.dart';
  9. import 'settings.dart';
  10. class HomeScreen extends StatefulWidget {
  11. HomeScreen({Key key}) : super(key: key);
  12. @override
  13. _MyHomePageState createState() => _MyHomePageState();
  14. }
  15. class _MyHomePageState extends State<HomeScreen> {
  16. TupleTheme theme;
  17. List<NotesModel> notesList = [];
  18. List<NotificationModel> notificationList = [];
  19. bool _inDeletion = false;
  20. @override
  21. void initState() {
  22. super.initState();
  23. NotesDatabaseService.db.init();
  24. setNotesFromDB();
  25. setNotificationFormDB();
  26. setLocalTheme();
  27. }
  28. /// Set app theme form file
  29. setLocalTheme() async {
  30. theme = (await getCurrentTheme());
  31. }
  32. /// Loads notes from database
  33. setNotesFromDB() async {
  34. if (!_inDeletion) {
  35. var fetchedNotes = await NotesDatabaseService.db.getNotesFromDB();
  36. notesList = fetchedNotes;
  37. } else
  38. print("Skip notes list update due to deletion");
  39. setState(() {});
  40. }
  41. /// Loads notifications from database
  42. setNotificationFormDB() async {
  43. var fetchedNotes = await NotesDatabaseService.db.getNotificationsFromDB();
  44. notificationList = fetchedNotes;
  45. deleteBadNotifications();
  46. setState(() {});
  47. }
  48. deleteBadNotifications() async {
  49. List<NotificationModel> badGuys = List<NotificationModel>();
  50. for (int i = 0; i < notificationList.length; ++i) {
  51. if (DateTime.parse(notificationList[i].date1).isBefore(DateTime.now())) {
  52. NotesDatabaseService.db.deleteNotificationInDB(notificationList[i]);
  53. badGuys.add(notificationList[i]);
  54. }
  55. }
  56. for (int i = 0; i < badGuys.length; ++i)
  57. notificationList.remove(badGuys[i]);
  58. if (badGuys.isNotEmpty) setState(() {});
  59. }
  60. @override
  61. Widget build(BuildContext context) {
  62. return Scaffold(
  63. body: SafeArea(
  64. child: Column(
  65. children: <Widget>[
  66. //HomeAppBar(),
  67. CustomToolbar(
  68. needBackBtn: false,
  69. title: "Home",
  70. icon: FontAwesomeIcons.cog,
  71. onPressed: openSettings,
  72. ),
  73. CustomListView(
  74. notesList: notesList,
  75. openNote: openNote,
  76. onDismissed: dismissNote,
  77. notificationList: notificationList,
  78. ),
  79. ],
  80. ),
  81. ),
  82. floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
  83. floatingActionButton: Padding(
  84. padding: EdgeInsets.only(right: 8, bottom: 46),
  85. child: FloatingActionButton(
  86. child: Icon(FontAwesomeIcons.plus),
  87. onPressed: () => openNote(NOTESCREEN_MODE_EDIT, null, true, null)),
  88. ),
  89. backgroundColor: Theme.of(context).backgroundColor,
  90. );
  91. }
  92. void openSettings() {
  93. Navigator.push(
  94. context,
  95. MaterialPageRoute(
  96. builder: (context) => SettingsScreen(
  97. title: theme,
  98. )));
  99. }
  100. void openNote(bool mode, NotesModel nm, bool isNew,
  101. NotificationModel notificationModel) async {
  102. final res = await Navigator.push(
  103. context,
  104. MaterialPageRoute(
  105. builder: (context) => AddNoteScreen(
  106. oldNm: nm,
  107. isNew: isNew,
  108. mode: mode,
  109. notificationModel: notificationModel)));
  110. if (res != null && res) {
  111. setNotesFromDB();
  112. setNotificationFormDB();
  113. }
  114. }
  115. dismissNote(NotesModel nm) async {
  116. _inDeletion = true;
  117. _inDeletion = !(await NotesDatabaseService.db.deleteNoteInDB(nm));
  118. notesList.remove(nm);
  119. setState(() {});
  120. }
  121. }
  122. /// Make elements on home screen dismissible
  123. class CustomDismissible extends StatelessWidget {
  124. CustomDismissible(
  125. {Key key,
  126. this.index,
  127. this.nm,
  128. this.openNote,
  129. this.onDismissed,
  130. this.notificationModel})
  131. : super(key: key);
  132. final int index;
  133. final NotesModel nm;
  134. final NotificationModel notificationModel;
  135. final Function openNote;
  136. final Function onDismissed;
  137. @override
  138. Widget build(BuildContext context) {
  139. return Dismissible(
  140. key: ValueKey(index),
  141. direction: DismissDirection.endToStart,
  142. child: Card(child: NoteListItem(index, nm, openNote, notificationModel)),
  143. background: Padding(
  144. padding: EdgeInsets.only(right: 30),
  145. child: Align(
  146. alignment: Alignment.centerRight,
  147. child: Icon(FontAwesomeIcons.trashAlt,
  148. color: Colors.red.shade500, size: 28),
  149. ),
  150. ),
  151. onDismissed: (direction) {
  152. onDismissed(nm);
  153. },
  154. confirmDismiss: (direction) => showDialog(
  155. context: context, builder: (context) => CustomAlertDialog()),
  156. );
  157. }
  158. }
  159. class CustomListView extends StatelessWidget {
  160. final List<NotesModel> notesList;
  161. final List<NotificationModel> notificationList;
  162. final Function openNote;
  163. final Function onDismissed;
  164. CustomListView(
  165. {Key key,
  166. this.notesList,
  167. this.openNote,
  168. this.onDismissed,
  169. this.notificationList})
  170. : super(key: key);
  171. NotificationModel getModel(int index) {
  172. for (int i = 0; i < notificationList.length; ++i)
  173. if (notificationList[i].note == notesList[index].id)
  174. return notificationList[i];
  175. return null;
  176. }
  177. @override
  178. Widget build(BuildContext context) {
  179. return Flexible(
  180. child: ListView.builder(
  181. padding: EdgeInsets.all(10),
  182. physics: BouncingScrollPhysics(),
  183. itemCount: notesList.length,
  184. itemBuilder: (context, index) => CustomDismissible(
  185. index: index,
  186. nm: notesList[index],
  187. openNote: openNote,
  188. onDismissed: onDismissed,
  189. notificationModel: getModel(index),
  190. ),
  191. ),
  192. );
  193. }
  194. }
  195. class NoteListItem extends StatelessWidget {
  196. NoteListItem(this.index, this.nm, this.openNote, this.notificationModel);
  197. final int index;
  198. final NotesModel nm;
  199. final NotificationModel notificationModel;
  200. final Function openNote;
  201. /// Returns text, stored on notification model
  202. String getText() {
  203. if (notificationModel != null)
  204. return notificationModel.getString();
  205. else
  206. return "";
  207. }
  208. @override
  209. Widget build(BuildContext context) {
  210. return ListTile(
  211. title: Text(
  212. nm.getTitleFromModel(16),
  213. maxLines: 2,
  214. overflow: TextOverflow.ellipsis,
  215. style: TextStyle(
  216. fontSize: 19,
  217. fontWeight: FontWeight.w500,
  218. color: Theme.of(context).primaryColor,
  219. ),
  220. ),
  221. subtitle: Padding(
  222. padding: const EdgeInsets.only(top: 8),
  223. child: Text(
  224. nm.getShortDesc(24),
  225. overflow: TextOverflow.ellipsis,
  226. maxLines: 1,
  227. style: TextStyle(
  228. fontSize: 16,
  229. fontWeight: FontWeight.w300,
  230. color: Theme.of(context).primaryColor,
  231. ),
  232. ),
  233. ),
  234. trailing: Text(
  235. getText(),
  236. style: TextStyle(
  237. fontSize: 12,
  238. fontWeight: FontWeight.w400,
  239. color: Theme.of(context).accentColor,
  240. ),
  241. ),
  242. onTap: () => openNote(NOTESCREEN_MODE_VIEW, nm, false, notificationModel),
  243. contentPadding: EdgeInsets.all(17),
  244. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
  245. tileColor: Theme.of(context).cardColor,
  246. );
  247. }
  248. }