Skip to content

Commit

Permalink
Posts and images sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
alim-zanibekov committed Mar 3, 2022
1 parent 34327e5 commit f7dbf71
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 123 deletions.
178 changes: 90 additions & 88 deletions lib/app/auth/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,106 +26,108 @@ class _AppAuthPageState extends State<AppAuthPage> {
return Scaffold(
appBar: AppBar(title: const Text('Аворизация')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(36.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 45.0,
child: Align(
alignment: Alignment.centerLeft,
child: AnimatedOpacity(
duration: const Duration(milliseconds: 150),
opacity: _error ? 1 : 0,
child: Text(
_errorMessage ?? 'Неверное имя пользователя или пароль',
style: style.copyWith(
color: Theme.of(context).errorColor,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(36.0),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 45.0,
child: Align(
alignment: Alignment.centerLeft,
child: AnimatedOpacity(
duration: const Duration(milliseconds: 150),
opacity: _error ? 1 : 0,
child: Text(
_errorMessage ?? 'Неверное имя пользователя или пароль',
style: style.copyWith(
color: Theme.of(context).errorColor,
),
),
),
),
),
),
TextFormField(
controller: _usernameEditingController,
obscureText: false,
validator: (value) => (value?.isEmpty ?? true)
? 'Введите имя пользователя'
: null,
decoration: const InputDecoration(
contentPadding: EdgeInsets.fromLTRB(10.0, 15.0, 10.0, 15.0),
hintText: 'Имя пользователя',
TextFormField(
controller: _usernameEditingController,
obscureText: false,
validator: (value) => (value?.isEmpty ?? true)
? 'Введите имя пользователя'
: null,
decoration: const InputDecoration(
contentPadding: EdgeInsets.fromLTRB(10.0, 15.0, 10.0, 15.0),
hintText: 'Имя пользователя',
),
),
),
const SizedBox(height: 25.0),
TextFormField(
controller: _passwordEditingController,
obscureText: true,
validator: (value) =>
(value?.isEmpty ?? true) ? 'Введите пароль' : null,
decoration: const InputDecoration(
contentPadding: EdgeInsets.fromLTRB(10.0, 15.0, 10.0, 15.0),
hintText: 'Пароль',
const SizedBox(height: 25.0),
TextFormField(
controller: _passwordEditingController,
obscureText: true,
validator: (value) =>
(value?.isEmpty ?? true) ? 'Введите пароль' : null,
decoration: const InputDecoration(
contentPadding: EdgeInsets.fromLTRB(10.0, 15.0, 10.0, 15.0),
hintText: 'Пароль',
),
),
),
const SizedBox(height: 35.0),
Container(
width: double.infinity,
child: OutlinedButton(
onPressed: () async {
if (_formKey.currentState?.validate() ?? false) {
FocusScope.of(context).requestFocus(FocusNode());
setState(() => _loading = true);
try {
await Auth().login(
_usernameEditingController.value.text,
_passwordEditingController.value.text);
_loading = false;
AppPages.appBottomBarPage.add(AppBottomBarPage.MAIN);
} catch (err) {
if (err is InvalidUsernameOrPasswordException) {
_errorMessage =
'Неверное имя пользователя или пароль';
} else if (err is RateLimitException) {
_errorMessage = 'Превышен лимит обращений';
} else if (err is InvalidStatusCodeException) {
_errorMessage =
'Что то пошло не так, повторите позже';
}
setState(() {
_error = true;
const SizedBox(height: 35.0),
Container(
width: double.infinity,
child: OutlinedButton(
onPressed: () async {
if (_formKey.currentState?.validate() ?? false) {
FocusScope.of(context).requestFocus(FocusNode());
setState(() => _loading = true);
try {
await Auth().login(
_usernameEditingController.value.text,
_passwordEditingController.value.text);
_loading = false;
});
AppPages.appBottomBarPage.add(AppBottomBarPage.MAIN);
} catch (err) {
if (err is InvalidUsernameOrPasswordException) {
_errorMessage =
'Неверное имя пользователя или пароль';
} else if (err is RateLimitException) {
_errorMessage = 'Превышен лимит обращений';
} else if (err is InvalidStatusCodeException) {
_errorMessage =
'Что то пошло не так, повторите позже';
}
setState(() {
_error = true;
_loading = false;
});
}
}
}
},
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 150),
child: IndexedStack(
key: ValueKey<int>(_loading ? 1 : 0),
index: _loading ? 1 : 0,
children: <Widget>[
Center(child: Text('Войти', style: style)),
const Center(
child: SizedBox(
child: CircularProgressIndicator(
strokeWidth: 1.5,
},
child: AnimatedSwitcher(
duration: const Duration(milliseconds: 150),
child: IndexedStack(
key: ValueKey<int>(_loading ? 1 : 0),
index: _loading ? 1 : 0,
children: <Widget>[
Center(child: Text('Войти', style: style)),
const Center(
child: SizedBox(
child: CircularProgressIndicator(
strokeWidth: 1.5,
),
height: 16,
width: 16,
),
height: 16,
width: 16,
),
)
],
)
],
),
),
),
),
),
const SizedBox(height: 15.0),
],
const SizedBox(height: 15.0),
],
),
),
),
),
Expand Down
25 changes: 23 additions & 2 deletions lib/app/content/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';

import '../../core/common/clipboard.dart';
import '../../core/common/menu.dart';
Expand Down Expand Up @@ -100,7 +101,8 @@ class AppContentLoader {
}
}

_filteredFutures = _futures.where((it) => it != null).map((it) => it!).toList();
_filteredFutures =
_futures.where((it) => it != null).map((it) => it!).toList();

if (_undefinedSizeImages.isEmpty) {
if (_filteredFutures.isNotEmpty) {
Expand Down Expand Up @@ -341,8 +343,26 @@ class _AppContentState extends State<AppContent> {
onSelect: () {
SaveFile.downloadAndSave(
context, image.prettyImageLink ?? image.value);
}),
})
]);
final checkMenu = () async {
final exist = await widget.loader.images[index].existInCache();
if (!exist || menu.items.any((it) => it.text == "Поделиться")) {
return;
}

menu.addItem(MenuItem(
text: "Поделиться",
onSelect: () async {
final image = widget.loader.images[index];
final file = await image.loadFromDiskCache();
if (file != null) {
Share.shareFiles([file.path]);
}
},
));
};
checkMenu();
return GestureDetector(
onLongPress: () {
menu.openUnderTap(pos);
Expand All @@ -360,6 +380,7 @@ class _AppContentState extends State<AppContent> {
child: AppSafeImage(
imageProvider: widget.loader.images[index],
onInfo: (e) {
checkMenu();
if (widget.loader.onImageInfo(e, image)) {
if (mounted) setState(() {});
}
Expand Down
26 changes: 20 additions & 6 deletions lib/app/image-gallery/image-gallery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:share_plus/share_plus.dart';
import 'package:vector_math/vector_math_64.dart' as vector;

import '../../core/common/menu.dart';
Expand Down Expand Up @@ -303,17 +304,30 @@ class _ImageGalleryState extends State<ImageGallery>
SnackBarHelper.show(context, 'Дождитесь загрузки изображения');
return;
}
final rawBytes =
await (_activeImage.imageProvider as AppNetworkImageWithRetry)
.loadFromDiskCache();
final url =
(_activeImage.imageProvider as AppNetworkImageWithRetry).url;
final image =
_activeImage.imageProvider as AppNetworkImageWithRetry;
final rawBytes = await image.loadContentsFromDiskCache();
final url = image.url;
if (rawBytes != null) {
await SaveFile.save(context, url, rawBytes);
} else {
SnackBarHelper.show(context, 'Не удалось загрузить изображение');
}
})
}),
MenuItem(
text: "Поделиться",
onSelect: () async {
if (_activeImage.info == null) {
SnackBarHelper.show(context, 'Дождитесь загрузки изображения');
return;
}
final image = _activeImage.imageProvider as AppNetworkImageWithRetry;
final file = await image.loadFromDiskCache();
if (file != null) {
Share.shareFiles([file.path]);
}
},
)
]);

return PopupMenuButton<int>(
Expand Down
Loading

0 comments on commit f7dbf71

Please sign in to comment.