Skip to content

Commit

Permalink
Improve URL handling is post creator
Browse files Browse the repository at this point in the history
  • Loading branch information
micahmo committed Oct 8, 2023
1 parent 900fb5a commit 6a99a17
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 3 deletions.
29 changes: 27 additions & 2 deletions lib/community/pages/create_post_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class _CreatePostPageState extends State<CreatePostPage> {
bool imageUploading = false;
bool postImageUploading = false;
String url = "";
String? urlError;
DraftPost newDraftPost = DraftPost();

final TextEditingController _bodyTextController = TextEditingController();
Expand All @@ -58,13 +59,14 @@ class _CreatePostPageState extends State<CreatePostPage> {
super.initState();

_titleTextController.addListener(() {
if (_titleTextController.text.isEmpty && !isSubmitButtonDisabled) setState(() => isSubmitButtonDisabled = true);
if (_titleTextController.text.isNotEmpty && isSubmitButtonDisabled) setState(() => isSubmitButtonDisabled = false);
_validateSubmission();

widget.onUpdateDraft?.call(newDraftPost..title = _titleTextController.text);
});

_urlTextController.addListener(() {
_validateSubmission();

url = _urlTextController.text;
debounce(const Duration(milliseconds: 1000), _updatePreview, [url]);

Expand Down Expand Up @@ -194,6 +196,7 @@ class _CreatePostPageState extends State<CreatePostPage> {
controller: _urlTextController,
decoration: InputDecoration(
hintText: AppLocalizations.of(context)!.postURL,
errorText: urlError,
suffixIcon: IconButton(
onPressed: () {
if (!postImageUploading) {
Expand Down Expand Up @@ -343,6 +346,28 @@ class _CreatePostPageState extends State<CreatePostPage> {
setState(() {});
}
}

void _validateSubmission() {
final Uri? parsedUrl = Uri.tryParse(_urlTextController.text);

if (isSubmitButtonDisabled) {
// It's disabled, check if we can enable it.
if (_titleTextController.text.isNotEmpty && parsedUrl != null) {
setState(() {
isSubmitButtonDisabled = false;
urlError = null;
});
}
} else {
// It's enabled, check if we need to disable it.
if (_titleTextController.text.isEmpty || parsedUrl == null) {
setState(() {
isSubmitButtonDisabled = true;
urlError = parsedUrl == null ? AppLocalizations.of(context)!.notValidUrl : null;
});
}
}
}
}

class DraftPost {
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"changeSort": "Change Sort",
"postTitle": "Title",
"postURL": "URL",
"notValidUrl": "Not a valid URL",
"postBody": "Post Body",
"postTogglePreview": "Toggle Preview",
"postUploadImageError": "Error when uploading image",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"changeSort": "Cambiar orden",
"postTitle": "Título",
"postURL": "URL",
"notValidUrl": "Not a valid URL",
"postBody": "Cuerpo de la publicación",
"postTogglePreview": "Cambiar vista previa",
"postUploadImageError": "Error al subir imagen",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_fi.arb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"dismissRead": "Dismiss Read",
"postTitle": "Title",
"postURL": "URL",
"notValidUrl": "Not a valid URL",
"postBody": "Post Body",
"postTogglePreview": "Toggle Preview",
"postUploadImageError": "Error when uploading image",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_pl.arb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"changeSort": "Zmiana sortowania",
"postTitle": "Tytuł",
"postURL": "Link",
"notValidUrl": "Not a valid URL",
"postBody": "Treść",
"postTogglePreview": "Przełącz podgląd",
"postUploadImageError": "Wystąpił błąd podczas przesyłania obrazu",
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_sv.arb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"dismissRead": "Dismiss Read",
"postTitle": "Title",
"postURL": "URL",
"notValidUrl": "Not a valid URL",
"postBody": "Post Body",
"postTogglePreview": "Toggle Preview",
"postUploadImageError": "Error when uploading image",
Expand Down
7 changes: 6 additions & 1 deletion lib/utils/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ String generateRandomHeroString({int? len}) {
bool isImageUrl(String url) {
final imageExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.svg', '.webp'];

final uri = Uri.parse(url);
Uri uri;
try {
uri = Uri.parse(url);
} catch (e) {
return false;
}
final path = uri.path.toLowerCase();

for (final extension in imageExtensions) {
Expand Down

0 comments on commit 6a99a17

Please sign in to comment.