root / src / app / settings / settings.component.ts @ 2c8d373c
Historique | Voir | Annoter | Télécharger (8,3 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 |
this.serverJSON = JSON.parse(getString("serverJSON")); |
52 |
|
53 |
let i = 0; |
54 |
this.serverJSON['servers'].forEach(element => { |
55 |
console.log(" JSON element : ", element.name); |
56 |
let option = new RadioOption(element.label, element.uri); |
57 |
this.radioOptions.push(option); |
58 |
if (i == 0) { |
59 |
console.log(" Premier element on le checked : ", element.label); |
60 |
this.serverLabel = element.label; |
61 |
this.serverName = element.label; |
62 |
this.serverURI = element.uri; |
63 |
option.selected = true; |
64 |
} |
65 |
i++; |
66 |
}); |
67 |
|
68 |
//Si jamais le téléchargement fait au lancement a foiré ... |
69 |
if (i == 0) { |
70 |
getJSON("https://clicaudio.abuledu.net/api.php").then((r: any) => { |
71 |
console.log("getJSON : ", r); |
72 |
this.serverJSON = r; |
73 |
let i = 0; |
74 |
r.servers.forEach(element => { |
75 |
console.log(" json element : ", element.name); |
76 |
let option = new RadioOption(element.label, element.uri); |
77 |
this.radioOptions.push(option); |
78 |
if (i == 0) { |
79 |
console.log(" premier element on le checked : ", element.label); |
80 |
this.serverLabel = element.label; |
81 |
this.serverName = element.label; |
82 |
this.serverURI = element.uri; |
83 |
option.selected = true; |
84 |
} |
85 |
i++; |
86 |
}); |
87 |
}, (e) => { |
88 |
console.log("getJSON Error", e); |
89 |
}); |
90 |
} |
91 |
|
92 |
} |
93 |
|
94 |
changeCheckedRadio(radioOption: RadioOption): void { |
95 |
radioOption.selected = !radioOption.selected; |
96 |
|
97 |
if (!radioOption.selected) { |
98 |
return; |
99 |
} |
100 |
|
101 |
this.serverLabel = radioOption.label; |
102 |
this.serverName = radioOption.label; |
103 |
this.serverURI = radioOption.uri; |
104 |
|
105 |
// uncheck all other options |
106 |
this.radioOptions.forEach(option => { |
107 |
//On est sur un qu'il faut décocher |
108 |
if (option.label !== radioOption.label) { |
109 |
option.selected = false; |
110 |
} |
111 |
//On est sur le choix coché |
112 |
}); |
113 |
} |
114 |
|
115 |
|
116 |
onDrawerButtonTap(): void { |
117 |
const sideDrawer = <RadSideDrawer>app.getRootView(); |
118 |
sideDrawer.showDrawer(); |
119 |
} |
120 |
|
121 |
onFocus(args: EventData): void { |
122 |
console.log("on focus: "); |
123 |
} |
124 |
|
125 |
onBlur(args: EventData): void { |
126 |
const tf = <TextField>args.object; |
127 |
this.pseudo = tf.text.toLowerCase().replace(/[^a-z0-9]/g, ""); |
128 |
tf.text = this.pseudo; |
129 |
} |
130 |
|
131 |
onReturnPress(args: EventData): void { |
132 |
console.log("on returnpress"); |
133 |
} |
134 |
|
135 |
onTextChange(args: EventData): void { |
136 |
console.log("on textChange "); |
137 |
} |
138 |
|
139 |
onSelectedIndexChanged(args: EventData) { |
140 |
const picker = <ListPicker>args.object; |
141 |
console.log('index: ${picker.selectedIndex}; item" ${this.years[picker.selectedIndex]}'); |
142 |
} |
143 |
|
144 |
onSaveTap(args: EventData): void { |
145 |
// alert("on sauvegarde ..."); |
146 |
let button = <View>args.object; |
147 |
let parent = button.parent; |
148 |
if (parent) { |
149 |
let p = <TextField>getViewById(parent, "pseudo"); |
150 |
let e = <TextField>getViewById(parent, "email"); |
151 |
|
152 |
this.pseudo = p.text.toLowerCase().replace(/[^a-z0-9]/g, "") |
153 |
this.email = e.text; |
154 |
|
155 |
// console.log("on a un parent, le pseudo : " + this.pseudo); |
156 |
// console.log("le mail : " + this.email); |
157 |
|
158 |
if (this.pseudo == "" || this.email == "") { |
159 |
dialogs.alert({ |
160 |
title: "Pseudo ou email vide !", |
161 |
message: "Veuillez saisir un pseudo et une adresse mail...", |
162 |
okButtonText: "Ok" |
163 |
}); |
164 |
} |
165 |
else { |
166 |
//il faudrait demander au serveur si ce pseudo n'est pas déjà utilisé ... |
167 |
request({ |
168 |
url: "https://" + this.serverURI + "/api.php", |
169 |
method: "POST", |
170 |
headers: { "Content-Type": "application/json" }, |
171 |
content: JSON.stringify({ |
172 |
command: "createUser", |
173 |
pseudo: this.pseudo, |
174 |
email: this.email |
175 |
}) |
176 |
}).then((response) => { |
177 |
const result = response.content.toJSON(); |
178 |
console.log("Valeur de loginOK dans le retour request : ", result['loginOK']); |
179 |
|
180 |
if (result['loginOK'] == true) { |
181 |
setString("pseudo", this.pseudo); |
182 |
setString("email", this.email); |
183 |
setString("serverURI", this.serverURI); |
184 |
setString("serverName", this.serverName); |
185 |
dialogs.alert({ |
186 |
title: "Pseudo enregistré !", |
187 |
message: "Votre pseudo est maintenant associé à votre adresse email vous pouvez utiliser l'application.", |
188 |
okButtonText: "Ok" |
189 |
}).then(() => { |
190 |
this.routerExtensions.navigate(["/home"], { relativeTo: this.activeRoute }); |
191 |
}); |
192 |
} |
193 |
else { |
194 |
console.log("Pseudo déjà utilisé"); |
195 |
dialogs.alert({ |
196 |
title: "Pseudo déjà utilisé !", |
197 |
message: "Malheureusement ce pseudo est déjà utilisé par quelqu'un d'autre. Veuillez changer de pseudo et tenter à nouveau votre chance :-)", |
198 |
okButtonText: "Ok" |
199 |
}); |
200 |
} |
201 |
}, (e) => { |
202 |
console.log("request Error :", e); |
203 |
dialogs.alert({ |
204 |
title: "Erreur de vérification !", |
205 |
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.", |
206 |
okButtonText: "Ok" |
207 |
}); |
208 |
}); |
209 |
} |
210 |
} |
211 |
} |
212 |
} |