Skip to content

feat(#50): Support UI update when updating locale using NStack SDK instance #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MainScreen extends StatelessWidget {
? const Locale('de-DE')
: const Locale('en-EN');

NStackScope.of(context).changeLanguage(locale);
NStack.localization.changeLocalization(locale);
},
child: Text(
'Selected locale: ${activeLanguage.name}',
Expand Down
19 changes: 19 additions & 0 deletions example/lib/nstack.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* `context.localization.yourSection.yourKey`.
*/

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:nstack/models/app_open_platform.dart';
Expand Down Expand Up @@ -181,19 +183,36 @@ class NStackState extends State<NStackWidget> {

late Future<bool> _nstackInitFuture;

late final StreamSubscription _localeChangedSubscription;

@override
void initState() {
super.initState();

_nstackInitFuture = _nstack.init();

_localeChangedSubscription =
NStack.localization.onLocaleChanged.listen(_onLocaleChanged);
}

void _onLocaleChanged(Locale locale) {
setState(() {});
}

@Deprecated('Use `NStack.localization.changeLocalization` instead')
Future<void> changeLanguage(Locale locale) {
return _nstack.localization
.changeLocalization(locale)
.whenComplete(() => setState(() {}));
}

@override
void dispose() {
_localeChangedSubscription.cancel();

super.dispose();
}

@override
Widget build(BuildContext context) {
if (!_initializedNStack) {
Expand Down
32 changes: 26 additions & 6 deletions lib/sdk/localization/nstack_localization.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert';

import 'package:flutter/widgets.dart';
Expand All @@ -15,6 +16,8 @@ class NStackLocalization<TLocalization> {
static const _prefsKeyLastUpdated = "nstack_last_updated";
static const _prefsSelectedLocale = "nstack_selected_locale";

final _onLocaleChanged = StreamController<Locale>.broadcast();

final NStackConfig config;
final TLocalization translations;
final NStackRepository _repository;
Expand All @@ -31,6 +34,8 @@ class NStackLocalization<TLocalization> {

String get checksum => LocalizationRepository().checksum;

Stream<Locale> get onLocaleChanged => _onLocaleChanged.stream;

Locale? clientLocale;

NStackLocalization({
Expand All @@ -53,6 +58,19 @@ class NStackLocalization<TLocalization> {
);
}

Future<void> _updateLocaleAndNotify({
required Map<String, dynamic> translationData,
required String languageLocale,
required Locale locale,
}) async {
await LocalizationRepository().updateLocalization(
translationData,
languageLocale,
);

_onLocaleChanged.add(locale);
}

/// Change the localization in the internal map
/// 1. Find the best match in the language list that the project was built with
/// 2. Look if we have a cached localization in preferences and use that
Expand All @@ -76,9 +94,10 @@ class NStackLocalization<TLocalization> {

final languageResponse = LocalizationData.fromJson(cachedResponse);

LocalizationRepository().updateLocalization(
languageResponse.data!,
localLanguage.language!.locale!,
await _updateLocaleAndNotify(
translationData: languageResponse.data!,
languageLocale: localLanguage.language!.locale!,
locale: locale,
);

_log("Switched cached localization...");
Expand All @@ -94,9 +113,10 @@ class NStackLocalization<TLocalization> {
jsonDecode(localizationResponse),
);

LocalizationRepository().updateLocalization(
translationJson.data!,
localLanguage.language!.locale!,
await _updateLocaleAndNotify(
translationData: translationJson.data!,
languageLocale: localLanguage.language!.locale!,
locale: locale,
);

_log("Switched API localization...");
Expand Down
18 changes: 18 additions & 0 deletions lib/src/nstack_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class NstackBuilder implements Builder {
* `context.localization.yourSection.yourKey`.
*/

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:nstack/models/app_open_platform.dart';
Expand Down Expand Up @@ -328,17 +330,33 @@ class NStackState extends State<NStackWidget> {

late Future<bool> _nstackInitFuture;

late final StreamSubscription _localeChangedSubscription;

@override
void initState() {
super.initState();

_nstackInitFuture = _nstack.init();

_localeChangedSubscription = NStack.localization.onLocaleChanged.listen(_onLocaleChanged);
}

void _onLocaleChanged(Locale locale) {
setState(() {});
}

@Deprecated('Use `NStack.localization.changeLocalization` instead')
Future<void> changeLanguage(Locale locale) {
return _nstack.localization.changeLocalization(locale).whenComplete(() => setState(() {}));
}

@override
void dispose() {
_localeChangedSubscription.cancel();

super.dispose();
}

@override
Widget build(BuildContext context) {
if (!_initializedNStack) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class LocalizationRepository {
_setupInternalMap();
}

void updateLocalization(
Future<void> updateLocalization(
Map<String, dynamic> localizationJson,
String bestFitLocale,
) {
Expand All @@ -68,7 +68,7 @@ class LocalizationRepository {
orElse: () => this._pickedLanguage,
);
_sectionsMap = localizationJson;
_persistInternalMap();
return _persistInternalMap();
}

overridePickedLanguage(String locale) {
Expand All @@ -95,7 +95,7 @@ class LocalizationRepository {
}
}

_persistInternalMap() async {
Future<void> _persistInternalMap() async {
try {
WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
Expand Down