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';
|
2023-12-06 15:05:33 +01:00
|
|
|
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';
|
2023-12-06 15:05:33 +01:00
|
|
|
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';
|
2024-10-18 18:52:38 +02:00
|
|
|
import 'package:nc_photos/legacy/connect.dart' as legacy;
|
2024-10-16 19:51:49 +02:00
|
|
|
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';
|
2024-10-18 18:52:38 +02:00
|
|
|
import 'package:nc_photos/widget/expandable_container.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
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';
|
2023-08-25 19:31:06 +02:00
|
|
|
import 'package:np_collection/np_collection.dart';
|
2023-12-06 15:05:33 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-10-18 18:52:38 +02:00
|
|
|
@npLog
|
2024-10-16 19:51:49 +02:00
|
|
|
class _WrappedSignInState extends State<_WrappedSignIn>
|
|
|
|
with RouteAware, PageVisibilityMixin {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-11-23 17:22:09 +01:00
|
|
|
return Theme(
|
2023-08-19 18:47:56 +02:00
|
|
|
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(
|
2024-09-27 15:12:21 +02:00
|
|
|
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(
|
2024-10-18 18:52:38 +02:00
|
|
|
selector: (state) => state.connectArg,
|
|
|
|
listener: (context, connectArg) {
|
|
|
|
if (connectArg == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (connectArg.username != null &&
|
|
|
|
connectArg.password != null) {
|
|
|
|
_onLegacyConnect(context, connectArg);
|
|
|
|
} else {
|
|
|
|
final uri = Uri.parse(
|
|
|
|
"${connectArg.scheme}://${connectArg.address}");
|
|
|
|
_onConnect(context, uri);
|
2024-10-16 19:51:49 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
_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(),
|
|
|
|
),
|
2022-11-22 14:46:28 +01:00
|
|
|
),
|
|
|
|
),
|
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-18 18:52:38 +02:00
|
|
|
|
|
|
|
Future<void> _onLegacyConnect(BuildContext context, _ConnectArg arg) async {
|
|
|
|
Account? account = Account(
|
|
|
|
id: Account.newId(),
|
|
|
|
scheme: arg.scheme,
|
|
|
|
address: arg.address,
|
|
|
|
userId: arg.username!.toCi(),
|
|
|
|
username2: arg.username!,
|
|
|
|
password: arg.password!,
|
|
|
|
roots: [""],
|
|
|
|
);
|
|
|
|
_log.info("[_onLegacyConnect] Try connecting with account: $account");
|
|
|
|
account = await Navigator.pushNamed<Account>(
|
|
|
|
context,
|
|
|
|
legacy.Connect.routeName,
|
|
|
|
arguments: legacy.ConnectArguments(account),
|
|
|
|
);
|
|
|
|
if (account == null) {
|
|
|
|
// connection failed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
account = await Navigator.pushNamed<Account>(
|
|
|
|
context,
|
|
|
|
RootPicker.routeName,
|
|
|
|
arguments: RootPickerArguments(account),
|
|
|
|
);
|
|
|
|
if (account == null) {
|
|
|
|
// ???
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// we've got a good account
|
|
|
|
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
|
|
|
}
|