Browse Source

Merge pull request #45 from jld3103/refactor/package-info-usage

neon: Refactor package info usage
pull/32/head
jld3103 2 years ago committed by GitHub
parent
commit
f915186ca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      packages/neon/lib/main.dart
  2. 9
      packages/neon/lib/src/blocs/accounts.dart
  3. 7
      packages/neon/lib/src/blocs/login.dart
  4. 25
      packages/neon/lib/src/models/account.dart
  5. 10
      packages/neon/lib/src/pages/login/login.dart
  6. 2
      packages/neon/lib/src/pages/settings/settings.dart
  7. 5
      packages/neon/lib/src/utils/global.dart
  8. 10
      packages/neon/lib/src/utils/global_options.dart

10
packages/neon/lib/main.dart

@ -6,6 +6,7 @@ import 'package:neon/app.dart';
import 'package:neon/src/blocs/accounts.dart'; import 'package:neon/src/blocs/accounts.dart';
import 'package:neon/src/blocs/push_notifications.dart'; import 'package:neon/src/blocs/push_notifications.dart';
import 'package:neon/src/neon.dart'; import 'package:neon/src/neon.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@ -25,16 +26,17 @@ Future main() async {
FlutterNativeSplash.preserve(widgetsBinding: WidgetsBinding.instance); FlutterNativeSplash.preserve(widgetsBinding: WidgetsBinding.instance);
await Global.init();
final sharedPreferences = await SharedPreferences.getInstance(); final sharedPreferences = await SharedPreferences.getInstance();
final platform = await getNeonPlatform(); final platform = await getNeonPlatform();
final requestManager = await getRequestManager(platform); final requestManager = await getRequestManager(platform);
final allAppImplementations = getAppImplementations(sharedPreferences, requestManager, platform); final allAppImplementations = getAppImplementations(sharedPreferences, requestManager, platform);
final packageInfo = await PackageInfo.fromPlatform();
final globalOptions = GlobalOptions( final globalOptions = GlobalOptions(
Storage('global', sharedPreferences), Storage('global', sharedPreferences),
packageInfo,
); );
final accountsBloc = AccountsBloc( final accountsBloc = AccountsBloc(
@ -42,6 +44,7 @@ Future main() async {
Storage('accounts', sharedPreferences), Storage('accounts', sharedPreferences),
sharedPreferences, sharedPreferences,
globalOptions, globalOptions,
packageInfo,
); );
final pushNotificationsBloc = PushNotificationsBloc( final pushNotificationsBloc = PushNotificationsBloc(
accountsBloc, accountsBloc,
@ -78,6 +81,9 @@ Future main() async {
Provider<List<AppImplementation>>( Provider<List<AppImplementation>>(
create: (final _) => allAppImplementations, create: (final _) => allAppImplementations,
), ),
Provider<PackageInfo>(
create: (final _) => packageInfo,
),
], ],
child: NeonApp( child: NeonApp(
accountsBloc: accountsBloc, accountsBloc: accountsBloc,

9
packages/neon/lib/src/blocs/accounts.dart

@ -4,6 +4,7 @@ import 'package:neon/src/blocs/user_details.dart';
import 'package:neon/src/blocs/user_status.dart'; import 'package:neon/src/blocs/user_status.dart';
import 'package:neon/src/models/account.dart'; import 'package:neon/src/models/account.dart';
import 'package:neon/src/neon.dart'; import 'package:neon/src/neon.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:rx_bloc/rx_bloc.dart'; import 'package:rx_bloc/rx_bloc.dart';
import 'package:rxdart/rxdart.dart'; import 'package:rxdart/rxdart.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@ -29,6 +30,7 @@ class AccountsBloc extends $AccountsBloc {
this._storage, this._storage,
this._sharedPreferences, this._sharedPreferences,
this._globalOptions, this._globalOptions,
this._packageInfo,
) { ) {
_accountsSubject.listen((final accounts) async { _accountsSubject.listen((final accounts) async {
_globalOptions.updateAccounts(accounts); _globalOptions.updateAccounts(accounts);
@ -53,6 +55,7 @@ class AccountsBloc extends $AccountsBloc {
}); });
_$addAccountEvent.listen((final account) async { _$addAccountEvent.listen((final account) async {
account.setupClient(_packageInfo);
if (_activeAccountSubject.valueOrNull == null) { if (_activeAccountSubject.valueOrNull == null) {
setActiveAccount(account); setActiveAccount(account);
} }
@ -71,6 +74,7 @@ class AccountsBloc extends $AccountsBloc {
}); });
_$updateAccountEvent.listen((final account) async { _$updateAccountEvent.listen((final account) async {
account.setupClient(_packageInfo);
final accounts = _accountsSubject.value; final accounts = _accountsSubject.value;
final index = accounts.indexWhere((final a) => a.id == account.id); final index = accounts.indexWhere((final a) => a.id == account.id);
if (index == -1) { if (index == -1) {
@ -92,7 +96,7 @@ class AccountsBloc extends $AccountsBloc {
_accountsSubject.add( _accountsSubject.add(
_storage _storage
.getStringList(_keyAccounts)! .getStringList(_keyAccounts)!
.map((final a) => Account.fromJson(json.decode(a) as Map<String, dynamic>)) .map((final a) => (Account.fromJson(json.decode(a) as Map<String, dynamic>))..setupClient(_packageInfo))
.toList(), .toList(),
); );
} }
@ -126,13 +130,14 @@ class AccountsBloc extends $AccountsBloc {
return null; return null;
} }
final RequestManager _requestManager;
final Storage _storage; final Storage _storage;
final SharedPreferences _sharedPreferences; final SharedPreferences _sharedPreferences;
final GlobalOptions _globalOptions; final GlobalOptions _globalOptions;
final PackageInfo _packageInfo;
final _keyAccounts = 'accounts'; final _keyAccounts = 'accounts';
final _keyLastUsedAccount = 'last-used-account'; final _keyLastUsedAccount = 'last-used-account';
final RequestManager _requestManager;
final _accountsOptions = <String, AccountSpecificOptions>{}; final _accountsOptions = <String, AccountSpecificOptions>{};
late final _activeAccountSubject = BehaviorSubject<Account?>.seeded(null); late final _activeAccountSubject = BehaviorSubject<Account?>.seeded(null);
late final _accountsSubject = BehaviorSubject<List<Account>>.seeded([]); late final _accountsSubject = BehaviorSubject<List<Account>>.seeded([]);

7
packages/neon/lib/src/blocs/login.dart

@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:neon/src/models/account.dart'; import 'package:neon/src/models/account.dart';
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:rx_bloc/rx_bloc.dart'; import 'package:rx_bloc/rx_bloc.dart';
import 'package:rxdart/rxdart.dart'; import 'package:rxdart/rxdart.dart';
@ -24,7 +25,7 @@ abstract class LoginBlocStates {
@RxBloc() @RxBloc()
class LoginBloc extends $LoginBloc { class LoginBloc extends $LoginBloc {
LoginBloc() { LoginBloc(this._packageInfo) {
_$setServerURLEvent.listen((final url) async { _$setServerURLEvent.listen((final url) async {
_serverURLSubject.add(url); _serverURLSubject.add(url);
_loginFlowInitSubject.add(null); _loginFlowInitSubject.add(null);
@ -35,7 +36,7 @@ class LoginBloc extends $LoginBloc {
try { try {
final client = NextcloudClient( final client = NextcloudClient(
url, url,
userAgentOverride: userAgentOverride(), userAgentOverride: userAgent(_packageInfo),
); );
final status = (await client.core.getStatus())!; final status = (await client.core.getStatus())!;
@ -74,6 +75,8 @@ class LoginBloc extends $LoginBloc {
} }
} }
final PackageInfo _packageInfo;
final _serverURLSubject = BehaviorSubject<String?>.seeded(null); final _serverURLSubject = BehaviorSubject<String?>.seeded(null);
final _serverConnectionStateSubject = BehaviorSubject<ServerConnectionState?>.seeded(null); final _serverConnectionStateSubject = BehaviorSubject<ServerConnectionState?>.seeded(null);
final _loginFlowInitSubject = BehaviorSubject<CoreLoginFlowInit?>.seeded(null); final _loginFlowInitSubject = BehaviorSubject<CoreLoginFlowInit?>.seeded(null);

25
packages/neon/lib/src/models/account.dart

@ -5,12 +5,13 @@ import 'package:json_annotation/json_annotation.dart';
import 'package:neon/l10n/localizations.dart'; import 'package:neon/l10n/localizations.dart';
import 'package:neon/src/neon.dart'; import 'package:neon/src/neon.dart';
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:rxdart/rxdart.dart'; import 'package:rxdart/rxdart.dart';
import 'package:settings/settings.dart'; import 'package:settings/settings.dart';
part 'account.g.dart'; part 'account.g.dart';
String userAgentOverride() => 'Neon ${Global.packageInfo.version}+${Global.packageInfo.buildNumber}'; String userAgent(PackageInfo packageInfo) => 'Neon ${packageInfo.version}+${packageInfo.buildNumber}';
@JsonSerializable() @JsonSerializable()
class Account { class Account {
@ -49,12 +50,22 @@ class Account {
NextcloudClient? _client; NextcloudClient? _client;
NextcloudClient get client => _client ??= NextcloudClient( void setupClient(PackageInfo packageInfo) {
serverURL, _client ??= NextcloudClient(
username: username, serverURL,
password: password ?? appPassword, username: username,
userAgentOverride: userAgentOverride(), password: password ?? appPassword,
); userAgentOverride: userAgent(packageInfo),
);
}
NextcloudClient get client {
if (_client == null) {
throw Exception('You need to call setupClient() first');
}
return _client!;
}
} }
Map<String, String> _idCache = {}; Map<String, String> _idCache = {};

10
packages/neon/lib/src/pages/login/login.dart

@ -16,12 +16,16 @@ class _LoginPageState extends State<LoginPage> {
WebViewController? _webViewController; WebViewController? _webViewController;
final _formKey = GlobalKey<FormState>(); final _formKey = GlobalKey<FormState>();
final _focusNode = FocusNode(); final _focusNode = FocusNode();
final _loginBloc = LoginBloc(); late final PackageInfo _packageInfo;
late final LoginBloc _loginBloc;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_packageInfo = Provider.of<PackageInfo>(context, listen: false);
_loginBloc = LoginBloc(_packageInfo);
if (widget.serverURL != null) { if (widget.serverURL != null) {
_loginBloc.setServerURL(widget.serverURL!); _loginBloc.setServerURL(widget.serverURL!);
} }
@ -73,7 +77,7 @@ class _LoginPageState extends State<LoginPage> {
final Env? env, final Env? env,
) => ) =>
{ {
HttpHeaders.userAgentHeader: userAgentOverride(), HttpHeaders.userAgentHeader: userAgent(_packageInfo),
if (env != null) ...{ if (env != null) ...{
HttpHeaders.authorizationHeader: HttpHeaders.authorizationHeader:
'Basic ${base64.encode(utf8.encode('${env.testUsername}:${env.testPassword}'))}', 'Basic ${base64.encode(utf8.encode('${env.testUsername}:${env.testPassword}'))}',
@ -137,7 +141,7 @@ class _LoginPageState extends State<LoginPage> {
? WebView( ? WebView(
javascriptMode: JavascriptMode.unrestricted, javascriptMode: JavascriptMode.unrestricted,
zoomEnabled: false, zoomEnabled: false,
userAgent: userAgentOverride(), userAgent: userAgent(_packageInfo),
onWebViewCreated: (final controller) async { onWebViewCreated: (final controller) async {
_webViewController = controller; _webViewController = controller;
final url = final url =

2
packages/neon/lib/src/pages/settings/settings.dart

@ -249,7 +249,7 @@ class _SettingsPageState extends State<SettingsPage> {
withoutText: true, withoutText: true,
), ),
applicationLegalese: await rootBundle.loadString('assets/LEGALESE.txt'), applicationLegalese: await rootBundle.loadString('assets/LEGALESE.txt'),
applicationVersion: (await PackageInfo.fromPlatform()).version, applicationVersion: Provider.of<PackageInfo>(context, listen: false).version,
); );
}, },
), ),

5
packages/neon/lib/src/utils/global.dart

@ -1,11 +1,6 @@
part of '../neon.dart'; part of '../neon.dart';
class Global { class Global {
static Future init() async {
packageInfo = await PackageInfo.fromPlatform();
}
static late PackageInfo packageInfo;
static Function()? onPushNotificationReceived; static Function()? onPushNotificationReceived;
static Function(String? payload)? onPushNotificationClicked; static Function(String? payload)? onPushNotificationClicked;
static Future<bool> Function(NotificationsNotification notification)? handleNotificationOpening; static Future<bool> Function(NotificationsNotification notification)? handleNotificationOpening;

10
packages/neon/lib/src/utils/global_options.dart

@ -1,7 +1,10 @@
part of '../neon.dart'; part of '../neon.dart';
class GlobalOptions { class GlobalOptions {
GlobalOptions(this._storage) { GlobalOptions(
this._storage,
this._packageInfo,
) {
themeMode.stream.listen((final value) { themeMode.stream.listen((final value) {
_themeOLEDAsDarkEnabledSubject.add(value != ThemeMode.light); _themeOLEDAsDarkEnabledSubject.add(value != ThemeMode.light);
}); });
@ -29,13 +32,14 @@ class GlobalOptions {
} }
final Storage _storage; final Storage _storage;
final PackageInfo _packageInfo;
final _accountsIDsSubject = BehaviorSubject<Map<String?, LabelBuilder>>(); final _accountsIDsSubject = BehaviorSubject<Map<String?, LabelBuilder>>();
final _themeOLEDAsDarkEnabledSubject = BehaviorSubject<bool>(); final _themeOLEDAsDarkEnabledSubject = BehaviorSubject<bool>();
final _pushNotificationsEnabledSubject = BehaviorSubject<bool>(); final _pushNotificationsEnabledSubject = BehaviorSubject<bool>();
final _pushNotificationsDistributorsSubject = BehaviorSubject<Map<String?, LabelBuilder>>(); final _pushNotificationsDistributorsSubject = BehaviorSubject<Map<String?, LabelBuilder>>();
final _distributorsMap = <String, String Function(BuildContext)>{ late final _distributorsMap = <String, String Function(BuildContext)>{
Global.packageInfo.packageName: (final context) => _packageInfo.packageName: (final context) =>
AppLocalizations.of(context).globalOptionsPushNotificationsDistributorFirebaseEmbedded, AppLocalizations.of(context).globalOptionsPushNotificationsDistributorFirebaseEmbedded,
'com.github.gotify.up': (final context) => 'com.github.gotify.up': (final context) =>
AppLocalizations.of(context).globalOptionsPushNotificationsDistributorGotifyUP, AppLocalizations.of(context).globalOptionsPushNotificationsDistributorGotifyUP,

Loading…
Cancel
Save