root / src / app / home / home.component.ts @ b16c83be
Historique | Voir | Annoter | Télécharger (16 ko)
1 | f3d5fa3b | Eric Seigne | import { Component, OnInit } from "@angular/core"; |
---|---|---|---|
2 | import { takePicture, requestCameraPermissions } from 'nativescript-camera'; |
||
3 | import { PhotoEditor, PhotoEditorControl } from "nativescript-photo-editor"; |
||
4 | import { ImageAsset } from 'tns-core-modules/image-asset'; |
||
5 | import { RadSideDrawer } from "nativescript-ui-sidedrawer"; |
||
6 | import { knownFolders, Folder, File, path } from "tns-core-modules/file-system"; |
||
7 | import { ImageSource, fromFile, fromResource, fromBase64, fromAsset } from "tns-core-modules/image-source"; |
||
8 | import { Image } from "tns-core-modules/ui/image"; |
||
9 | import { EventData } from "tns-core-modules/data/observable"; |
||
10 | import { getViewById } from "tns-core-modules/ui/core/view"; |
||
11 | import { TextField } from "tns-core-modules/ui/text-field"; |
||
12 | import { isAndroid, isIOS, device, screen } from "tns-core-modules/platform"; |
||
13 | 2216fd44 | Eric Seigne | import { ActivityIndicator } from "tns-core-modules/ui/activity-indicator"; |
14 | f3d5fa3b | Eric Seigne | import { getString, setString, hasKey, remove, clear } from "tns-core-modules/application-settings"; |
15 | 2216fd44 | Eric Seigne | import { Button } from "tns-core-modules/ui/button"; |
16 | 2c8d373c | Eric Seigne | import { NavigationEnd, Router } from "@angular/router"; |
17 | import { RouterExtensions } from "nativescript-angular/router"; |
||
18 | f3d5fa3b | Eric Seigne | |
19 | 2216fd44 | Eric Seigne | import * as bghttp from "nativescript-background-http"; |
20 | import * as appversion from "nativescript-appversion"; |
||
21 | f3d5fa3b | Eric Seigne | import * as dialogs from "tns-core-modules/ui/dialogs"; |
22 | import * as app from "tns-core-modules/application"; |
||
23 | cda781e8 | Eric Seigne | import * as imagepicker from "nativescript-imagepicker"; |
24 | f3d5fa3b | Eric Seigne | |
25 | declare var NSString: any; |
||
26 | declare var NSUTF8StringEncoding: any; |
||
27 | declare var java: any; |
||
28 | declare var android: any; |
||
29 | @Component({ |
||
30 | selector: "Home", |
||
31 | templateUrl: "./home.component.html" |
||
32 | }) |
||
33 | export class HomeComponent implements OnInit { |
||
34 | public cameraImage: ImageAsset; |
||
35 | public pseudo: string; |
||
36 | public email: string; |
||
37 | public label: string; |
||
38 | 25220fd6 | Eric Seigne | public legende: string; |
39 | f3d5fa3b | Eric Seigne | public album: string; |
40 | public albumPass: string; |
||
41 | private photoEditor; |
||
42 | private appVersion; |
||
43 | public serverName: string; |
||
44 | public serverURI: string; |
||
45 | public hasPicture: boolean; |
||
46 | 2216fd44 | Eric Seigne | public isBusy: boolean = true; |
47 | f3d5fa3b | Eric Seigne | |
48 | |||
49 | 2c8d373c | Eric Seigne | constructor(private router: Router, private routerExtensions: RouterExtensions) { |
50 | f3d5fa3b | Eric Seigne | // Use the component constructor to inject providers. |
51 | appversion.getVersionName().then((v: string) => { |
||
52 | if (isAndroid) { |
||
53 | this.appVersion = "Android/" + v; |
||
54 | } |
||
55 | else if (isIOS) { |
||
56 | this.appVersion = "iOS/" + v; |
||
57 | } |
||
58 | else { |
||
59 | this.appVersion = "Other/" + v; |
||
60 | } |
||
61 | 2216fd44 | Eric Seigne | console.log("clicalbum: Your app's version is: " + this.appVersion); |
62 | f3d5fa3b | Eric Seigne | }); |
63 | } |
||
64 | |||
65 | ngOnInit(): void { |
||
66 | // Init your component properties here. |
||
67 | cda781e8 | Eric Seigne | this.photoEditor = new PhotoEditor(); |
68 | this.hasPicture = false; |
||
69 | f3d5fa3b | Eric Seigne | this.pseudo = getString("pseudo", ""); |
70 | this.email = getString("email", ""); |
||
71 | this.serverName = getString("serverName", "AbulEdu-FR"); |
||
72 | this.serverURI = getString("serverURI", "clicalbum.abuledu.net"); |
||
73 | this.album = getString("album", ""); |
||
74 | this.albumPass = getString("albumPass", ""); |
||
75 | this.label = ""; |
||
76 | 25220fd6 | Eric Seigne | this.legende = ""; |
77 | 2216fd44 | Eric Seigne | this.isBusy = false; |
78 | 2c8d373c | Eric Seigne | this.enableButtons("btnSendPicture", false); |
79 | |||
80 | if (this.email == "" || this.pseudo == "") { |
||
81 | // alert("L'application n'est pas configurée ...") |
||
82 | this.routerExtensions.navigate(["/settings"]); |
||
83 | } |
||
84 | |||
85 | f3d5fa3b | Eric Seigne | } |
86 | |||
87 | 2216fd44 | Eric Seigne | enableButtons(btnName, onOff) { |
88 | let b = <Button>getViewById(app.getRootView(), btnName); |
||
89 | if (b) { |
||
90 | b.isEnabled = onOff; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | |||
95 | f3d5fa3b | Eric Seigne | onBlur(args: EventData): void { |
96 | let obj = <TextField>args.object; |
||
97 | if (obj.id == "album") { |
||
98 | this.album = obj.text.toLowerCase().replace(/[^a-z0-9]/g, ""); |
||
99 | } |
||
100 | if (obj.id == "albumPass") { |
||
101 | this.albumPass = obj.text.toLowerCase().replace(/[^a-z0-9]/g, ""); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | onDrawerButtonTap(): void { |
||
106 | const sideDrawer = <RadSideDrawer>app.getRootView(); |
||
107 | sideDrawer.showDrawer(); |
||
108 | } |
||
109 | |||
110 | cda781e8 | Eric Seigne | onGetPictureFromRollTap(args: EventData) { |
111 | var that = this; |
||
112 | let context = imagepicker.create({ |
||
113 | mode: "single" // use "multiple" for multiple selection |
||
114 | }); |
||
115 | context |
||
116 | .authorize() |
||
117 | .then(function () { |
||
118 | return context.present(); |
||
119 | }) |
||
120 | .then(function (selection) { |
||
121 | selection.forEach(function (selected) { |
||
122 | let imageSource = new ImageSource(); |
||
123 | imageSource.fromAsset(selected).then((imageSource) => { |
||
124 | console.log("ORIG IMAGE SIZE FROM ROLL: " + imageSource.height + " x " + imageSource.width); |
||
125 | console.dir(imageSource); |
||
126 | // console.log("un ter"); |
||
127 | f3d5fa3b | Eric Seigne | |
128 | cda781e8 | Eric Seigne | that.photoEditor.editPhoto({ |
129 | imageSource: imageSource, |
||
130 | hiddenControls: [ |
||
131 | PhotoEditorControl.Draw, |
||
132 | PhotoEditorControl.Text, |
||
133 | PhotoEditorControl.Clear, |
||
134 | ], |
||
135 | }).then((newImage: ImageSource) => { |
||
136 | console.log("NEW IMAGE: ", newImage.height, newImage.width); |
||
137 | that.imageCropped(newImage); |
||
138 | // Here you can save newImage, send it to your backend or simply display it in your app |
||
139 | // that.resultImage.imageSource = newImage; |
||
140 | }).catch((e) => { |
||
141 | console.log("erreur ES 1"); |
||
142 | console.error(e); |
||
143 | }); |
||
144 | console.log("trois"); |
||
145 | }); |
||
146 | |||
147 | }); |
||
148 | console.log("Selection des fichiers ", selection); |
||
149 | }).catch(function (e) { |
||
150 | console.log("Error ES: " + e); |
||
151 | // process error |
||
152 | }); |
||
153 | |||
154 | } |
||
155 | |||
156 | onTakePictureTap(args: EventData) { |
||
157 | f3d5fa3b | Eric Seigne | const options = { |
158 | keepAspectRatio: true, |
||
159 | saveToGallery: false |
||
160 | }; |
||
161 | |||
162 | // let button = args.object as Button; |
||
163 | requestCameraPermissions().then( |
||
164 | () => { |
||
165 | takePicture(options) |
||
166 | .then((imageAsset: any) => { |
||
167 | let imageSource = new ImageSource(); |
||
168 | |||
169 | imageSource.fromAsset(imageAsset).then((imageSource) => { |
||
170 | cda781e8 | Eric Seigne | console.log("ORIG IMAGE SIZE: " + imageSource.height + " x " + imageSource.width); |
171 | f3d5fa3b | Eric Seigne | console.dir(imageSource); |
172 | // console.log("un ter"); |
||
173 | |||
174 | this.photoEditor.editPhoto({ |
||
175 | imageSource: imageSource, |
||
176 | hiddenControls: [ |
||
177 | PhotoEditorControl.Draw, |
||
178 | PhotoEditorControl.Text, |
||
179 | PhotoEditorControl.Clear, |
||
180 | ], |
||
181 | }).then((newImage: ImageSource) => { |
||
182 | console.log("NEW IMAGE: ", newImage.height, newImage.width); |
||
183 | this.imageCropped(newImage); |
||
184 | // Here you can save newImage, send it to your backend or simply display it in your app |
||
185 | // this.resultImage.imageSource = newImage; |
||
186 | }).catch((e) => { |
||
187 | console.log("erreur 1"); |
||
188 | console.error(e); |
||
189 | }); |
||
190 | console.log("trois"); |
||
191 | }); |
||
192 | |||
193 | }, (error) => { |
||
194 | console.log("Error: " + error); |
||
195 | }); |
||
196 | }, |
||
197 | () => alert('permissions rejected') |
||
198 | ); |
||
199 | } |
||
200 | |||
201 | onResetPictureTap(args) { |
||
202 | // this.cameraImage = "res://icon"; |
||
203 | } |
||
204 | |||
205 | onGotoWebTap() { |
||
206 | if (this.album == "") { |
||
207 | dialogs.alert({ |
||
208 | title: "Nom d'album", |
||
209 | message: "Il faut renseigner le nom de l'album que vous voulez consulter ...", |
||
210 | okButtonText: "Ok" |
||
211 | }).then(() => { |
||
212 | }); |
||
213 | } |
||
214 | |||
215 | var utilityModule = require("utils/utils"); |
||
216 | utilityModule.openUrl("https://" + this.serverURI + "/" + this.album); |
||
217 | } |
||
218 | |||
219 | onSendPictureTap(args) { |
||
220 | 2216fd44 | Eric Seigne | this.enableButtons("btnSendPicture", false) |
221 | this.isBusy = true; |
||
222 | f3d5fa3b | Eric Seigne | let labelObj = <TextField>getViewById(app.getRootView(), "label"); |
223 | this.label = labelObj.text; |
||
224 | 25220fd6 | Eric Seigne | let legendeObj = <TextField>getViewById(app.getRootView(), "legende"); |
225 | this.legende = legendeObj.text; |
||
226 | f3d5fa3b | Eric Seigne | let albumObj = <TextField>getViewById(app.getRootView(), "album"); |
227 | 2216fd44 | Eric Seigne | this.album = albumObj.text.toLowerCase().replace(/[^a-z0-9]/g, ""); |
228 | f3d5fa3b | Eric Seigne | albumObj = <TextField>getViewById(app.getRootView(), "albumPass"); |
229 | this.albumPass = albumObj.text; |
||
230 | |||
231 | console.log("on a maintenant ", this.albumPass); |
||
232 | |||
233 | |||
234 | if (this.album == "" || this.label == "") { |
||
235 | dialogs.alert({ |
||
236 | title: "Informations manquantes", |
||
237 | message: "Il faut renseigner le nom de l'album auquel vous voulez participer et donner un nom à votre photo ...", |
||
238 | okButtonText: "Ok" |
||
239 | }).then(() => { |
||
240 | 2216fd44 | Eric Seigne | this.enableButtons("btnSendPicture", true) |
241 | this.isBusy = false; |
||
242 | console.log("clicalbum: album ou label vide"); |
||
243 | return; |
||
244 | f3d5fa3b | Eric Seigne | }); |
245 | } |
||
246 | else { |
||
247 | setString("album", this.album); |
||
248 | setString("albumPass", this.albumPass); |
||
249 | this.sendPicture(args); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | sendPicture(args) { |
||
254 | 2216fd44 | Eric Seigne | this.enableButtons("btnSendPicture", false) |
255 | this.isBusy = true; |
||
256 | f3d5fa3b | Eric Seigne | // let button = args.object as Button; |
257 | const folderPath: string = knownFolders.documents().path; |
||
258 | const fileName = "fichier_temporaire.jpeg"; |
||
259 | const filePath = path.join(folderPath, fileName); |
||
260 | 2216fd44 | Eric Seigne | let labelObj = <TextField>getViewById(app.getRootView(), "label"); |
261 | this.label = labelObj.text; |
||
262 | 25220fd6 | Eric Seigne | let legendeObj = <TextField>getViewById(app.getRootView(), "legende"); |
263 | this.legende = legendeObj.text; |
||
264 | 2216fd44 | Eric Seigne | |
265 | console.log("On voudrait uploader le fichier " + filePath + " dans l'album " + this.album + " sous le nom " + this.label); |
||
266 | |||
267 | if (this.serverURI == "") { |
||
268 | dialogs.alert({ |
||
269 | title: "Serveur ????", |
||
270 | message: "Suite à une collision dans l'espace-temps des nano particules permettant la bonne configuration du logiciel l'adresse du serveur s'est retrouvée vide ... bien entendu ce bug ne devrait pas surgir mais pour continuer vous devrez à nouveau choisir un serveur dans la configuration ... Merci pour votre compréhension", |
||
271 | okButtonText: "Ok" |
||
272 | }).then(() => { |
||
273 | this.enableButtons("btnSendPicture", true) |
||
274 | this.isBusy = false; |
||
275 | console.log("clicalbum: Adresse serveur vide"); |
||
276 | return; |
||
277 | }); |
||
278 | } |
||
279 | f3d5fa3b | Eric Seigne | |
280 | var url = "https://" + this.serverURI + "/u.php"; |
||
281 | 2216fd44 | Eric Seigne | console.log("clicalbum: POST vers " + url); |
282 | f3d5fa3b | Eric Seigne | |
283 | // // upload configuration |
||
284 | var session = bghttp.session("image-upload"); |
||
285 | var request = { |
||
286 | url: url, |
||
287 | method: "POST", |
||
288 | headers: { |
||
289 | "Content-Type": "application/octet-stream", |
||
290 | "User-Agent": "ClicAlbum " + this.appVersion, |
||
291 | }, |
||
292 | description: "Uploading " + fileName |
||
293 | }; |
||
294 | |||
295 | var params = [ |
||
296 | { name: "login", value: this.base64Encode(this.pseudo) }, |
||
297 | { name: "email", value: this.base64Encode(this.email) }, |
||
298 | { name: "label", value: this.base64Encode(this.label) }, |
||
299 | 25220fd6 | Eric Seigne | { name: "legende", value: this.base64Encode(this.legende) }, |
300 | f3d5fa3b | Eric Seigne | { name: "album", value: this.base64Encode(this.album) }, |
301 | { name: "albumPass", value: this.base64Encode(this.albumPass) }, |
||
302 | { name: "version", value: "2" }, |
||
303 | { name: "fileToUpload", filename: filePath, mimeType: "image/jpeg" } |
||
304 | ]; |
||
305 | var task = session.multipartUpload(params, request); |
||
306 | |||
307 | 2216fd44 | Eric Seigne | task.on("progress", logEvent.bind(this)); |
308 | task.on("error", this.uploadError.bind(this)); |
||
309 | task.on("complete", this.uploadCompleted.bind(this)); |
||
310 | f3d5fa3b | Eric Seigne | function logEvent(e) { |
311 | 2216fd44 | Eric Seigne | console.log("clicalbum: UPLOAD STATUS: " + e.eventName); |
312 | f3d5fa3b | Eric Seigne | } |
313 | 2216fd44 | Eric Seigne | } |
314 | f3d5fa3b | Eric Seigne | |
315 | 2216fd44 | Eric Seigne | uploadError(this) { |
316 | console.log("clicalbum: UPLOAD ERROR"); |
||
317 | dialogs.alert({ |
||
318 | title: "Erreur de téléversement", |
||
319 | message: "Pour une raison inconnue le téléversement de votre fichier n'a pas été possible ... si cette erreur persiste merci de nous le signaler via le formulaire de contact disponible sur clicpdf.org", |
||
320 | okButtonText: "Ok" |
||
321 | }).then(() => { |
||
322 | this.enableButtons("btnSendPicture", true) |
||
323 | this.isBusy = false; |
||
324 | console.log("clicalbum: Dialog closed!"); |
||
325 | }); |
||
326 | } |
||
327 | f3d5fa3b | Eric Seigne | |
328 | 2216fd44 | Eric Seigne | uploadCompleted(this) { |
329 | console.log("clicalbum: UPLOAD STATUS: terminé"); |
||
330 | console.log("clicalbum: UPLOAD STATUS: terminé end"); |
||
331 | dialogs.alert({ |
||
332 | title: "Téléversement terminé", |
||
333 | message: "Votre document est maintenant sur le serveur", |
||
334 | okButtonText: "Ok" |
||
335 | }).then(() => { |
||
336 | this.isBusy = false; |
||
337 | this.displayImage(""); |
||
338 | this.enableButtons("btnSendPicture", false) |
||
339 | console.log("clicalbum: Dialog closed!"); |
||
340 | }); |
||
341 | } |
||
342 | |||
343 | saveImage(image, fileName) { |
||
344 | // // =========== sauvegarde de l'image |
||
345 | if (image.saveToFile(fileName, "jpeg", 95)) { |
||
346 | console.log("clicalbum: Image saved successfully in " + fileName); |
||
347 | this.displayImage(fileName); |
||
348 | f3d5fa3b | Eric Seigne | } |
349 | 2216fd44 | Eric Seigne | } |
350 | f3d5fa3b | Eric Seigne | |
351 | 2216fd44 | Eric Seigne | displayImage(fileName) { |
352 | console.log("clicalbum: displayImage " + fileName); |
||
353 | // if (fileName != "") { |
||
354 | let i = new ImageSource().fromFile(fileName); |
||
355 | this.cameraImage = fileName; |
||
356 | // } |
||
357 | // let i = <Image>getViewById(app.getRootView(), "cameraImage"); |
||
358 | // if (i) { |
||
359 | // console.log(" objet image ok"); |
||
360 | |||
361 | // if (fileName != "") { |
||
362 | // i.src = fileName; |
||
363 | // this.enableButtons("btnSendPicture", true) |
||
364 | // } |
||
365 | // else { |
||
366 | // i.src = "res://icon"; |
||
367 | // } |
||
368 | // } |
||
369 | |||
370 | this.isBusy = false; |
||
371 | if (fileName != "") { |
||
372 | this.hasPicture = true; |
||
373 | } |
||
374 | else { |
||
375 | this.hasPicture = false; |
||
376 | } |
||
377 | let b = <ActivityIndicator>getViewById(app.getRootView(), "busyIndicator"); |
||
378 | if (b) { |
||
379 | console.log(" objet busy ok"); |
||
380 | b.busy = false; |
||
381 | f3d5fa3b | Eric Seigne | } |
382 | 2216fd44 | Eric Seigne | |
383 | console.log("clicalbum: end of displayImage"); |
||
384 | f3d5fa3b | Eric Seigne | } |
385 | |||
386 | 2216fd44 | Eric Seigne | |
387 | f3d5fa3b | Eric Seigne | imageCropped(image) { |
388 | 2216fd44 | Eric Seigne | // this.cameraImage = image; |
389 | f3d5fa3b | Eric Seigne | console.log("imageCropped ..."); |
390 | console.log("NEW IMAGE: ", image.height, image.width); |
||
391 | |||
392 | // // =========== sauvegarde de l'image |
||
393 | const folderPath: string = knownFolders.documents().path; |
||
394 | const fileName = "fichier_temporaire.jpeg"; |
||
395 | const filePath = path.join(folderPath, fileName); |
||
396 | 2216fd44 | Eric Seigne | this.saveImage(image, filePath) |
397 | f3d5fa3b | Eric Seigne | } |
398 | |||
399 | base64Encode(value) { |
||
400 | 2216fd44 | Eric Seigne | console.log("clicalbum: base64 pour " + value); |
401 | f3d5fa3b | Eric Seigne | if (isIOS) { |
402 | let text = NSString.stringWithString(value); |
||
403 | let data = text.dataUsingEncoding(NSUTF8StringEncoding); |
||
404 | return data.base64EncodedStringWithOptions(0); |
||
405 | } else { |
||
406 | let text = new java.lang.String(value); |
||
407 | let data = text.getBytes("UTF-8"); |
||
408 | return android.util.Base64.encodeToString(data, android.util.Base64.DEFAULT); |
||
409 | } |
||
410 | } |
||
411 | |||
412 | 2216fd44 | Eric Seigne | onBusyChanged(args: EventData) { |
413 | let indicator: ActivityIndicator = <ActivityIndicator>args.object; |
||
414 | console.log("clicalbum: indicator.busy changed to: " + indicator.busy); |
||
415 | } |
||
416 | |||
417 | f3d5fa3b | Eric Seigne | } |