· 6 years ago · Mar 10, 2019, 06:22 AM
1{
2 "duration": 5,
3 "intervals": 60,
4 "time_work_start": 540,
5 "time_work_end": 1080,
6 "lunch_start": 780,
7 "lunch_end": 840,
8 "workweek": {
9 "1": "Понедельник",
10 "2": "Вторник"
11 },
12 "time_delay": 15
13}
14
15public class BodyUserSettings implements Serializable {
16
17@SerializedName("duration")
18@Expose
19private String duration;
20@SerializedName("intervals")
21@Expose
22private String intervals;
23@SerializedName("time_work_start")
24@Expose
25private String timeWorkStart;
26@SerializedName("time_work_end")
27@Expose
28private String timeWorkEnd;
29@SerializedName("lunch_start")
30@Expose
31private String lunchStart;
32@SerializedName("lunch_end")
33@Expose
34private String lunchEnd;
35@SerializedName("workweek")
36@Expose
37private Workweek workweek;
38@SerializedName("time_delay")
39@Expose
40private String timeDelay;
41
42public String getDuration() {
43 return duration;
44}
45
46public void setDuration(String duration) {
47 this.duration = duration;
48}
49
50public BodyUserSettings withDuration(String duration) {
51 this.duration = duration;
52 return this;
53}
54
55public String getIntervals() {
56 return intervals;
57}
58
59public void setIntervals(String intervals) {
60 this.intervals = intervals;
61}
62
63public BodyUserSettings withIntervals(String intervals) {
64 this.intervals = intervals;
65 return this;
66}
67
68public String getTimeWorkStart() {
69 return timeWorkStart;
70}
71
72public void setTimeWorkStart(String timeWorkStart) {
73 this.timeWorkStart = timeWorkStart;
74}
75
76public BodyUserSettings withTimeWorkStart(String timeWorkStart) {
77 this.timeWorkStart = timeWorkStart;
78 return this;
79}
80
81public String getTimeWorkEnd() {
82 return timeWorkEnd;
83}
84
85public void setTimeWorkEnd(String timeWorkEnd) {
86 this.timeWorkEnd = timeWorkEnd;
87}
88
89public BodyUserSettings withTimeWorkEnd(String timeWorkEnd) {
90 this.timeWorkEnd = timeWorkEnd;
91 return this;
92}
93
94public String getLunchStart() {
95 return lunchStart;
96}
97
98public void setLunchStart(String lunchStart) {
99 this.lunchStart = lunchStart;
100}
101
102public BodyUserSettings withLunchStart(String lunchStart) {
103 this.lunchStart = lunchStart;
104 return this;
105}
106
107public String getLunchEnd() {
108 return lunchEnd;
109}
110
111public void setLunchEnd(String lunchEnd) {
112 this.lunchEnd = lunchEnd;
113}
114
115public BodyUserSettings withLunchEnd(String lunchEnd) {
116 this.lunchEnd = lunchEnd;
117 return this;
118}
119
120public Workweek getWorkweek() {
121 return workweek;
122}
123
124public void setWorkweek(Workweek workweek) {
125 this.workweek = workweek;
126}
127
128public BodyUserSettings withWorkweek(Workweek workweek) {
129 this.workweek = workweek;
130 return this;
131}
132
133public String getTimeDelay() {
134 return timeDelay;
135}
136
137public void setTimeDelay(String timeDelay) {
138 this.timeDelay = timeDelay;
139}
140
141public BodyUserSettings withTimeDelay(String timeDelay) {
142 this.timeDelay = timeDelay;
143 return this;
144}
145
146public class Workweek implements Serializable {
147
148 @SerializedName("1")
149 @Expose
150 private String _1;
151 @SerializedName("2")
152 @Expose
153 private String _2;
154
155 public Workweek(String _1, String _2) {
156 super();
157 this._1 = _1;
158 this._2 = _2;
159 }
160
161 public String getId() {
162 return _1;
163 }
164
165 public void setId(String id) {
166 this._1 = id;
167 }
168
169 public Workweek withId(String _1) {
170 this._1 = _1;
171 return this;
172 }
173
174 public String getTitle() {
175 return _2;
176 }
177
178 public void setTitle(String _2) {
179 this._2 = _2;
180 }
181
182 public Workweek withTitle(String _2) {
183 this._2 = _2;
184 return this;
185 }
186}
187
188@POST("/settings")
189Call<BodyUserSettings> postIdToken(@Body BodyIdToken id_token);
190
191private void sendRequestIdTokenToServer() {
192 BodyIdToken hexPost = new BodyIdToken();
193 hexPost.idToken = mIdToken;
194 Call<BodyUserSettings> call = mService.postIdToken(hexPost);
195 call.enqueue(new Callback<BodyUserSettings>() {
196 @Override
197 public void onResponse(@NonNull Call<BodyUserSettings> call, @NonNull Response<BodyUserSettings> response) {
198 mBodyUserSettings = response.body();
199 mMainDatabase.saveSettings(mBodyUserSettings);
200 mMainDatabase.saveWorkweekDays(mBodyWorkweek);
201 }
202
203 @Override
204 public void onFailure(@NonNull Call<BodyUserSettings> call, @NonNull Throwable t) {
205
206 }
207 });
208}
209
210private final String CREATE_SETTINGS_TABLE = "CREATE TABLE IF NOT EXISTS " +
211 SETTINGS_TABLE + "(" +
212 _id + " INTEGER PRIMARY KEY AUTOINCREMENT," +
213 DURATION + "INTEGER," +
214 INTERVALS + " INTEGER," +
215 TIME_WORK_START + " INTEGER," +
216 TIME_WORK_END + " INTEGER," +
217 LAUNCH_START + " INTEGER," +
218 LAUNCH_END + " INTEGER," +
219 WORKWEEK + " INTEGER," +
220 TIME_DELAY + " INTEGER" + ")";
221
222 private final String CREATE_WORKWEEK_TABLE = "CREATE TABLE IF NOT EXISTS " +
223 WORKWEEK_TABLE + "(" +
224 _id + " INTEGER PRIMARY KEY," +
225 ID_DAY + " TEXT," +
226 TITLE_DAY + " TEXT" + ")";
227
228public void saveSettings(BodyUserSettings settings) {
229 SQLiteDatabase db = this.getWritableDatabase();
230 ContentValues cv = new ContentValues();
231
232 cv.put(DURATION, settings.getDuration());
233 cv.put(INTERVALS, settings.getIntervals());
234 cv.put(TIME_WORK_START, settings.getTimeWorkStart());
235 cv.put(TIME_WORK_END, settings.getTimeWorkEnd());
236 cv.put(LAUNCH_START, settings.getLunchStart());
237 cv.put(LAUNCH_END, settings.getLunchEnd());
238 cv.put(WORKWEEK, String.valueOf(settings.getWorkweek()));
239 cv.put(TIME_DELAY, settings.getTimeDelay());
240
241 db.insert(SETTINGS_TABLE, null, cv);
242 Log.d("LOG_TAG_saveSettings ", "save: " +
243 settings.getDuration() + "; " +
244 settings.getIntervals() + "; " +
245 settings.getTimeWorkStart() + "; " +
246 settings.getTimeWorkEnd() + "; " +
247 settings.getLunchStart() + "; " +
248 settings.getLunchEnd() + "; " +
249 settings.getWorkweek() + "; " +
250 settings.getTimeDelay() + ".");
251 db.close();
252}
253
254public void saveWorkweekDays(BodyUserSettings.Workweek workweek) {
255 SQLiteDatabase db = this.getWritableDatabase();
256 ContentValues cv = new ContentValues();
257
258 cv.put(ID_DAY, workweek.getId());
259 cv.put(TITLE_DAY, workweek.getTitle());
260
261 db.insert(WORKWEEK_TABLE, null, cv);
262 Log.d("LOG_TAG_saveWorkweek", "save " +
263 workweek.getId() + "; " +
264 workweek.getTitle() + ".");
265 db.close();
266}