Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.6 KiB

  1. import 'package:flutter/material.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import '/views/main/conversation_list.dart';
  4. import '/views/main/friend_list.dart';
  5. import '/views/main/profile.dart';
  6. import '/utils/storage/friends.dart';
  7. import '/utils/storage/conversations.dart';
  8. import '/utils/storage/messages.dart';
  9. import '/models/conversations.dart';
  10. import '/models/friends.dart';
  11. class Home extends StatefulWidget {
  12. const Home({Key? key}) : super(key: key);
  13. @override
  14. State<Home> createState() => _HomeState();
  15. }
  16. class _HomeState extends State<Home> {
  17. List<Conversation> conversations = [];
  18. List<Friend> friends = [];
  19. bool isLoading = true;
  20. int _selectedIndex = 0;
  21. List<Widget> _widgetOptions = <Widget>[
  22. const ConversationList(conversations: []),
  23. const FriendList(friends: []),
  24. const Profile(),
  25. ];
  26. @override
  27. void initState() {
  28. super.initState();
  29. updateData();
  30. }
  31. void updateData() async {
  32. await checkLogin();
  33. await updateFriends();
  34. await updateConversations();
  35. await updateMessageThreads();
  36. conversations = await getConversations();
  37. friends = await getFriends();
  38. setState(() {
  39. _widgetOptions = <Widget>[
  40. ConversationList(conversations: conversations),
  41. FriendList(friends: friends),
  42. const Profile(),
  43. ];
  44. isLoading = false;
  45. });
  46. }
  47. // TODO: Do server GET check here
  48. Future checkLogin() async {
  49. SharedPreferences preferences = await SharedPreferences.getInstance();
  50. if (preferences.getBool('islogin') != true) {
  51. Navigator.pushNamedAndRemoveUntil(context, '/landing', ModalRoute.withName('/landing'));
  52. }
  53. }
  54. void _onItemTapped(int index) {
  55. setState(() {
  56. _selectedIndex = index;
  57. });
  58. }
  59. Widget loading() {
  60. return Stack(
  61. children: <Widget>[
  62. const Opacity(
  63. opacity: 0.1,
  64. child: ModalBarrier(dismissible: false, color: Colors.black),
  65. ),
  66. Center(
  67. child: Column(
  68. mainAxisSize: MainAxisSize.max,
  69. mainAxisAlignment: MainAxisAlignment.center,
  70. children: const <Widget> [
  71. CircularProgressIndicator(),
  72. SizedBox(height: 25),
  73. Text("Loading..."),
  74. ],
  75. )
  76. ),
  77. ]
  78. );
  79. }
  80. @override
  81. Widget build(BuildContext context) {
  82. return WillPopScope(
  83. onWillPop: () async => false,
  84. child: Scaffold(
  85. body: isLoading ? loading() : _widgetOptions.elementAt(_selectedIndex),
  86. bottomNavigationBar: isLoading ? const SizedBox.shrink() : BottomNavigationBar(
  87. currentIndex: _selectedIndex,
  88. onTap: _onItemTapped,
  89. selectedItemColor: Colors.red,
  90. unselectedItemColor: Colors.grey.shade600,
  91. selectedLabelStyle: const TextStyle(fontWeight: FontWeight.w600),
  92. unselectedLabelStyle: const TextStyle(fontWeight: FontWeight.w600),
  93. type: BottomNavigationBarType.fixed,
  94. items: const [
  95. BottomNavigationBarItem(
  96. icon: Icon(Icons.message),
  97. label: "Chats",
  98. ),
  99. BottomNavigationBarItem(
  100. icon: Icon(Icons.group_work),
  101. label: "Friends",
  102. ),
  103. BottomNavigationBarItem(
  104. icon: Icon(Icons.account_box),
  105. label: "Profile",
  106. ),
  107. ],
  108. ),
  109. ),
  110. );
  111. }
  112. }