import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'package:flutter_dotenv/flutter_dotenv.dart'; import '/views/main/conversation_list.dart'; import '/views/main/friend_list.dart'; import '/views/main/profile.dart'; import '/utils/storage/friends.dart'; import '/utils/storage/conversations.dart'; import '/utils/storage/messages.dart'; import '/utils/storage/session_cookie.dart'; import '/models/conversations.dart'; import '/models/friends.dart'; class Home extends StatefulWidget { const Home({Key? key}) : super(key: key); @override State createState() => _HomeState(); } class _HomeState extends State { List conversations = []; List friends = []; bool isLoading = true; int _selectedIndex = 0; List _widgetOptions = [ const ConversationList(conversations: []), const FriendList(friends: []), const Profile(), ]; @override void initState() { super.initState(); updateData(); } void updateData() async { await checkLogin(); await updateFriends(); await updateConversations(); await updateMessageThreads(); conversations = await getConversations(); friends = await getFriends(); setState(() { _widgetOptions = [ ConversationList(conversations: conversations), FriendList(friends: friends), const Profile(), ]; isLoading = false; }); } Future checkLogin() async { SharedPreferences preferences = await SharedPreferences.getInstance(); var loggedInTime = preferences.getString('logged_in_at'); if (loggedInTime == null) { preferences.remove('logged_in_at'); preferences.remove('username'); preferences.remove('userId'); preferences.remove('asymmetricPublicKey'); Navigator.pushNamedAndRemoveUntil(context, '/landing', ModalRoute.withName('/landing')); return; } DateTime loggedInAt = DateTime.parse(loggedInTime); bool isAfter = loggedInAt.isAfter((DateTime.now()).add(const Duration(hours: 12))); int statusCode = 200; try { var resp = await http.get( Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/check'), headers: { 'cookie': await getSessionCookie(), } ); statusCode = resp.statusCode; } catch(SocketException) { if (!isAfter) { return; } } if (!isAfter && statusCode == 200) { return; } preferences.remove('logged_in_at'); preferences.remove('username'); preferences.remove('userId'); preferences.remove('asymmetricPublicKey'); Navigator.pushNamedAndRemoveUntil(context, '/landing', ModalRoute.withName('/landing')); } void _onItemTapped(int index) { setState(() { _selectedIndex = index; }); } Widget loading() { return Stack( children: [ const Opacity( opacity: 0.1, child: ModalBarrier(dismissible: false, color: Colors.black), ), Center( child: Column( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.center, children: const [ CircularProgressIndicator(), SizedBox(height: 25), Text("Loading..."), ], ) ), ] ); } @override Widget build(BuildContext context) { return WillPopScope( onWillPop: () async => false, child: Scaffold( body: isLoading ? loading() : _widgetOptions.elementAt(_selectedIndex), bottomNavigationBar: isLoading ? const SizedBox.shrink() : BottomNavigationBar( currentIndex: _selectedIndex, onTap: _onItemTapped, selectedItemColor: Colors.red, unselectedItemColor: Colors.grey.shade600, selectedLabelStyle: const TextStyle(fontWeight: FontWeight.w600), unselectedLabelStyle: const TextStyle(fontWeight: FontWeight.w600), type: BottomNavigationBarType.fixed, items: const [ BottomNavigationBarItem( icon: Icon(Icons.message), label: "Chats", ), BottomNavigationBarItem( icon: Icon(Icons.group_work), label: "Friends", ), BottomNavigationBarItem( icon: Icon(Icons.account_box), label: "Profile", ), ], ), ), ); } }