Skip to content

Commit 7f502d5

Browse files
committed
+ added DbPopulator.java
1 parent c097e22 commit 7f502d5

File tree

13 files changed

+320
-172
lines changed

13 files changed

+320
-172
lines changed

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/build
2+
/release
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package dev.favier.exam1radioamateur;
2+
3+
import android.content.Context;
4+
import org.json.JSONArray;
5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
8+
import java.io.*;
9+
import java.nio.charset.StandardCharsets;
10+
11+
/**
12+
* rempli la base de donne de question
13+
*/
14+
public class DbPopulator {
15+
16+
AppDatabase appDb;
17+
Context context;
18+
19+
public DbPopulator(Context context) throws IOException, JSONException {
20+
this.context = context;
21+
appDb = AppDatabase.getInstance(context);
22+
populateDbFromJson();
23+
}
24+
25+
/**
26+
* Ajoute les questions dans la bdd depuis un json
27+
* @throws IOException
28+
* @throws JSONException
29+
*/
30+
public void populateDbFromJson() throws IOException, JSONException {
31+
32+
//AppDatabase appDb = AppDatabase.getInstance(context);
33+
appDb.questionDao().clearQuestions();
34+
InputStream is = context.getResources().openRawResource(R.raw.questions);
35+
Writer writer = new StringWriter();
36+
char[] buffer = new char[1024];
37+
try {
38+
Reader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
39+
int n;
40+
while ((n = reader.read(buffer)) != -1) {
41+
writer.write(buffer, 0, n);
42+
}
43+
} catch (IOException e) {
44+
e.printStackTrace();
45+
} finally {
46+
is.close();
47+
}
48+
49+
JSONObject mainJSObject = new JSONObject(writer.toString());
50+
JSONArray questionsJsonObject = mainJSObject.getJSONArray("questions");
51+
52+
Question question;
53+
54+
for (int i = 0; i < questionsJsonObject.length(); i++) {
55+
JSONObject questionObj = questionsJsonObject.getJSONObject(i);
56+
57+
question = new Question();
58+
question.setNumero(questionObj.getInt("num"));
59+
question.setQuestion(questionObj.getString("question"));
60+
61+
JSONArray propositionArray = questionObj.getJSONArray("propositions");
62+
for (int j = 0; j < propositionArray.length(); j++) {
63+
question.addProposition(propositionArray.getString(j));
64+
}
65+
66+
question.setReponse(questionObj.getInt("reponse"));
67+
question.setThemeID(questionObj.getInt("themeNum"));
68+
question.setCommentaire(questionObj.getString("commentaire"));
69+
question.setCoursUrl(questionObj.getString("cours"));
70+
71+
//question.demo();
72+
73+
appDb.questionDao().insertQuestion(question);
74+
}
75+
76+
//appDb.close();
77+
78+
}
79+
}

app/src/main/java/dev/favier/exam1radioamateur/Examen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public ResultCalculator getResults(){
7373
* @throws IOException
7474
* @throws JSONException
7575
*/
76-
public void populateDbFromJson() throws IOException, JSONException {
76+
/*public void populateDbFromJson() throws IOException, JSONException {
7777
7878
//AppDatabase appDb = AppDatabase.getInstance(context);
7979
appDb.questionDao().clearQuestions();
@@ -121,7 +121,7 @@ public void populateDbFromJson() throws IOException, JSONException {
121121
122122
//appDb.close();
123123
124-
}
124+
}*/
125125

126126
/**
127127
* Genène les question de l'examen aléatoirement

app/src/main/java/dev/favier/exam1radioamateur/ExamenActivity.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import java.util.Timer;
2323
import java.util.TimerTask;
2424

25+
/**
26+
* Affiche l'interface des question de l'examen
27+
*/
2528
public class ExamenActivity extends AppCompatActivity {
2629

2730
Button delRespButton, coursQButton, reponseQButton, prevQButton, nextQButton;
@@ -39,6 +42,7 @@ public class ExamenActivity extends AppCompatActivity {
3942
String coursUrl;
4043
boolean firstRun;
4144
boolean examTimerEnable;
45+
boolean showResponces;
4246
int examTime;
4347
long timeLeft = 0;
4448
long timeSpent = 0;
@@ -57,18 +61,18 @@ protected void onCreate(Bundle savedInstanceState) {
5761
firstRun = bundle.getBoolean("firstRun");
5862
examTimerEnable = bundle.getBoolean("examTimerEnable");
5963
examTime = bundle.getInt("timer");
64+
showResponces = bundle.getBoolean("showResponces");
6065

6166
examen = new Examen(this, themeList, numberOfQuestionParTheme, malusEnable);
6267
setupControls();
6368

6469
indexQuestion = 0;
65-
// TODO: populate db on fist start
6670
AsyncTask.execute(new Runnable() {
6771
@Override
6872
public void run() {
6973
if (firstRun) {
7074
try {
71-
Log.w("debug", "populate db");
75+
Log.i("debug", "populate db");
7276
examen.populateDbFromJson();
7377
} catch (IOException | JSONException e) {
7478
e.printStackTrace();
@@ -87,6 +91,9 @@ public void run() {
8791

8892
}
8993

94+
/**
95+
* register layout variable and buttons event
96+
*/
9097
private void setupControls() {
9198
delRespButton = findViewById(R.id.delRespButton);
9299
coursQButton = findViewById(R.id.coursQButton);
@@ -106,7 +113,11 @@ private void setupControls() {
106113
commentTextView = findViewById(R.id.commentTextView);
107114
timerTextView = findViewById(R.id.timerTextView);
108115

109-
// setup timer
116+
// desactivate showResponces fx
117+
if(!showResponces){
118+
reponseQButton.setEnabled(false);
119+
}
120+
// setup event time limit
110121
if (examTimerEnable) {
111122

112123
//Log.w("debug", String.valueOf(timerInMillis) + "timer time");
@@ -125,6 +136,7 @@ public void onFinish() {
125136
};
126137
countDownTimer.start();
127138
}
139+
// setup timer of exam
128140
timer = new Timer();
129141
timer.schedule(new TimerTask() {
130142
@Override
@@ -199,6 +211,9 @@ public void onClick(View v) {
199211
checkEnableBtn();
200212
}
201213

214+
/**
215+
* active/désactive les btn dans le cas de la première/derniere question
216+
*/
202217
private void checkEnableBtn() {
203218
// enable/disable btn
204219
if (indexQuestion == indexMaxQuestion) {
@@ -213,6 +228,9 @@ private void checkEnableBtn() {
213228
}
214229
}
215230

231+
/**
232+
* Affiche le commentaire et la réponse à la question
233+
*/
216234
private void printResponse() {
217235
Question currentQuestion = examen.getQuestion(indexQuestion);
218236
if (!currentQuestion.getCommentaire().equals("null")) {
@@ -229,7 +247,7 @@ private void printResponse() {
229247
propo3RadioButton.setTextColor(Color.RED);
230248
propo4RadioButton.setTextColor(Color.RED);
231249

232-
Log.w("debug", String.valueOf(currentQuestion.getReponse()));
250+
//Log.w("debug", String.valueOf(currentQuestion.getReponse()));
233251
switch (currentQuestion.getReponse()) {
234252
case 0:
235253
propo1RadioButton.setTextColor(ContextCompat.getColor(getBaseContext(), R.color.colorOk));
@@ -248,6 +266,9 @@ private void printResponse() {
248266
examen.setReponseAsked(indexQuestion);
249267
}
250268

269+
/**
270+
* Affiche la question
271+
*/
251272
private void showQuestion() {
252273
Question currentQuestion = examen.getQuestion(indexQuestion);
253274

@@ -369,11 +390,20 @@ private void showQuestion() {
369390
}
370391
}
371392

393+
/**
394+
* boutton vers le cours sur internet de f6kgl
395+
* @param view
396+
*/
372397
public void gotoCours(View view) {
373398
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(coursUrl));
374399
startActivity(browserIntent);
375400
}
376401

402+
/**
403+
* ajoute le menu top bar
404+
* @param menu
405+
* @return
406+
*/
377407
@Override
378408
public boolean onCreateOptionsMenu(Menu menu) {
379409
// setup top Menu
@@ -382,6 +412,11 @@ public boolean onCreateOptionsMenu(Menu menu) {
382412
return true;
383413
}
384414

415+
/**
416+
* envenment top bar
417+
* @param item
418+
* @return
419+
*/
385420
@Override
386421
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
387422
switch (item.getItemId()) {
@@ -392,12 +427,14 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
392427
return super.onOptionsItemSelected(item);
393428
}
394429

430+
/**
431+
* fini l'examen et envois vers la page des résultats
432+
*/
395433
public void stopExam() {
396434
if(examTimerEnable){
397435
countDownTimer.cancel();
398436
}
399-
400-
Log.w("debug", "examen terminé");
437+
//Log.w("debug", "examen terminé");
401438
Intent intent = new Intent(getBaseContext(), ExamenResults.class);
402439
intent.putExtra("exam", examen.getResults());
403440
intent.putExtra("timeSpent", timeSpent);

app/src/main/java/dev/favier/exam1radioamateur/ExamenResults.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
import android.content.Intent;
44
import android.graphics.Color;
55
import android.util.Log;
6-
//import android.util.TypedValue;
76
import android.util.TypedValue;
87
import android.view.*;
9-
import android.widget.LinearLayout;
108
import android.widget.TextView;
11-
//import android.widget.LinearLayout.LayoutParams;
129
import androidx.annotation.NonNull;
1310
import androidx.appcompat.app.AppCompatActivity;
1411
import android.os.Bundle;
@@ -116,6 +113,7 @@ private void printResults() {
116113
PieDataSet pieDataSet = new PieDataSet(dataVals, "");
117114
pieDataSet.setColors(colorArray);
118115
PieData pieData = new PieData(pieDataSet);
116+
pieData.setValueTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getApplicationContext().getResources().getDisplayMetrics()));
119117
pieChart.setData(pieData);
120118
pieChart.setDrawEntryLabels(false);
121119
pieChart.getDescription().setEnabled(false);
@@ -182,4 +180,10 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
182180
}
183181
return super.onOptionsItemSelected(item);
184182
}
183+
184+
@Override
185+
public void onBackPressed() {
186+
// empeche de retouner au question d'examens
187+
//super.onBackPressed();
188+
}
185189
}

app/src/main/java/dev/favier/exam1radioamateur/MainActivity.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected void onResume() {
5454
super.onResume();
5555
if (sharedPref.getBoolean("firstrun", true)) {
5656
// first run
57-
Log.w("debug", "first run!");
57+
Log.i("debug", "first run!");
5858
firstRun = true;
5959
sharedPref.edit().putBoolean("firstrun", false).commit();
6060
} else {
@@ -255,6 +255,7 @@ public void onClick(View v) {
255255
resistancesCouleursCheckBox.setChecked(true);
256256
electriciteCheckBox.setChecked(true);
257257
condoBobCheckBox.setChecked(true);
258+
updateNbrofQSpinner();
258259
}
259260
});
260261

@@ -284,6 +285,7 @@ public void onClick(View v) {
284285
resistancesCouleursCheckBox.setChecked(false);
285286
electriciteCheckBox.setChecked(false);
286287
condoBobCheckBox.setChecked(false);
288+
updateNbrofQSpinner();
287289
}
288290
});
289291

@@ -378,6 +380,7 @@ public void onClick(View v) {
378380
Intent intent = new Intent(getBaseContext(), ExamenActivity.class);
379381
intent.putIntegerArrayListExtra("ThemeList", ThemeList);
380382
intent.putExtra("malusEnable", malusEnable);
383+
intent.putExtra("showResponces", showResponces);
381384
intent.putExtra("numberOfQuestions", Integer.parseInt(nbrQSpinner.getSelectedItem().toString()));
382385
intent.putExtra("examTimerEnable", timerEnable);
383386
intent.putExtra("timer", examTime);
@@ -456,7 +459,7 @@ public void updateNbrofQSpinner(View view) {
456459
themeRegistered++;
457460
}
458461

459-
Log.w("debug", String.valueOf(themeRegistered) + " themes sont cochés");
462+
//Log.w("debug", String.valueOf(themeRegistered) + " themes sont cochés");
460463
if(themeRegistered == 3){
461464
themeRegistered = 6;
462465
}
@@ -536,7 +539,7 @@ public void updateNbrofQSpinner() {
536539
themeRegistered++;
537540
}
538541

539-
Log.w("debug", String.valueOf(themeRegistered) + " themes sont cochés");
542+
//Log.w("debug", String.valueOf(themeRegistered) + " themes sont cochés");
540543
if(themeRegistered == 3){
541544
themeRegistered = 6;
542545
}

app/src/main/java/dev/favier/exam1radioamateur/Question.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ public void setPropositionsJson(String propositionsJson) {
191191
this.propositionsJson = propositionsJson;
192192
}
193193

194-
public void demo() {
194+
/*public void demo() {
195195
Log.w("debug", numero + "-" + question);
196-
}
196+
}*/
197197

198198
public void setReponseAsked(boolean reponseAsked) {
199199
this.reponseAsked = reponseAsked;

app/src/main/java/dev/favier/exam1radioamateur/QuestionViewer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
import java.util.ArrayList;
1616

17+
/**
18+
* Affiche une des question via {@link ExamenResults}
19+
*/
1720
public class QuestionViewer extends AppCompatActivity {
1821

1922
TextView themeViewerTextView, commentViewTextView, questionIdViewTextView;
@@ -34,6 +37,9 @@ protected void onCreate(Bundle savedInstanceState) {
3437
printQuestion();
3538
}
3639

40+
/**
41+
* configuration des variable du layout
42+
*/
3743
private void setupControls(){
3844
themeViewerTextView = findViewById(R.id.themeViewerTextView);
3945
questionIdViewTextView = findViewById(R.id.questionIdViewTextView);
@@ -46,6 +52,9 @@ private void setupControls(){
4652
coursViewerButton = findViewById(R.id.coursViewerButton);
4753
}
4854

55+
/**
56+
* Affichage de la question
57+
*/
4958
private void printQuestion(){
5059
//set theme
5160
switch (question.getThemeID()){

0 commit comments

Comments
 (0)