Skip to content

Commit

Permalink
Lazy load languages
Browse files Browse the repository at this point in the history
  • Loading branch information
alkaitagi committed Jul 11, 2022
1 parent 26082a7 commit 07f255e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 40 deletions.
4 changes: 2 additions & 2 deletions lib/modules/dictionary/dictionary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DictionaryScreenState extends State<DictionaryScreen> {
firstPageKey: 0,
);
late final search = SearchController(
GlobalStore.languages.keys,
GlobalStore.languages,
algolia.index('dictionary'),
paging.refresh,
);
Expand Down Expand Up @@ -137,7 +137,7 @@ class DictionaryScreenState extends State<DictionaryScreen> {
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),
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/dictionary/widgets/search_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class SearchToolbarState extends State<SearchToolbar> {
onTap: () => setLanguage(''),
),
OptionItem.divider(),
for (final l in GlobalStore.languages.keys)
for (final l in GlobalStore.languages)
OptionItem.tile(
Transform.scale(
scale: 1.25,
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class _HomeScreenState extends State<HomeScreen> {
.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 = {
Expand Down Expand Up @@ -134,7 +134,7 @@ class _HomeScreenState extends State<HomeScreen> {
? 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),
Expand Down
24 changes: 21 additions & 3 deletions lib/modules/settings/widgets/editor_mode_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -20,6 +21,7 @@ class EditorModeCard extends StatefulWidget {
class _EditorModeCardState extends State<EditorModeCard> {
late final StreamSubscription<User?> _authStream;
var adminable = <String>[];
List<Language>? languages;

@override
void initState() {
Expand All @@ -36,6 +38,23 @@ class _EditorModeCardState extends State<EditorModeCard> {
}

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(() {});
}
Expand Down Expand Up @@ -65,15 +84,14 @@ class _EditorModeCardState extends State<EditorModeCard> {
],
),
),
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<Language>())
for (final l in languages!)
InputChip(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
avatar: LanguageAvatar(l.name),
Expand Down
39 changes: 7 additions & 32 deletions lib/store.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -48,39 +46,16 @@ class EditorStore {
}

class GlobalStore {
static Map<String, Language?> languages = {};
static var languages = <String>[];

static void set({
Iterable<String>? names,
Iterable<Language>? 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<String> 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<String>? 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<String>? names]) =>
set(prefs.getStringList('languages') ?? ['aghul']);
}

0 comments on commit 07f255e

Please sign in to comment.