Skip to content

Commit 9157a66

Browse files
webongushahidlee
andauthored
Fix: Submitting post via mobile app (#829)
* Gives each date|time widget a unique id so they can be dealt with separately. (#792) * fix(USH-1128): rewrite mobile image uploader component --------- Co-authored-by: ushahidlee <[email protected]>
1 parent f7fc2c4 commit 9157a66

File tree

5 files changed

+68
-57
lines changed

5 files changed

+68
-57
lines changed

apps/mobile-mzima-client/android/app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
</application>
3939

4040
<!-- Permissions -->
41-
4241
<uses-permission android:name="android.permission.INTERNET" />
4342
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
4443

apps/mobile-mzima-client/src/app/post/components/image-uploader/image-uploader.component.ts

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class ImageUploaderComponent implements ControlValueAccessor {
4444

4545
writeValue(obj: any): void {
4646
if (obj) {
47+
console.log('writeValue > obj', obj);
4748
this.upload = false;
4849
this.captionControl.patchValue(obj.caption);
4950
this.id = obj.id;
@@ -70,55 +71,81 @@ export class ImageUploaderComponent implements ControlValueAccessor {
7071
try {
7172
if (Capacitor.getPlatform() != 'web') await Camera.requestPermissions();
7273
const options = {
73-
quality: 90,
74+
quality: 100,
7475
allowEditing: false,
7576
source: CameraSource.Prompt,
7677
width: 600,
7778
resultType: CameraResultType.Uri,
7879
};
7980
const image = await Camera.getPhoto(options);
81+
// Check if the storage folder exists or can be read
8082
const folderExist = await this.checkFolder();
8183
if (folderExist) {
8284
if (image) await this.saveImage(image);
8385
}
86+
this.transferData({ upload: this.upload });
8487
} catch (e) {
8588
console.log('takePicture error: ', e);
8689
}
8790
}
8891

89-
async checkFolder(): Promise<boolean> {
90-
const options = {
91-
directory: Directory.Data,
92-
path: IMAGE_DIR,
93-
};
94-
try {
95-
const result = await Filesystem.readdir(options);
96-
return !!result.files;
97-
} catch (e) {
98-
await Filesystem.mkdir(options);
99-
return true;
100-
}
101-
}
102-
10392
/**
10493
* Save image to storage
10594
*/
10695
async saveImage(photo: Photo) {
10796
const base64Data = await new ConvertImage().readAsBase64(photo);
10897
this.fileName = new Date().getTime() + '.jpeg';
98+
const filePath = `${IMAGE_DIR}/${this.fileName}`;
10999
try {
110-
await Filesystem.writeFile({
100+
const savedFile = await Filesystem.writeFile({
111101
directory: Directory.Data,
112-
path: `${IMAGE_DIR}/${this.fileName}`,
102+
path: filePath,
113103
data: base64Data,
114104
});
115-
this.loadFiles();
105+
// const file = await this.loadFile();
106+
107+
if (Capacitor.getPlatform() === 'hybrid') {
108+
// Display the new image by rewriting the 'file://' path to HTTP
109+
// Details: https://ionicframework.com/docs/building/webview#file-protocol
110+
this.photo = {
111+
name: this.fileName,
112+
path: savedFile.uri,
113+
data: Capacitor.convertFileSrc(savedFile.uri),
114+
};
115+
} else {
116+
// Use webPath to display the new image instead of base64 since it's
117+
// already loaded into memory
118+
this.photo = {
119+
name: this.fileName,
120+
path: filePath,
121+
data: photo.webPath!,
122+
// data: `data:image/jpeg;base64,${file.data}`,
123+
};
124+
}
125+
126+
this.upload = true;
127+
this.preview = null;
128+
} catch (e) {
129+
console.log(e);
130+
}
131+
}
132+
133+
async deleteSelectedImage() {
134+
try {
135+
await Filesystem.deleteFile({
136+
directory: Directory.Data,
137+
path: this.photo!.path,
138+
});
139+
this.photo = null;
140+
this.upload = false;
141+
this.preview = null;
142+
this.transferData({ delete: true });
116143
} catch (e) {
117144
console.log(e);
118145
}
119146
}
120147

121-
async loadFiles() {
148+
async loadFile() {
122149
const options = {
123150
directory: Directory.Data,
124151
path: IMAGE_DIR,
@@ -127,60 +154,43 @@ export class ImageUploaderComponent implements ControlValueAccessor {
127154
try {
128155
if (Capacitor.getPlatform() != 'web') await Filesystem.requestPermissions();
129156
const result = await Filesystem.readdir(options);
130-
this.loadFileData(result.files);
157+
return await this.loadFileData(result.files);
131158
} catch (e) {
132159
console.log('readdir', e);
133160
await Filesystem.mkdir(options);
161+
return false;
134162
}
135163
}
136164

137165
async loadFileData(files: FileInfo[]) {
138166
const file = files.find((el) => el.name === this.fileName)!;
139167
const filePath = `${IMAGE_DIR}/${file.name}`;
140-
const readFile = await Filesystem.readFile({
168+
const result = await Filesystem.readFile({
141169
directory: Directory.Data,
142170
path: filePath,
143171
});
144172

145-
this.upload = true;
146-
this.preview = null;
147-
this.photo = {
148-
name: file.name,
149-
path: filePath,
150-
data: `data:image/jpeg;base64,${readFile.data}`,
151-
};
152-
153-
console.log('loadFileData', this.photo);
154-
155-
this.transferData({ upload: this.upload });
173+
return result;
156174
}
157175

158-
async deleteSelectedImage() {
159-
try {
160-
await this.deleteImage();
161-
this.loadFiles();
162-
this.transferData({ delete: true });
163-
} catch (e) {
164-
console.log(e);
165-
}
176+
captionChanged() {
177+
this.transferData({ upload: this.upload });
166178
}
167179

168-
async deleteImage() {
180+
private async checkFolder(): Promise<boolean> {
181+
const options = {
182+
directory: Directory.Data,
183+
path: IMAGE_DIR,
184+
};
169185
try {
170-
await Filesystem.deleteFile({
171-
directory: Directory.Data,
172-
path: this.photo!.path,
173-
});
174-
this.photo = null;
186+
const result = await Filesystem.readdir(options);
187+
return !!result.files;
175188
} catch (e) {
176-
console.log(e);
189+
await Filesystem.mkdir(options);
190+
return true;
177191
}
178192
}
179193

180-
captionChanged() {
181-
this.transferData({ upload: this.upload });
182-
}
183-
184194
private transferData(action: any) {
185195
let params = {
186196
caption: this.captionControl.value,

apps/mobile-mzima-client/src/app/post/helpers/convert-image.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { Filesystem } from '@capacitor/filesystem';
55
export class ConvertImage {
66
async readAsBase64(photo: Photo) {
77
if (Capacitor.getPlatform() === 'hybrid') {
8+
console.log('Reading in Hybrid mode');
89
const file = await Filesystem.readFile({
910
path: photo.path!,
1011
});
1112

1213
return file.data;
1314
} else {
15+
console.log('Reading in Web mode');
1416
const response = await fetch(photo.webPath!);
1517
const blob = await response.blob();
1618

apps/mobile-mzima-client/src/app/post/helpers/upload-file.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ export class UploadFileHelper {
66
constructor(private mediaService: MediaService) {}
77

88
async uploadFile(postData: any, { data, name, caption, path }: any) {
9+
console.log('uploadFile > photo', data, name, caption, path);
910
const fetchPhoto = await fetch(data);
1011
const blob = await fetchPhoto.blob();
12+
console.log('uploadFile > blob', blob);
1113
const file = new File([blob], name);
14+
console.log('uploadFile > file ', file);
1215
try {
1316
const uploadObservable = this.mediaService.uploadFile(file, caption);
1417
const response: any = await lastValueFrom(uploadObservable);
15-
18+
console.log(response);
1619
for (const content of postData.post_content) {
1720
for (const field of content.fields) {
1821
if (field.input === 'upload') {

apps/mobile-mzima-client/src/app/post/post-edit/post-edit.page.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,6 @@ export class PostEditPage {
518518
*/
519519
public async submitPost(): Promise<void> {
520520
if (this.form.disabled) return;
521-
// this.form.disable();
522521

523522
try {
524523
await this.preparationData();
@@ -554,9 +553,6 @@ export class PostEditPage {
554553

555554
await this.offlineStore(postData);
556555

557-
// TODO: Remove after testing
558-
console.log('postData', postData);
559-
560556
if (this.isConnection) {
561557
await this.uploadPost();
562558
} else {
@@ -582,6 +578,7 @@ export class PostEditPage {
582578
*/
583579
async uploadPost() {
584580
const pendingPosts: any[] = await this.dataBaseService.get(STORAGE_KEYS.PENDING_POST_KEY);
581+
console.log('uploadPosts > pendingPosts', pendingPosts);
585582
for (let postData of pendingPosts) {
586583
if (postData?.file?.upload) {
587584
postData = await new UploadFileHelper(this.mediaService).uploadFile(

0 commit comments

Comments
 (0)