root / src / app / settings / settings.component.ts @ 62ed0250
Historique | Voir | Annoter | Télécharger (7,49 ko)
1 |
import { Component, OnInit } from "@angular/core"; |
---|---|
2 |
import { RadSideDrawer } from "nativescript-ui-sidedrawer"; |
3 |
import { TextField } from "tns-core-modules/ui/text-field"; |
4 |
import { EventData } from "tns-core-modules/data/observable"; |
5 |
import { getViewById } from "tns-core-modules/ui/core/view"; |
6 |
import { View } from "tns-core-modules/ui/core/view"; |
7 |
import { Page } from "tns-core-modules/ui/page"; |
8 |
import { ActivatedRoute } from "@angular/router"; |
9 |
import { RouterExtensions } from "nativescript-angular/router"; |
10 |
import { getFile, getImage, getJSON, request, HttpResponse } from "tns-core-modules/http"; |
11 |
import { getString, setString, hasKey, remove, clear } from "tns-core-modules/application-settings"; |
12 |
import { ListPicker } from "tns-core-modules/ui/list-picker"; |
13 |
import { getFrameById } from "tns-core-modules/ui/frame"; |
14 |
import { ObservableArray, ChangedData } from "tns-core-modules/data/observable-array"; |
15 |
import { CheckBox } from '@nstudio/nativescript-checkbox'; |
16 |
import { topmost } from 'tns-core-modules/ui/frame'; |
17 |
|
18 |
import * as dialogs from "tns-core-modules/ui/dialogs"; |
19 |
import * as app from "tns-core-modules/application"; |
20 |
import { observable } from "rxjs"; |
21 |
import { RadioOption } from "./radio-option"; |
22 |
|
23 |
@Component({ |
24 |
selector: "Settings", |
25 |
templateUrl: "./settings.component.html" |
26 |
}) |
27 |
export class SettingsComponent implements OnInit { |
28 |
public pseudo: string; |
29 |
public email: string; |
30 |
public serverJSON: JSON; |
31 |
public serverName: string; |
32 |
public serverURI: string; |
33 |
public serverLabel: string; |
34 |
radioOptions?: Array<RadioOption>; |
35 |
|
36 |
constructor(private routerExtensions: RouterExtensions, private activeRoute: ActivatedRoute) { |
37 |
// Use the component constructor to inject providers. |
38 |
} |
39 |
|
40 |
ngOnInit(): void { |
41 |
this.radioOptions = []; |
42 |
|
43 |
let serversLabelTMP = Array(); |
44 |
// Init your component properties here. |
45 |
this.pseudo = getString("pseudo", ""); |
46 |
this.email = getString("email", ""); |
47 |
this.serverName = getString("serverName", ""); |
48 |
this.serverURI = getString("serverURI", ""); |
49 |
|
50 |
//On recupere les infos serveurs et en particulier la liste des serveurs disponibles |
51 |
getJSON("https://clicalbum.abuledu.net/api.php").then((r: any) => { |
52 |
console.log("getJSON : ", r); |
53 |
this.serverJSON = r; |
54 |
let i = 0; |
55 |
r.servers.forEach(element => { |
56 |
console.log(" json element : ", element.name); |
57 |
let option = new RadioOption(element.label, element.uri); |
58 |
this.radioOptions.push(option); |
59 |
if (i == 0) { |
60 |
console.log(" premier element on le checked : ", element.label); |
61 |
this.serverLabel = element.label; |
62 |
this.serverName = element.label; |
63 |
this.serverURI = element.uri; |
64 |
option.selected = true; |
65 |
} |
66 |
i++; |
67 |
}); |
68 |
}, (e) => { |
69 |
console.log("getJSON Error", e); |
70 |
}); |
71 |
} |
72 |
|
73 |
changeCheckedRadio(radioOption: RadioOption): void { |
74 |
radioOption.selected = !radioOption.selected; |
75 |
|
76 |
if (!radioOption.selected) { |
77 |
return; |
78 |
} |
79 |
|
80 |
this.serverLabel = radioOption.label; |
81 |
this.serverName = radioOption.label; |
82 |
this.serverURI = radioOption.uri; |
83 |
|
84 |
// uncheck all other options |
85 |
this.radioOptions.forEach(option => { |
86 |
//On est sur un qu'il faut décocher |
87 |
if (option.label !== radioOption.label) { |
88 |
option.selected = false; |
89 |
} |
90 |
//On est sur le choix coché |
91 |
}); |
92 |
} |
93 |
|
94 |
|
95 |
onDrawerButtonTap(): void { |
96 |
const sideDrawer = <RadSideDrawer>app.getRootView(); |
97 |
sideDrawer.showDrawer(); |
98 |
} |
99 |
|
100 |
onFocus(args: EventData): void { |
101 |
console.log("on focus: "); |
102 |
} |
103 |
|
104 |
onBlur(args: EventData): void { |
105 |
const tf = <TextField>args.object; |
106 |
this.pseudo = tf.text.toLowerCase().replace(/[^a-z0-9]/g, ""); |
107 |
tf.text = this.pseudo; |
108 |
} |
109 |
|
110 |
onReturnPress(args: EventData): void { |
111 |
console.log("on returnpress"); |
112 |
} |
113 |
|
114 |
onTextChange(args: EventData): void { |
115 |
console.log("on textChange "); |
116 |
} |
117 |
|
118 |
onSelectedIndexChanged(args: EventData) { |
119 |
const picker = <ListPicker>args.object; |
120 |
console.log('index: ${picker.selectedIndex}; item" ${this.years[picker.selectedIndex]}'); |
121 |
} |
122 |
|
123 |
onSaveTap(args: EventData): void { |
124 |
// alert("on sauvegarde ..."); |
125 |
let button = <View>args.object; |
126 |
let parent = button.parent; |
127 |
if (parent) { |
128 |
let p = <TextField>getViewById(parent, "pseudo"); |
129 |
let e = <TextField>getViewById(parent, "email"); |
130 |
|
131 |
this.pseudo = p.text.toLowerCase().replace(/[^a-z0-9]/g, "") |
132 |
this.email = e.text; |
133 |
|
134 |
// console.log("on a un parent, le pseudo : " + this.pseudo); |
135 |
// console.log("le mail : " + this.email); |
136 |
|
137 |
if (this.pseudo == "" || this.email == "") { |
138 |
dialogs.alert({ |
139 |
title: "Pseudo ou email vide !", |
140 |
message: "Veuillez saisir un pseudo et une adresse mail...", |
141 |
okButtonText: "Ok" |
142 |
}); |
143 |
} |
144 |
else { |
145 |
//il faudrait demander au serveur si ce pseudo n'est pas déjà utilisé ... |
146 |
request({ |
147 |
url: "https://" + this.serverURI + "/api.php", |
148 |
method: "POST", |
149 |
headers: { "Content-Type": "application/json" }, |
150 |
content: JSON.stringify({ |
151 |
command: "createUser", |
152 |
pseudo: this.pseudo, |
153 |
email: this.email |
154 |
}) |
155 |
}).then((response) => { |
156 |
const result = response.content.toJSON(); |
157 |
console.log("Valeur de loginOK dans le retour request : ", result['loginOK']); |
158 |
|
159 |
if (result['loginOK'] == true) { |
160 |
setString("pseudo", this.pseudo); |
161 |
setString("email", this.email); |
162 |
setString("serverURI", this.serverURI); |
163 |
setString("serverName", this.serverName); |
164 |
dialogs.alert({ |
165 |
title: "Pseudo enregistré !", |
166 |
message: "Votre pseudo est maintenant associé à votre adresse email vous pouvez utiliser l'application.", |
167 |
okButtonText: "Ok" |
168 |
}).then(() => { |
169 |
this.routerExtensions.navigate(["/home"], { relativeTo: this.activeRoute }); |
170 |
}); |
171 |
} |
172 |
else { |
173 |
console.log("Pseudo déjà utilisé"); |
174 |
dialogs.alert({ |
175 |
title: "Pseudo déjà utilisé !", |
176 |
message: "Malheureusement ce pseudo est déjà utilisé par quelqu'un d'autre. Veuillez changer de pseudo et tenter à nouveau votre chance :-)", |
177 |
okButtonText: "Ok" |
178 |
}); |
179 |
} |
180 |
}, (e) => { |
181 |
console.log("request Error :", e); |
182 |
dialogs.alert({ |
183 |
title: "Erreur de vérification !", |
184 |
message: "Une erreur de communication avec le serveur nous empêche de vérifier si ce pseudo est déjà utilisé ... ré-essayez plus tard.", |
185 |
okButtonText: "Ok" |
186 |
}); |
187 |
}); |
188 |
} |
189 |
} |
190 |
} |
191 |
} |