ui: Move chat background into chat

This commit is contained in:
PapaTutuWawa 2022-09-07 00:29:30 +02:00
parent fdbf2534b7
commit 0d8bf8dd12
5 changed files with 81 additions and 122 deletions

View File

@ -40,7 +40,6 @@ import 'package:moxxyv2/ui/pages/profile/profile.dart';
import 'package:moxxyv2/ui/pages/sendfiles.dart';
import 'package:moxxyv2/ui/pages/server_info.dart';
import 'package:moxxyv2/ui/pages/settings/about.dart';
import 'package:moxxyv2/ui/pages/settings/appearance/appearance.dart';
import 'package:moxxyv2/ui/pages/settings/appearance/cropbackground.dart';
import 'package:moxxyv2/ui/pages/settings/conversation.dart';
import 'package:moxxyv2/ui/pages/settings/debugging.dart';
@ -305,7 +304,6 @@ class MyAppState extends State<MyApp> with WidgetsBindingObserver {
case settingsRoute: return SettingsPage.route;
case aboutRoute: return SettingsAboutPage.route;
case licensesRoute: return SettingsLicensesPage.route;
case appearanceRoute: return AppearancePage.route;
case networkRoute: return NetworkPage.route;
case privacyRoute: return PrivacyPage.route;
case debuggingRoute: return DebuggingPage.route;

View File

@ -51,7 +51,6 @@ const String aboutRoute = '$settingsRoute/about';
const String debuggingRoute = '$settingsRoute/debugging';
const String privacyRoute = '$settingsRoute/privacy';
const String networkRoute = '$settingsRoute/network';
const String appearanceRoute = '$settingsRoute/appearance';
const String backgroundCroppingRoute = '$settingsRoute/appearance/background';
const String conversationSettingsRoute = '$settingsRoute/conversation';
const String blocklistRoute = '/blocklist';

View File

@ -1,111 +0,0 @@
import 'dart:async';
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import 'package:moxxyv2/shared/preferences.dart';
import 'package:moxxyv2/ui/bloc/cropbackground_bloc.dart';
import 'package:moxxyv2/ui/bloc/preferences_bloc.dart';
import 'package:moxxyv2/ui/constants.dart';
import 'package:moxxyv2/ui/helpers.dart';
import 'package:moxxyv2/ui/service/thumbnail.dart';
import 'package:moxxyv2/ui/widgets/topbar.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:settings_ui/settings_ui.dart';
class AppearancePage extends StatelessWidget {
const AppearancePage({ Key? key }): super(key: key);
static MaterialPageRoute<dynamic> get route => MaterialPageRoute<dynamic>(
builder: (_) => const AppearancePage(),
settings: const RouteSettings(
name: appearanceRoute,
),
);
// TODO(Unknown): Move this somewhere else to not mix UI and application logic
Future<String?> _pickBackgroundImage() async {
final result = await FilePicker.platform.pickFiles(
type: FileType.image,
);
if (result == null) return null;
final appDir = await getApplicationDocumentsDirectory();
final backgroundPath = path.join(appDir.path, result.files.single.name);
await File(result.files.single.path!).copy(backgroundPath);
return backgroundPath;
}
Future<void> _removeBackgroundImage(BuildContext context, PreferencesState state) async {
final backgroundPath = state.backgroundPath;
if (backgroundPath.isEmpty) return;
// TODO(Unknown): Move this into the [PreferencesBloc]
final file = File(backgroundPath);
if (file.existsSync()) {
await file.delete();
}
// TODO(Unknown): END
// Remove from the cache
unawaited(GetIt.I.get<ThumbnailCacheService>().invalidateEntry(backgroundPath));
// ignore: use_build_context_synchronously
context.read<PreferencesBloc>().add(
PreferencesChangedEvent(
state.copyWith(backgroundPath: ''),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: BorderlessTopbar.simple('Appearance'),
body: BlocBuilder<PreferencesBloc, PreferencesState>(
builder: (context, state) => SettingsList(
sections: [
SettingsSection(
title: const Text('Conversation Background'),
tiles: [
SettingsTile(
title: const Text('Select background image'),
description: const Text('This image will be the background of all your chats'),
onPressed: (context) async {
final backgroundPath = await _pickBackgroundImage();
if (backgroundPath != null) {
// ignore: use_build_context_synchronously
context.read<CropBackgroundBloc>().add(
CropBackgroundRequestedEvent(backgroundPath),
);
}
},
),
SettingsTile(
title: const Text('Remove background image'),
onPressed: (context) {
showConfirmationDialog(
'Are you sure?',
'Are you sure you want to remove your conversation background image?',
context,
() async {
await _removeBackgroundImage(context, state);
// ignore: use_build_context_synchronously
Navigator.of(context).pop();
}
);
},
)
],
)
],
),
),
);
}
}

View File

@ -1,9 +1,16 @@
import 'dart:async';
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:moxxyv2/shared/preferences.dart';
import 'package:moxxyv2/ui/bloc/cropbackground_bloc.dart';
import 'package:moxxyv2/ui/bloc/preferences_bloc.dart';
import 'package:moxxyv2/ui/constants.dart';
import 'package:moxxyv2/ui/helpers.dart';
import 'package:moxxyv2/ui/widgets/topbar.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:settings_ui/settings_ui.dart';
class ConversationSettingsPage extends StatelessWidget {
@ -16,14 +23,85 @@ class ConversationSettingsPage extends StatelessWidget {
name: conversationSettingsRoute,
),
);
// TODO(Unknown): Move this somewhere else to not mix UI and application logic
Future<String?> _pickBackgroundImage() async {
final result = await FilePicker.platform.pickFiles(
type: FileType.image,
);
if (result == null) return null;
final appDir = await getApplicationDocumentsDirectory();
final backgroundPath = path.join(appDir.path, result.files.single.name);
await File(result.files.single.path!).copy(backgroundPath);
return backgroundPath;
}
Future<void> _removeBackgroundImage(BuildContext context, PreferencesState state) async {
final backgroundPath = state.backgroundPath;
if (backgroundPath.isEmpty) return;
// TODO(Unknown): Move this into the [PreferencesBloc]
final file = File(backgroundPath);
if (file.existsSync()) {
await file.delete();
}
// TODO(Unknown): END
// Remove from the cache
// TODO(PapaTutuWawa): Invalidate the cache
// ignore: use_build_context_synchronously
context.read<PreferencesBloc>().add(
PreferencesChangedEvent(
state.copyWith(backgroundPath: ''),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: BorderlessTopbar.simple('Conversation'),
appBar: BorderlessTopbar.simple('Chat'),
body: BlocBuilder<PreferencesBloc, PreferencesState>(
builder: (context, state) => SettingsList(
sections: [
SettingsSection(
title: const Text('Appearance'),
tiles: [
SettingsTile(
title: const Text('Select background image'),
description: const Text('This image will be the background of all your chats'),
onPressed: (context) async {
final backgroundPath = await _pickBackgroundImage();
if (backgroundPath != null) {
// ignore: use_build_context_synchronously
context.read<CropBackgroundBloc>().add(
CropBackgroundRequestedEvent(backgroundPath),
);
}
},
),
SettingsTile(
title: const Text('Remove background image'),
onPressed: (context) {
showConfirmationDialog(
'Are you sure?',
'Are you sure you want to remove your conversation background image?',
context,
() async {
await _removeBackgroundImage(context, state);
// ignore: use_build_context_synchronously
Navigator.of(context).pop();
}
);
},
),
],
),
SettingsSection(
title: const Text('New Conversations'),
tiles: [
@ -37,7 +115,7 @@ class ConversationSettingsPage extends StatelessWidget {
),
),
],
)
),
],
),
),

View File

@ -27,15 +27,10 @@ class SettingsPage extends StatelessWidget {
title: const Text('Conversations'),
tiles: [
SettingsTile(
title: const Text('Conversation'),
title: const Text('Chat'),
leading: const Icon(Icons.chat_bubble),
onPressed: (context) => Navigator.pushNamed(context, conversationSettingsRoute),
),
SettingsTile(
title: const Text('Appearance'),
leading: const Icon(Icons.brush),
onPressed: (context) => Navigator.pushNamed(context, appearanceRoute),
),
SettingsTile(
title: const Text('Network'),
leading: const Icon(Icons.network_wifi),