Skip to content

armgit / ente

Public Imported from GitHub
0

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

7.0 KB
Raw Blame
import "dart:async";

import "package:ente_components/ente_components.dart";
import "package:ente_strings/ente_strings.dart";
import "package:flutter/material.dart";
import "package:logging/logging.dart";
import "package:photos/core/event_bus.dart";
import "package:photos/events/notification_event.dart";
import "package:photos/service_locator.dart";
import "package:photos/services/machine_learning/ml_service.dart";
import "package:photos/services/machine_learning/semantic_search/semantic_search_service.dart";
import "package:photos/ui/common/web_page.dart";
import "package:styled_text/styled_text.dart";

final _logger = Logger("MLConsentSheet");

Future<bool> showMLConsentSheet(BuildContext context) async {
  Future<void>? enabling;
  var enabled = false;
  var declined = false;
  await showBottomSheetComponent<void>(
    context: context,
    builder: (_) => _MLConsentSheet(
      onEnable: () => enabling = enableMLConsent().then((_) => enabled = true),
      onDecline: () => declined = true,
    ),
  );
  await enabling?.catchError((Object _) {});
  if (enabled) {
    return true;
  }
  if (declined) {
    await markMLConsentPromptSeen();
  }
  return false;
}

Future<void> enableMLConsent() async {
  await setMLConsent(true);
  memoriesCacheService.queueUpdateCache();
  Bus.instance.fire(NotificationEvent());
  try {
    await MLService.instance.init();
    await SemanticSearchService.instance.init();
    unawaited(MLService.instance.runAllML(force: true));
  } catch (e, s) {
    _logger.severe("Could not start machine learning after consent", e, s);
  }
}

Future<void> markMLConsentPromptSeen() async {
  if (hasGrantedMLConsent) {
    return;
  }
  await localSettings.setHasSeenMLEnablingBanner();
  Bus.instance.fire(NotificationEvent());
}

class _MLConsentSheet extends StatefulWidget {
  const _MLConsentSheet({required this.onEnable, required this.onDecline});

  final Future<void> Function() onEnable;
  final VoidCallback onDecline;

  @override
  State<_MLConsentSheet> createState() => _MLConsentSheetState();
}

class _MLConsentSheetState extends State<_MLConsentSheet> {
  bool _hasAcknowledged = false;
  bool _isEnabling = false;

  Future<void> _enable() async {
    setState(() => _isEnabling = true);
    try {
      await widget.onEnable();
    } finally {
      if (mounted) {
        setState(() => _isEnabling = false);
      }
    }
    if (mounted && (ModalRoute.of(context)?.isCurrent ?? false)) {
      Navigator.of(context).pop();
    }
  }

  @override
  Widget build(BuildContext context) {
    return BottomSheetComponent(
      title: context.strings.machineLearning,
      closeTooltip: context.strings.close,
      actionsTopSpacing: Spacing.xxl,
      content: Flexible(
        fit: FlexFit.loose,
        child: IgnorePointer(
          ignoring: _isEnabling,
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Center(
                  child: Image.asset(
                    "assets/ducky_ml.png",
                    height: 150,
                    fit: BoxFit.contain,
                  ),
                ),
                const SizedBox(height: 20),
                const MLConsentDescription(),
                const SizedBox(height: 20),
                MLConsentAcknowledgement(
                  selected: _hasAcknowledged,
                  onChanged: () {
                    setState(() => _hasAcknowledged = !_hasAcknowledged);
                  },
                ),
              ],
            ),
          ),
        ),
      ),
      actions: [
        ButtonComponent(
          label: context.strings.mlConsent,
          isDisabled: !_hasAcknowledged,
          onTap: _enable,
        ),
        MLConsentCancelLink(
          onTap: _isEnabling
              ? null
              : () {
                  widget.onDecline();
                  Navigator.of(context).pop();
                },
        ),
      ],
    );
  }
}

class MLConsentDescription extends StatelessWidget {
  const MLConsentDescription({super.key});

  @override
  Widget build(BuildContext context) {
    final textStyle = TextStyles.body.copyWith(
      color: context.componentColors.textLight,
    );

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        for (final paragraph in context.strings.mlConsentDescription.split(
          "\n\n",
        )) ...[Text(paragraph, style: textStyle), const SizedBox(height: 8)],
        StyledText(
          text: context.strings.mlConsentPrivacyDetails,
          style: textStyle,
          tags: {
            'policy': StyledTextActionTag(
              (String? text, Map<String?, String?> attrs) =>
                  _openPrivacyPolicy(context),
              style: textStyle.copyWith(
                decoration: TextDecoration.underline,
                decorationColor: textStyle.color,
              ),
            ),
          },
        ),
      ],
    );
  }

  Future<void> _openPrivacyPolicy(BuildContext context) async {
    await Navigator.of(context).push(
      MaterialPageRoute(
        builder: (BuildContext context) {
          return WebPage(
            context.strings.privacyPolicyTitle,
            "https://ente.com/privacy",
          );
        },
      ),
    );
  }
}

class MLConsentAcknowledgement extends StatelessWidget {
  const MLConsentAcknowledgement({
    super.key,
    required this.selected,
    required this.onChanged,
  });

  final bool selected;
  final VoidCallback onChanged;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: onChanged,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          CheckboxComponent(selected: selected, onChanged: (_) => onChanged()),
          const SizedBox(width: 10),
          Expanded(
            child: Text(
              context.strings.mlConsentConfirmation,
              style: TextStyles.body.copyWith(
                color: context.componentColors.textLight,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class MLConsentCancelLink extends StatelessWidget {
  const MLConsentCancelLink({super.key, required this.onTap});

  final VoidCallback? onTap;

  @override
  Widget build(BuildContext context) {
    final color = onTap == null
        ? context.componentColors.textLightest
        : context.componentColors.textBase;
    return Semantics(
      button: true,
      enabled: onTap != null,
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: onTap,
        child: Padding(
          padding: const EdgeInsets.symmetric(vertical: Spacing.lg),
          child: Center(
            child: Text(
              context.strings.cancel,
              style: TextStyles.bodyBold.copyWith(
                color: color,
                decoration: TextDecoration.underline,
                decorationColor: color,
              ),
            ),
          ),
        ),
      ),
    );
  }
}