forked from jmshrv/finamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
323 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import 'dart:collection'; | ||
|
||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
import 'package:locale_names/locale_names.dart'; | ||
|
||
import '../../services/finamp_settings_helper.dart'; | ||
import '../../services/locale_helper.dart'; | ||
|
||
class LanguageList extends StatefulWidget { | ||
const LanguageList({super.key}); | ||
|
||
@override | ||
State<LanguageList> createState() => _LanguageListState(); | ||
} | ||
|
||
class _LanguageListState extends State<LanguageList> { | ||
// yeah I'm a computer science student how could you tell | ||
// (sorts locales without having to copy them into a list first) | ||
final locales = SplayTreeMap<String, Locale>.fromIterable( | ||
AppLocalizations.supportedLocales, | ||
key: (element) => (element as Locale).defaultDisplayLanguage, | ||
value: (element) => element, | ||
); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scrollbar( | ||
// We have a ValueListenableBuilder here to rebuild all the ListTiles when | ||
// the language changes | ||
child: ValueListenableBuilder( | ||
valueListenable: LocaleHelper.localeListener, | ||
builder: (_, __, ___) { | ||
return CustomScrollView( | ||
slivers: [ | ||
const SliverList( | ||
delegate: SliverChildListDelegate.fixed([ | ||
LanguageListTile(), | ||
Divider(), | ||
]), | ||
), | ||
SliverList( | ||
delegate: SliverChildBuilderDelegate( | ||
(context, index) { | ||
final locale = locales.values.elementAt(index); | ||
|
||
return LanguageListTile(locale: locale); | ||
}, | ||
childCount: locales.length, | ||
), | ||
) | ||
], | ||
); | ||
}, | ||
), | ||
); | ||
} | ||
} | ||
|
||
class LanguageListTile extends StatelessWidget { | ||
const LanguageListTile({ | ||
super.key, | ||
this.locale, | ||
}); | ||
|
||
final Locale? locale; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return RadioListTile<Locale?>( | ||
title: Text(locale?.nativeDisplayLanguage ?? | ||
AppLocalizations.of(context)!.system), | ||
subtitle: locale == null ? null : Text(locale!.defaultDisplayLanguage), | ||
value: locale, | ||
groupValue: LocaleHelper.locale, | ||
onChanged: (_) { | ||
LocaleHelper.setLocale(locale); | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:hive/hive.dart'; | ||
|
||
class LocaleAdapter extends TypeAdapter<Locale> { | ||
@override | ||
int get typeId => 42; | ||
|
||
@override | ||
Locale read(BinaryReader reader) { | ||
final localeList = reader.readStringList(); | ||
|
||
final languageCode = localeList[0]; | ||
|
||
// scriptCode and countryCode are empty strings when null (see write) | ||
final scriptCode = localeList[1] == "" ? null : localeList[1]; | ||
final countryCode = localeList[2] == "" ? null : localeList[2]; | ||
|
||
return Locale.fromSubtags( | ||
languageCode: languageCode, | ||
scriptCode: scriptCode, | ||
countryCode: countryCode, | ||
); | ||
} | ||
|
||
@override | ||
void write(BinaryWriter writer, Locale obj) { | ||
writer.writeStringList([ | ||
obj.languageCode, | ||
obj.scriptCode ?? "", | ||
obj.countryCode ?? "", | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
|
||
import '../components/LanguageSelectionScreen/language_list.dart'; | ||
|
||
class LanguageSelectionScreen extends StatelessWidget { | ||
const LanguageSelectionScreen({super.key}); | ||
|
||
static const routeName = "/settings/language"; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text(AppLocalizations.of(context)!.language), | ||
), | ||
body: const LanguageList(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
|
||
class LocaleHelper { | ||
static const boxName = "Locale"; | ||
|
||
static ValueListenable<Box<Locale?>> get localeListener => | ||
Hive.box<Locale?>(boxName).listenable(); | ||
|
||
static Locale? get locale => Hive.box<Locale?>(boxName).get(boxName); | ||
|
||
static void setLocale(Locale? locale) { | ||
Hive.box<Locale?>(boxName).put(boxName, locale); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters