-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from papercups-io/lighten-sendMessage
Attchment Improvements & Better Maintainability
- Loading branch information
Showing
32 changed files
with
959 additions
and
668 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import 'package:universal_io/io.dart'; | ||
|
||
class PapercupsAttachment { | ||
String? id; | ||
String? fileName; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
lib/utils/getCustomerHistory.dart → ...ls/apiInteraction/getCustomerHistory.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:http/http.dart'; | ||
import 'package:open_file/open_file.dart'; | ||
import 'package:papercups_flutter/models/models.dart'; | ||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:universal_io/io.dart'; | ||
|
||
Future<void> handleDownloadStream(Stream<StreamedResponse> resp, | ||
{required File file, | ||
Function? onDownloading, | ||
Function? onDownloaded}) async { | ||
List<List<int>> chunks = []; | ||
|
||
if (onDownloading != null) { | ||
onDownloading(); | ||
} | ||
|
||
resp.listen((StreamedResponse r) { | ||
r.stream.listen((List<int> chunk) { | ||
if (r.contentLength == null) { | ||
print("Error"); | ||
} | ||
|
||
chunks.add(chunk); | ||
}, onDone: () async { | ||
final Uint8List bytes = Uint8List(r.contentLength ?? 0); | ||
int offset = 0; | ||
for (List<int> chunk in chunks) { | ||
bytes.setRange(offset, offset + chunk.length, chunk); | ||
offset += chunk.length; | ||
} | ||
await file.writeAsBytes(bytes); | ||
OpenFile.open(file.absolute.path); | ||
if (onDownloaded != null) { | ||
onDownloaded(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
Future<File> getAttachment(PapercupsAttachment attachment) async { | ||
String dir = (await getApplicationDocumentsDirectory()).path; | ||
File? file = File(dir + | ||
Platform.pathSeparator + | ||
(attachment.id ?? "noId") + | ||
(attachment.fileName ?? "noName")); | ||
return file; | ||
} | ||
|
||
Future<bool> checkCachedFiles(PapercupsAttachment attachment) async { | ||
var file = await getAttachment(attachment); | ||
if (await file.exists()) { | ||
return true; | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'package:file_picker/file_picker.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:papercups_flutter/models/models.dart'; | ||
import 'package:papercups_flutter/utils/fileInteraction/uploadFile.dart'; | ||
import 'package:papercups_flutter/widgets/alert.dart'; | ||
|
||
void nativeFilePicker( | ||
{required FileType type, | ||
required BuildContext context, | ||
required widget, | ||
required Function onUploadSuccess}) async { | ||
try { | ||
final _paths = (await FilePicker.platform.pickFiles( | ||
type: type, | ||
)) | ||
?.files; | ||
if (_paths != null && _paths.first.path != null) { | ||
Alert.show( | ||
"Uploading...", | ||
context, | ||
textStyle: Theme.of(context).textTheme.bodyText2, | ||
backgroundColor: Theme.of(context).bottomAppBarColor, | ||
gravity: Alert.bottom, | ||
duration: Alert.lengthLong, | ||
); | ||
List<PapercupsAttachment> attachments = await uploadFile( | ||
widget.props, | ||
filePath: _paths.first.path, | ||
onUploadProgress: (sentBytes, totalBytes) { | ||
Alert.show( | ||
"${(sentBytes * 100 / totalBytes).toStringAsFixed(2)}% uploaded", | ||
context, | ||
textStyle: Theme.of(context).textTheme.bodyText2, | ||
backgroundColor: Theme.of(context).bottomAppBarColor, | ||
gravity: Alert.bottom, | ||
duration: Alert.lengthLong, | ||
); | ||
}, | ||
); | ||
|
||
onUploadSuccess(attachments); | ||
} | ||
} on PlatformException catch (_) { | ||
Alert.show( | ||
"Failed to upload attachment", | ||
context, | ||
textStyle: Theme.of(context).textTheme.bodyText2, | ||
backgroundColor: Theme.of(context).bottomAppBarColor, | ||
gravity: Alert.bottom, | ||
duration: Alert.lengthLong, | ||
); | ||
throw _; | ||
} catch (_) { | ||
Alert.show( | ||
"Failed to upload attachment", | ||
context, | ||
textStyle: Theme.of(context).textTheme.bodyText2, | ||
backgroundColor: Theme.of(context).bottomAppBarColor, | ||
gravity: Alert.bottom, | ||
duration: Alert.lengthLong, | ||
); | ||
throw _; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import 'package:file_picker/file_picker.dart'; | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:papercups_flutter/models/attachment.dart'; | ||
import 'package:papercups_flutter/utils/fileInteraction/uploadFile.dart'; | ||
import 'package:papercups_flutter/widgets/alert.dart'; | ||
|
||
Future<void> webFilePicker( | ||
{required BuildContext context, | ||
required Function onUploadSuccess, | ||
required widget}) async { | ||
try { | ||
var picked = await FilePicker.platform.pickFiles(); | ||
|
||
if (picked != null && picked.files.first.bytes != null) { | ||
Alert.show( | ||
"Uploading...", | ||
context, | ||
textStyle: Theme.of(context).textTheme.bodyText2, | ||
backgroundColor: Theme.of(context).bottomAppBarColor, | ||
gravity: Alert.bottom, | ||
duration: Alert.lengthLong, | ||
); | ||
List<PapercupsAttachment> attachments = await uploadFile( | ||
widget.props, | ||
fileBytes: picked.files.first.bytes, | ||
fileName: picked.files.first.name, | ||
); | ||
onUploadSuccess(attachments); | ||
} | ||
} on Exception catch (_) { | ||
Alert.show( | ||
"Failed to upload attachment", | ||
context, | ||
textStyle: Theme.of(context).textTheme.bodyText2, | ||
backgroundColor: Theme.of(context).bottomAppBarColor, | ||
gravity: Alert.bottom, | ||
duration: Alert.lengthLong, | ||
); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
lib/utils/intitChannels.dart → lib/utils/socket/intitChannels.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.