2021-02-02 17:17:48 +00:00
|
|
|
import 'package:clipboard/clipboard.dart';
|
2021-02-02 14:33:23 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_translate/flutter_translate.dart';
|
2021-02-02 17:17:48 +00:00
|
|
|
import 'package:provider/provider.dart';
|
2021-02-02 14:33:23 +00:00
|
|
|
|
|
|
|
import '../../core/enums/viewstate.dart';
|
2021-02-02 17:17:48 +00:00
|
|
|
import '../../core/models/session.dart';
|
2021-02-02 14:33:23 +00:00
|
|
|
import '../../core/viewmodels/upload_model.dart';
|
|
|
|
import '../shared/app_colors.dart';
|
|
|
|
import '../widgets/centered_error_row.dart';
|
|
|
|
import '../widgets/my_appbar.dart';
|
|
|
|
import 'base_view.dart';
|
|
|
|
|
|
|
|
class UploadView extends StatelessWidget {
|
|
|
|
static const routeName = '/upload';
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return BaseView<UploadModel>(
|
2021-02-03 00:57:42 +00:00
|
|
|
onModelReady: (model) => model.init(),
|
2022-12-16 13:40:55 +00:00
|
|
|
builder: (context, model, child) =>
|
|
|
|
Scaffold(appBar: MyAppBar(title: Text(translate('titles.upload'))), body: _render(model, context)));
|
2021-04-06 12:00:00 +00:00
|
|
|
}
|
2021-02-02 17:17:48 +00:00
|
|
|
|
2021-04-06 12:00:00 +00:00
|
|
|
bool _isUploadButtonEnabled(UploadModel model) {
|
2022-06-22 22:44:08 +00:00
|
|
|
return model.pasteTextTouched || (model.paths != null && model.paths!.length > 0);
|
2021-04-06 12:00:00 +00:00
|
|
|
}
|
2021-02-02 17:17:48 +00:00
|
|
|
|
2021-04-06 12:00:00 +00:00
|
|
|
Widget _render(UploadModel model, BuildContext context) {
|
|
|
|
var url = Provider.of<Session>(context).url;
|
2021-02-02 14:33:23 +00:00
|
|
|
|
2021-04-06 12:00:00 +00:00
|
|
|
return model.state == ViewState.Busy
|
|
|
|
? Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
CircularProgressIndicator(),
|
2022-06-22 22:44:08 +00:00
|
|
|
(model.stateMessage != null && model.stateMessage!.isNotEmpty ? Text(model.stateMessage!) : Container())
|
2021-04-06 12:00:00 +00:00
|
|
|
]))
|
|
|
|
: ListView(children: <Widget>[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(left: 25.0, right: 25.0),
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 10.0, bottom: 10.0),
|
|
|
|
child: TextFormField(
|
|
|
|
minLines: 1,
|
|
|
|
maxLines: 7,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: Icon(
|
|
|
|
Icons.text_snippet,
|
|
|
|
),
|
|
|
|
suffixIcon: IconButton(
|
2022-06-22 22:44:08 +00:00
|
|
|
onPressed: () => model.pasteTextController.clear(),
|
2021-04-06 12:00:00 +00:00
|
|
|
icon: Icon(Icons.clear),
|
|
|
|
),
|
|
|
|
hintText: translate('upload.text_to_be_pasted'),
|
2022-06-22 22:44:08 +00:00
|
|
|
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
|
|
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
|
2021-02-02 14:33:23 +00:00
|
|
|
),
|
2021-04-06 12:00:00 +00:00
|
|
|
controller: model.pasteTextController)),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 10.0, bottom: 10.0),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
|
|
children: [Text(translate('upload.and_or'))])),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 10.0, bottom: 10.0),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
|
|
children: [
|
|
|
|
ElevatedButton.icon(
|
2022-06-22 22:44:08 +00:00
|
|
|
icon: Icon(Icons.file_copy_sharp, color: blueColor),
|
2021-04-06 12:00:00 +00:00
|
|
|
onPressed: () => model.openFileExplorer(),
|
|
|
|
label: Text(
|
|
|
|
translate('upload.open_file_explorer'),
|
|
|
|
)),
|
|
|
|
ElevatedButton.icon(
|
|
|
|
icon: Icon(Icons.cancel, color: orangeColor),
|
2022-06-22 22:44:08 +00:00
|
|
|
onPressed: model.paths != null && model.paths!.length > 0
|
2021-04-06 12:00:00 +00:00
|
|
|
? () => model.clearCachedFiles()
|
|
|
|
: null,
|
|
|
|
label: Text(
|
|
|
|
translate('upload.clear_temporary_files'),
|
|
|
|
)),
|
2021-02-02 14:33:23 +00:00
|
|
|
],
|
2021-04-06 12:00:00 +00:00
|
|
|
)),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 10.0, bottom: 10.0),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Checkbox(
|
|
|
|
value: model.createMulti,
|
|
|
|
onChanged: (v) => model.toggleCreateMulti(),
|
|
|
|
),
|
|
|
|
Text(translate('upload.multipaste')),
|
|
|
|
])),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 10.0, bottom: 10.0),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
|
|
children: [
|
|
|
|
ElevatedButton.icon(
|
|
|
|
onPressed: !_isUploadButtonEnabled(model)
|
|
|
|
? null
|
|
|
|
: () async {
|
2022-06-22 22:44:08 +00:00
|
|
|
Map<String, bool>? items = await model.upload();
|
|
|
|
String? clipboardContent = model.generatePasteLinks(items, url);
|
2021-04-06 12:00:00 +00:00
|
|
|
|
2022-06-22 22:44:08 +00:00
|
|
|
if (clipboardContent != null && clipboardContent.isNotEmpty) {
|
|
|
|
FlutterClipboard.copy(clipboardContent).then((value) {
|
2021-04-06 12:00:00 +00:00
|
|
|
final snackBar = SnackBar(
|
|
|
|
action: SnackBarAction(
|
2022-06-22 22:44:08 +00:00
|
|
|
label: translate('upload.dismiss'),
|
2021-04-06 12:00:00 +00:00
|
|
|
textColor: blueColor,
|
|
|
|
onPressed: () {
|
2022-06-22 22:44:08 +00:00
|
|
|
ScaffoldMessenger.of(context).hideCurrentSnackBar();
|
2021-04-06 12:00:00 +00:00
|
|
|
},
|
|
|
|
),
|
2022-06-22 22:44:08 +00:00
|
|
|
content: Text(translate('upload.uploaded')),
|
2021-04-06 12:00:00 +00:00
|
|
|
duration: Duration(seconds: 10),
|
|
|
|
);
|
2022-06-22 22:44:08 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
2021-04-06 12:00:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2022-06-22 22:44:08 +00:00
|
|
|
icon: Icon(Icons.upload_rounded, color: greenColor),
|
2021-04-06 12:00:00 +00:00
|
|
|
label: Text(
|
|
|
|
translate('upload.upload'),
|
|
|
|
)),
|
|
|
|
])),
|
2021-11-29 23:44:22 +00:00
|
|
|
model.errorMessage != null && model.errorMessage!.isNotEmpty
|
2021-04-06 12:00:00 +00:00
|
|
|
? (Padding(
|
2022-06-22 22:44:08 +00:00
|
|
|
padding: const EdgeInsets.only(top: 10.0, bottom: 10.0),
|
2021-04-06 12:00:00 +00:00
|
|
|
child: CenteredErrorRow(model.errorMessage)))
|
|
|
|
: Container(),
|
|
|
|
Builder(
|
|
|
|
builder: (BuildContext context) => model.loadingPath
|
|
|
|
? Padding(
|
|
|
|
padding: const EdgeInsets.only(bottom: 10.0),
|
|
|
|
child: const CircularProgressIndicator(),
|
|
|
|
)
|
|
|
|
: model.paths != null
|
|
|
|
? Container(
|
|
|
|
padding: const EdgeInsets.only(bottom: 30.0),
|
2022-06-22 22:44:08 +00:00
|
|
|
height: MediaQuery.of(context).size.height * 0.50,
|
2021-04-06 12:00:00 +00:00
|
|
|
child: ListView.separated(
|
2022-06-22 22:44:08 +00:00
|
|
|
itemCount: model.paths != null && model.paths!.isNotEmpty ? model.paths!.length : 1,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
final bool isMultiPath = model.paths != null && model.paths!.isNotEmpty;
|
2021-04-06 12:00:00 +00:00
|
|
|
final String name = (isMultiPath
|
2022-06-22 22:44:08 +00:00
|
|
|
? model.paths!.map((e) => e.name).toList()[index]
|
2021-04-06 12:00:00 +00:00
|
|
|
: model.fileName ?? '...');
|
2021-11-29 23:44:22 +00:00
|
|
|
final path = model.paths!.length > 0
|
2022-06-22 22:44:08 +00:00
|
|
|
? model.paths!.map((e) => e.path).toList()[index].toString()
|
2021-04-06 12:00:00 +00:00
|
|
|
: '';
|
|
|
|
|
|
|
|
return Card(
|
|
|
|
child: ListTile(
|
|
|
|
title: Text(
|
|
|
|
name,
|
|
|
|
),
|
|
|
|
subtitle: Text(path),
|
|
|
|
));
|
|
|
|
},
|
2022-06-22 22:44:08 +00:00
|
|
|
separatorBuilder: (BuildContext context, int index) => const Divider(),
|
2021-04-06 12:00:00 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
: Container(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
))
|
|
|
|
]);
|
2021-02-02 14:33:23 +00:00
|
|
|
}
|
|
|
|
}
|