nc-photos/app/lib/widget/sign_in.dart

161 lines
5 KiB
Dart
Raw Normal View History

2022-07-28 18:59:26 +02:00
import 'dart:async';
2024-10-16 19:51:49 +02:00
import 'package:copy_with/copy_with.dart';
2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2021-04-10 06:28:12 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
2024-10-16 19:51:49 +02:00
import 'package:nc_photos/bloc_util.dart';
2024-08-18 12:07:56 +02:00
import 'package:nc_photos/controller/pref_controller.dart';
import 'package:nc_photos/db/entity_converter.dart';
2023-07-17 09:35:45 +02:00
import 'package:nc_photos/entity/pref.dart';
import 'package:nc_photos/entity/pref_util.dart' as pref_util;
2024-10-16 19:51:49 +02:00
import 'package:nc_photos/exception_event.dart';
import 'package:nc_photos/snack_bar_manager.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/theme.dart';
2024-08-10 13:13:56 +02:00
import 'package:nc_photos/widget/app_intermediate_circular_progress_indicator.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/widget/connect.dart';
import 'package:nc_photos/widget/home.dart';
2024-10-16 19:51:49 +02:00
import 'package:nc_photos/widget/page_visibility_mixin.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/widget/root_picker.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
import 'package:np_collection/np_collection.dart';
2024-10-16 19:51:49 +02:00
import 'package:np_common/unique.dart';
import 'package:np_db/np_db.dart';
2023-08-25 18:37:17 +02:00
import 'package:np_string/np_string.dart';
2024-10-16 19:51:49 +02:00
import 'package:to_string/to_string.dart';
2022-12-16 16:01:04 +01:00
part 'sign_in.g.dart';
2024-10-16 19:51:49 +02:00
part 'sign_in/bloc.dart';
part 'sign_in/state_event.dart';
part 'sign_in/type.dart';
part 'sign_in/view.dart';
2021-04-10 06:28:12 +02:00
2024-10-16 19:51:49 +02:00
class SignIn extends StatelessWidget {
2021-04-10 06:28:12 +02:00
static const routeName = "/sign-in";
2024-05-28 17:10:33 +02:00
const SignIn({super.key});
2021-04-10 06:28:12 +02:00
@override
2024-10-16 19:51:49 +02:00
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => _Bloc(
npDb: context.read(),
prefController: context.read(),
),
child: const _WrappedSignIn(),
);
}
2021-04-10 06:28:12 +02:00
}
2022-12-16 16:01:04 +01:00
@npLog
2024-10-16 19:51:49 +02:00
class _WrappedSignIn extends StatefulWidget {
const _WrappedSignIn();
2021-04-10 06:28:12 +02:00
@override
2024-10-16 19:51:49 +02:00
State<StatefulWidget> createState() => _WrappedSignInState();
}
class _WrappedSignInState extends State<_WrappedSignIn>
with RouteAware, PageVisibilityMixin {
@override
Widget build(BuildContext context) {
2022-11-23 17:22:09 +01:00
return Theme(
data: buildDarkTheme(context).copyWith(
2022-11-23 17:22:09 +01:00
scaffoldBackgroundColor: Colors.transparent,
2022-11-27 08:02:47 +01:00
progressIndicatorTheme: const ProgressIndicatorThemeData(
color: Colors.white,
),
2022-11-23 17:22:09 +01:00
textButtonTheme: TextButtonThemeData(
style: ButtonStyle(
foregroundColor: WidgetStateProperty.all(Colors.white),
2022-11-23 17:22:09 +01:00
),
),
),
child: Stack(
fit: StackFit.expand,
children: [
2024-10-16 19:51:49 +02:00
const _Background(),
2022-11-23 17:22:09 +01:00
Scaffold(
2024-10-16 19:51:49 +02:00
body: MultiBlocListener(
listeners: [
_BlocListenerT(
selector: (state) => state.connectUri,
listener: (context, connectUri) {
if (connectUri != null) {
_onConnect(context, connectUri.value);
}
},
),
_BlocListenerT(
selector: (state) => state.isCompleted,
listener: (context, isCompleted) {
if (isCompleted) {
Navigator.pushNamedAndRemoveUntil(
context,
Home.routeName,
(route) => false,
arguments:
HomeArguments(context.state.connectedAccount!),
);
}
},
2021-04-10 06:28:12 +02:00
),
2024-10-16 19:51:49 +02:00
_BlocListenerT(
selector: (state) => state.error,
listener: (context, error) {
if (error != null && isPageVisible()) {
SnackBarManager().showSnackBarForException(error.error);
}
},
2021-04-10 06:28:12 +02:00
),
2022-11-23 17:22:09 +01:00
],
2024-10-16 19:51:49 +02:00
child: _BlocSelector(
selector: (state) => state.isConnecting,
builder: (context, isConnecting) =>
isConnecting ? const _ConnectingBody() : const _Body(),
),
),
),
2024-10-16 19:51:49 +02:00
],
),
);
2021-04-10 06:28:12 +02:00
}
2024-10-16 19:51:49 +02:00
Future<void> _onConnect(BuildContext context, Uri connectUri) async {
var account = await Navigator.pushNamed<Account>(
context,
Connect.routeName,
arguments: ConnectArguments(connectUri),
);
2021-12-05 13:02:22 +01:00
if (account == null) {
// connection failed
return;
}
2024-10-16 19:51:49 +02:00
account = await Navigator.pushNamed<Account>(
context,
RootPicker.routeName,
arguments: RootPickerArguments(account),
);
2021-12-05 13:02:22 +01:00
if (account == null) {
// ???
return;
}
// we've got a good account
2024-10-16 19:51:49 +02:00
context.addEvent(_SetConnectedAccount(account));
2021-04-10 06:28:12 +02:00
}
}
2024-10-16 19:51:49 +02:00
// typedef _BlocBuilder = BlocBuilder<_Bloc, _State>;
// typedef _BlocListener = BlocListener<_Bloc, _State>;
typedef _BlocListenerT<T> = BlocListenerT<_Bloc, _State, T>;
typedef _BlocSelector<T> = BlocSelector<_Bloc, _State, T>;
typedef _Emitter = Emitter<_State>;
2022-11-23 17:22:09 +01:00
2024-10-16 19:51:49 +02:00
extension on BuildContext {
_Bloc get bloc => read<_Bloc>();
_State get state => bloc.state;
void addEvent(_Event event) => bloc.add(event);
2021-04-10 06:28:12 +02:00
}