diff --git a/lib/ui/mobile/widgets/buttons/menu_button.dart b/lib/ui/mobile/widgets/buttons/menu_button.dart index a0f4e041..d486f1b5 100644 --- a/lib/ui/mobile/widgets/buttons/menu_button.dart +++ b/lib/ui/mobile/widgets/buttons/menu_button.dart @@ -19,42 +19,72 @@ class MenuButton extends StatefulWidget { class _MenuButtonState extends State { final iconButtonKey = GlobalKey(); + static LargeLanguageModelType lastModelType = LargeLanguageModelType.none; + static DateTime lastCheck = DateTime.now(); + static List cache = []; List options = []; + bool canUseCache(Session session) { + if (cache.isEmpty && session.model.type != LargeLanguageModelType.llamacpp) return false; + + if (session.model.type != lastModelType) return false; + + if (DateTime.now().difference(lastCheck).inMinutes > 1) return false; + + return true; + } + @override Widget build(BuildContext context) { return Consumer( builder: (context, session, child) { - return FutureBuilder( - future: session.model.options, - builder: (context, snapshot) { - if (snapshot.connectionState == ConnectionState.done) { - options = snapshot.data as List; + if (canUseCache(session)) { + options = cache; + return IconButton( + key: iconButtonKey, + icon: const Icon( + Icons.account_tree_rounded, + size: 24, + ), + onPressed: onPressed, + ); + } + else { + lastModelType = session.model.type; + lastCheck = DateTime.now(); - return IconButton( - key: iconButtonKey, - icon: const Icon( - Icons.account_tree_rounded, - size: 24, - ), - onPressed: onPressed, - ); - } else { - return const Padding( - padding: EdgeInsets.all(8.0), // Adjust padding to match the visual space of the IconButton - child: SizedBox( - width: 24, // Width of the CircularProgressIndicator - height: 24, // Height of the CircularProgressIndicator - child: Center( - child: CircularProgressIndicator( - strokeWidth: 3.0, // Adjust the thickness of the spinner here + return FutureBuilder( + future: session.model.options, + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.done) { + options = snapshot.data as List; + cache = options; + + return IconButton( + key: iconButtonKey, + icon: const Icon( + Icons.account_tree_rounded, + size: 24, + ), + onPressed: onPressed, + ); + } else { + return const Padding( + padding: EdgeInsets.all(8.0), // Adjust padding to match the visual space of the IconButton + child: SizedBox( + width: 24, // Width of the CircularProgressIndicator + height: 24, // Height of the CircularProgressIndicator + child: Center( + child: CircularProgressIndicator( + strokeWidth: 3.0, // Adjust the thickness of the spinner here + ), ), ), - ), - ); + ); + } } - } - ); + ); + } } ); } @@ -130,6 +160,7 @@ class _MenuButtonState extends State { contentPadding: const EdgeInsets.symmetric(horizontal: 8.0), title: const Text('App Settings'), onTap: () { + cache.clear(); Navigator.pop(context); Navigator.push( context,