refactor: Magic numbers

This commit is contained in:
Krille Fear 2021-10-26 18:47:05 +02:00
parent 56646cb697
commit fcdf5a7ee4
8 changed files with 63 additions and 54 deletions

View File

@ -99,9 +99,10 @@ class _FluffyChatAppState extends State<FluffyChatApp> {
initial: AdaptiveThemeMode.system,
builder: (theme, darkTheme) => LayoutBuilder(
builder: (context, constraints) {
const maxColumns = 3;
var newColumns =
(constraints.maxWidth / FluffyThemes.columnWidth).floor();
if (newColumns > 3) newColumns = 3;
if (newColumns > maxColumns) newColumns = maxColumns;
columnMode ??= newColumns > 1;
_router ??= GlobalKey<VRouterState>();
if (columnMode != newColumns > 1) {

View File

@ -552,50 +552,50 @@ class ChatController extends State<Chat> {
if (eventIndex == -1) {
// event id not found...maybe we can fetch it?
// the try...finally is here to start and close the loading dialog reliably
final task = Future.microtask(() async {
// okay, we first have to fetch if the event is in the room
try {
final event = await timeline.getEventById(eventId);
if (event == null) {
// event is null...meaning something is off
return;
}
} catch (err) {
if (err is MatrixException && err.errcode == 'M_NOT_FOUND') {
// event wasn't found, as the server gave a 404 or something
return;
}
rethrow;
}
// okay, we know that the event *is* in the room
while (eventIndex == -1) {
if (!canLoadMore) {
// we can't load any more events but still haven't found ours yet...better stop here
return;
}
try {
await timeline.requestHistory(historyCount: _loadHistoryCount);
} catch (err) {
if (err is TimeoutException) {
// loading the history timed out...so let's do nothing
return;
await showFutureLoadingDialog(
context: context,
future: () async {
// okay, we first have to fetch if the event is in the room
try {
final event = await timeline.getEventById(eventId);
if (event == null) {
// event is null...meaning something is off
return;
}
} catch (err) {
if (err is MatrixException && err.errcode == 'M_NOT_FOUND') {
// event wasn't found, as the server gave a 404 or something
return;
}
rethrow;
}
rethrow;
}
eventIndex = filteredEvents.indexWhere((e) => e.eventId == eventId);
}
});
if (context != null) {
await showFutureLoadingDialog(context: context, future: () => task);
} else {
await task;
}
// okay, we know that the event *is* in the room
while (eventIndex == -1) {
if (!canLoadMore) {
// we can't load any more events but still haven't found ours yet...better stop here
return;
}
try {
await timeline.requestHistory(historyCount: _loadHistoryCount);
} catch (err) {
if (err is TimeoutException) {
// loading the history timed out...so let's do nothing
return;
}
rethrow;
}
eventIndex =
filteredEvents.indexWhere((e) => e.eventId == eventId);
}
});
}
if (!mounted) {
return;
}
await scrollController.scrollToIndex(eventIndex,
preferPosition: AutoScrollPosition.middle);
await scrollController.scrollToIndex(
eventIndex,
preferPosition: AutoScrollPosition.middle,
);
_updateScrollController();
}

View File

@ -294,11 +294,13 @@ class ChatDetailsController extends State<ChatDetails> {
}
}
static const fixedWidth = 360.0;
@override
Widget build(BuildContext context) {
members ??= Matrix.of(context).client.getRoomById(roomId).getParticipants();
return SizedBox(
width: 360.0,
width: fixedWidth,
child: ChatDetailsView(this),
);
}

View File

@ -27,11 +27,13 @@ class ImageViewerController extends State<ImageViewer> {
/// Save this file with a system call.
void saveFileAction() => widget.event.saveFile(context);
static const maxScaleFactor = 1.5;
/// Go back if user swiped it away
void onInteractionEnds(ScaleEndDetails endDetails) {
if (PlatformInfos.usesTouchscreen == false) {
if (endDetails.velocity.pixelsPerSecond.dy >
MediaQuery.of(context).size.height * 1.50) {
MediaQuery.of(context).size.height * maxScaleFactor) {
Navigator.of(context, rootNavigator: false).pop();
}
}

View File

@ -155,10 +155,7 @@ class _KeyVerificationPageState extends State<KeyVerificationDialog> {
);
buttons.add(AdaptiveFlatButton(
label: L10n.of(context).submit,
onPressed: () {
input = textEditingController.text;
checkInput(input);
},
onPressed: () => checkInput(textEditingController.text),
));
buttons.add(AdaptiveFlatButton(
label: L10n.of(context).skip,
@ -207,11 +204,9 @@ class _KeyVerificationPageState extends State<KeyVerificationDialog> {
buttons.add(AdaptiveFlatButton(
label: L10n.of(context).reject,
textColor: Colors.red,
onPressed: () {
widget.request.rejectVerification().then((_) {
Navigator.of(context, rootNavigator: false).pop();
});
},
onPressed: () => widget.request
.rejectVerification()
.then((_) => Navigator.of(context, rootNavigator: false).pop()),
));
buttons.add(AdaptiveFlatButton(
label: L10n.of(context).accept,

View File

@ -104,6 +104,8 @@ class SearchController extends State<Search> {
String currentSearchTerm;
List<Profile> foundProfiles = [];
static const searchUserDirectoryLimit = 10;
void searchUser(BuildContext context, String text) async {
if (text.isEmpty) {
setState(() {
@ -115,7 +117,10 @@ class SearchController extends State<Search> {
final matrix = Matrix.of(context);
SearchUserDirectoryResponse response;
try {
response = await matrix.client.searchUserDirectory(text, limit: 10);
response = await matrix.client.searchUserDirectory(
text,
limit: searchUserDirectoryLimit,
);
} catch (_) {}
foundProfiles = List<Profile>.from(response?.results ?? []);
if (foundProfiles.isEmpty && text.isValidMatrixId && text.sigil == '@') {

View File

@ -24,11 +24,13 @@ class SendFileDialog extends StatefulWidget {
class _SendFileDialogState extends State<SendFileDialog> {
bool origImage = false;
bool _isSending = false;
static const maxWidth = 1600;
Future<void> _send() async {
var file = widget.file;
if (file is MatrixImageFile && !origImage) {
try {
file = await resizeImage(file, max: 1600);
file = await resizeImage(file, max: maxWidth);
} catch (e) {
// couldn't resize
}

View File

@ -198,6 +198,8 @@ class EmotesSettingsController extends State<EmotesSettings> {
});
}
static const maxImageWidth = 1600;
void imagePickerAction(
ValueNotifier<ImagePackImageContent> controller) async {
final result =
@ -208,7 +210,7 @@ class EmotesSettingsController extends State<EmotesSettings> {
name: result.fileName,
);
try {
file = await resizeImage(file, max: 1600);
file = await resizeImage(file, max: maxImageWidth);
} catch (_) {
// do nothing
}