Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

6.4 KB
Raw Blame
import 'package:dots_indicator/dots_indicator.dart';
import 'package:ente_accounts/ente_accounts.dart';
import 'package:ente_accounts/widgets/account_app_bar_logo.dart';
import 'package:ente_components/ente_components.dart';
import 'package:ente_strings/ente_strings.dart';
import 'package:ente_ui/components/buttons/dynamic_fab.dart';
import 'package:ente_ui/theme/ente_theme.dart';
import 'package:flutter/material.dart';

class OTTVerificationPage extends StatefulWidget {
  final String email;
  final bool isChangeEmail;
  final bool isCreateAccountScreen;
  final bool isResetPasswordScreen;

  const OTTVerificationPage(
    this.email, {
    this.isChangeEmail = false,
    this.isCreateAccountScreen = false,
    this.isResetPasswordScreen = false,
    super.key,
  });

  @override
  State<OTTVerificationPage> createState() => _OTTVerificationPageState();
}

class _OTTVerificationPageState extends State<OTTVerificationPage> {
  final _pinController = TextEditingController();
  bool _isSubmitting = false;

  Future<void> onPressed() async {
    if (_isSubmitting) {
      return;
    }
    setState(() {
      _isSubmitting = true;
    });
    try {
      if (widget.isChangeEmail) {
        await UserService.instance.changeEmail(
          context,
          widget.email,
          _pinController.text,
        );
      } else {
        await UserService.instance.verifyEmail(
          context,
          _pinController.text,
          isResettingPasswordScreen: widget.isResetPasswordScreen,
        );
      }
    } finally {
      if (mounted) {
        setState(() {
          _isSubmitting = false;
        });
      }
    }
    if (mounted) {
      FocusScope.of(context).unfocus();
    }
  }

  @override
  void dispose() {
    _pinController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final isKeypadOpen = MediaQuery.of(context).viewInsets.bottom > 100;
    final colorScheme = getEnteColorScheme(context);

    FloatingActionButtonLocation? fabLocation() {
      if (isKeypadOpen) {
        return null;
      } else {
        return FloatingActionButtonLocation.centerFloat;
      }
    }

    return Scaffold(
      resizeToAvoidBottomInset: isKeypadOpen,
      backgroundColor: colorScheme.backgroundBase,
      appBar: AppBar(
        elevation: 0,
        scrolledUnderElevation: 0,
        backgroundColor: colorScheme.backgroundBase,
        centerTitle: true,
        title: const AccountAppBarLogo(),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          color: getAccountAppBarColor(context),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
      ),
      body: _getBody(),
      floatingActionButton: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (widget.isCreateAccountScreen && !isKeypadOpen)
            Padding(
              padding: const EdgeInsets.only(bottom: 20),
              child: DotsIndicator(
                dotsCount: 3,
                position: 1,
                decorator: DotsDecorator(
                  activeColor: colorScheme.primary700,
                  color: colorScheme.primary700.withValues(alpha: 0.32),
                  activeShape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(14),
                  ),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(14),
                  ),
                  size: const Size(10, 10),
                  activeSize: const Size(20, 10),
                  spacing: const EdgeInsets.all(6),
                ),
              ),
            ),
          DynamicFAB(
            isKeypadOpen: isKeypadOpen,
            isFormValid: _pinController.text.length == 6 && !_isSubmitting,
            buttonText: context.strings.verify,
            onPressedFunction: onPressed,
          ),
        ],
      ),
      floatingActionButtonLocation: fabLocation(),
      floatingActionButtonAnimator: NoScalingAnimation(),
    );
  }

  Widget _getBody() {
    final colorScheme = getEnteColorScheme(context);
    final textTheme = getEnteTextTheme(context);

    return SafeArea(
      child: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              const SizedBox(height: 24),
              Image.asset('assets/upload_file.png'),
              const SizedBox(height: 24),
              Text(
                context.strings.weHaveSentCode(email: widget.email),
                style: textTheme.body.copyWith(color: colorScheme.textBase),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 8),
              Text(
                context.strings.checkInboxAndSpamFolder,
                style: textTheme.small.copyWith(color: colorScheme.textMuted),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 32),
              PinInputComponent(
                length: 6,
                controller: _pinController,
                autofocus: true,
                autofillHints: const [AutofillHints.oneTimeCode],
                onCompleted: (value) {
                  if (value.length == 6) {
                    onPressed();
                  }
                },
                onChanged: (_) {
                  setState(() {});
                },
              ),
              const SizedBox(height: 8),
              Align(
                alignment: Alignment.centerRight,
                child: TextButton(
                  onPressed: () {
                    UserService.instance.sendOtt(
                      context,
                      widget.email,
                      isCreateAccountScreen: widget.isCreateAccountScreen,
                      isChangeEmail: widget.isChangeEmail,
                      isResetPasswordScreen: widget.isResetPasswordScreen,
                    );
                  },
                  child: Text(
                    context.strings.resendCode,
                    style: textTheme.small.copyWith(
                      color: colorScheme.primary700,
                      decoration: TextDecoration.underline,
                      decorationColor: colorScheme.primary700,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}