home.dart 7.2 KB

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