From 07f255ec576f572fd5976227f6afee32dc16ba2a Mon Sep 17 00:00:00 2001 From: alkaitagi Date: Mon, 11 Jul 2022 20:08:24 +0300 Subject: [PATCH] Lazy load languages --- lib/modules/dictionary/dictionary.dart | 4 +- .../dictionary/widgets/search_toolbar.dart | 2 +- lib/modules/home/home.dart | 4 +- .../settings/widgets/editor_mode_card.dart | 24 ++++++++++-- lib/store.dart | 39 ++++--------------- 5 files changed, 33 insertions(+), 40 deletions(-) diff --git a/lib/modules/dictionary/dictionary.dart b/lib/modules/dictionary/dictionary.dart index 2b48e4e2..0c11c220 100644 --- a/lib/modules/dictionary/dictionary.dart +++ b/lib/modules/dictionary/dictionary.dart @@ -29,7 +29,7 @@ class DictionaryScreenState extends State { firstPageKey: 0, ); late final search = SearchController( - GlobalStore.languages.keys, + GlobalStore.languages, algolia.index('dictionary'), paging.refresh, ); @@ -137,7 +137,7 @@ class DictionaryScreenState extends State { leading: IconButton( onPressed: () async { await context.pushRoute(const HomeRoute()); - search.setLanguage('', GlobalStore.languages.keys); + search.setLanguage('', GlobalStore.languages); }, tooltip: 'Home', icon: const Icon(Icons.landscape_outlined), diff --git a/lib/modules/dictionary/widgets/search_toolbar.dart b/lib/modules/dictionary/widgets/search_toolbar.dart index 9ef7a570..21726ce5 100644 --- a/lib/modules/dictionary/widgets/search_toolbar.dart +++ b/lib/modules/dictionary/widgets/search_toolbar.dart @@ -73,7 +73,7 @@ class SearchToolbarState extends State { onTap: () => setLanguage(''), ), OptionItem.divider(), - for (final l in GlobalStore.languages.keys) + for (final l in GlobalStore.languages) OptionItem.tile( Transform.scale( scale: 1.25, diff --git a/lib/modules/home/home.dart b/lib/modules/home/home.dart index 2fb69db6..35e98d16 100644 --- a/lib/modules/home/home.dart +++ b/lib/modules/home/home.dart @@ -51,7 +51,7 @@ class _HomeScreenState extends State { .get() .then((r) => r.docs.map((d) => d.data()).toList()); - selected = GlobalStore.languages.keys + selected = GlobalStore.languages .map((n) => catalogue.firstWhere((l) => l.name == n)) .toSet(); tags = { @@ -134,7 +134,7 @@ class _HomeScreenState extends State { ? null : FloatingActionButton( onPressed: () { - GlobalStore.set(objects: selected); + GlobalStore.set(selected.map((l) => l.name).toList()); context.navigateTo(const RootRoute()); }, child: const Icon(Icons.done_all_outlined), diff --git a/lib/modules/settings/widgets/editor_mode_card.dart b/lib/modules/settings/widgets/editor_mode_card.dart index 40ac2f66..4a787e5d 100644 --- a/lib/modules/settings/widgets/editor_mode_card.dart +++ b/lib/modules/settings/widgets/editor_mode_card.dart @@ -7,6 +7,7 @@ import 'package:bazur/shared/widgets/column_card.dart'; import 'package:bazur/shared/widgets/language_avatar.dart'; import 'package:bazur/shared/widgets/span_icon.dart'; import 'package:bazur/store.dart'; +import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; @@ -20,6 +21,7 @@ class EditorModeCard extends StatefulWidget { class _EditorModeCardState extends State { late final StreamSubscription _authStream; var adminable = []; + List? languages; @override void initState() { @@ -36,6 +38,23 @@ class _EditorModeCardState extends State { } void updateAdminable() async { + if (languages == null) { + languages = []; + for (final l in GlobalStore.languages) { + await FirebaseFirestore.instance + .doc('languages/$l') + .withConverter( + fromFirestore: (snapshot, _) => + Language.fromJson(snapshot.data()!), + toFirestore: (_, __) => {}, + ) + .get() + .then((d) { + final l = d.data(); + if (l != null) languages!.add(l); + }); + } + } adminable = await EditorStore.getAdminable(); setState(() {}); } @@ -65,15 +84,14 @@ class _EditorModeCardState extends State { ], ), ), - if (EditorStore.user != null) + if (EditorStore.user != null && languages != null) Padding( padding: const EdgeInsets.all(8), child: Wrap( spacing: 8, runSpacing: 8, children: [ - for (final l - in GlobalStore.languages.values.whereType()) + for (final l in languages!) InputChip( materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, avatar: LanguageAvatar(l.name), diff --git a/lib/store.dart b/lib/store.dart index 7072df53..2e85aa21 100644 --- a/lib/store.dart +++ b/lib/store.dart @@ -1,8 +1,6 @@ import 'package:algolia/algolia.dart'; -import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'models/language.dart'; import 'shared/utils.dart'; late final Algolia algolia; @@ -48,39 +46,16 @@ class EditorStore { } class GlobalStore { - static Map languages = {}; + static var languages = []; - static void set({ - Iterable? names, - Iterable? objects, - }) { - if (objects != null) { - languages = {for (final l in objects) l.name: l}; - } else if (names != null) { - languages = {for (final l in names) l: null}; - } - if (!languages.containsKey(EditorStore.language)) { + static void set(List languages) { + GlobalStore.languages = [...languages]; + if (!languages.contains(EditorStore.language)) { EditorStore.language = null; } - prefs.setStringList('languages', languages.keys.toList()); + prefs.setStringList('languages', languages); } - static void init([List? names]) { - set( - names: names ?? prefs.getStringList('languages') ?? ['aghul'], - ); - for (final l in languages.keys) { - FirebaseFirestore.instance - .doc('languages/$l') - .withConverter( - fromFirestore: (snapshot, _) => Language.fromJson(snapshot.data()!), - toFirestore: (_, __) => {}, - ) - .get() - .then((r) { - final l = r.data(); - if (l != null) languages[l.name] = l; - }); - } - } + static void init([List? names]) => + set(prefs.getStringList('languages') ?? ['aghul']); }