Skip to content

Commit ddaa8c3

Browse files
committed
+ Change db population way
+ UI upgrades + bug fixes
1 parent 7f502d5 commit ddaa8c3

File tree

7 files changed

+68
-93
lines changed

7 files changed

+68
-93
lines changed

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

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -68,61 +68,6 @@ public ResultCalculator getResults(){
6868
return new ResultCalculator(questions, themesList, nbrQuestionParTheme, malusEnable);
6969
}
7070

71-
/**
72-
* Ajoute les questions dans la bdd depuis un json
73-
* @throws IOException
74-
* @throws JSONException
75-
*/
76-
/*public void populateDbFromJson() throws IOException, JSONException {
77-
78-
//AppDatabase appDb = AppDatabase.getInstance(context);
79-
appDb.questionDao().clearQuestions();
80-
InputStream is = context.getResources().openRawResource(R.raw.questions);
81-
Writer writer = new StringWriter();
82-
char[] buffer = new char[1024];
83-
try {
84-
Reader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
85-
int n;
86-
while ((n = reader.read(buffer)) != -1) {
87-
writer.write(buffer, 0, n);
88-
}
89-
} catch (IOException e) {
90-
e.printStackTrace();
91-
} finally {
92-
is.close();
93-
}
94-
95-
JSONObject mainJSObject = new JSONObject(writer.toString());
96-
JSONArray questionsJsonObject = mainJSObject.getJSONArray("questions");
97-
98-
Question question;
99-
100-
for (int i = 0; i < questionsJsonObject.length(); i++) {
101-
JSONObject questionObj = questionsJsonObject.getJSONObject(i);
102-
103-
question = new Question();
104-
question.setNumero(questionObj.getInt("num"));
105-
question.setQuestion(questionObj.getString("question"));
106-
107-
JSONArray propositionArray = questionObj.getJSONArray("propositions");
108-
for (int j = 0; j < propositionArray.length(); j++) {
109-
question.addProposition(propositionArray.getString(j));
110-
}
111-
112-
question.setReponse(questionObj.getInt("reponse"));
113-
question.setThemeID(questionObj.getInt("themeNum"));
114-
question.setCommentaire(questionObj.getString("commentaire"));
115-
question.setCoursUrl(questionObj.getString("cours"));
116-
117-
//question.demo();
118-
119-
appDb.questionDao().insertQuestion(question);
120-
}
121-
122-
//appDb.close();
123-
124-
}*/
125-
12671
/**
12772
* Genène les question de l'examen aléatoirement
12873
*/
@@ -135,13 +80,6 @@ public void genrateQuestions() {
13580
Log.w("debug", "nbr de questions " + String.valueOf(questions.size()));
13681
}
13782

138-
/*public void debug() {
139-
ArrayList<Question> tes = new ArrayList<>();
140-
tes = (ArrayList<Question>) appDb.questionDao().getRandomQuestion(303, 5);
141-
for (Question q : tes) {
142-
q.demo();
143-
}
144-
}*/
14583

14684
/**
14785
* defini la réponse donné par l'utilisateur d'une question de l"examen

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public class ExamenActivity extends AppCompatActivity {
4040
int indexQuestion;
4141
int indexMaxQuestion;
4242
String coursUrl;
43-
boolean firstRun;
4443
boolean examTimerEnable;
4544
boolean showResponces;
4645
int examTime;
@@ -58,7 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
5857
int numberOfQuestionParTheme = (indexMaxQuestion + 1) / themeList.size();
5958

6059
boolean malusEnable = bundle.getBoolean("malusEnable");
61-
firstRun = bundle.getBoolean("firstRun");
60+
//firstRun = bundle.getBoolean("firstRun");
6261
examTimerEnable = bundle.getBoolean("examTimerEnable");
6362
examTime = bundle.getInt("timer");
6463
showResponces = bundle.getBoolean("showResponces");
@@ -70,15 +69,6 @@ protected void onCreate(Bundle savedInstanceState) {
7069
AsyncTask.execute(new Runnable() {
7170
@Override
7271
public void run() {
73-
if (firstRun) {
74-
try {
75-
Log.i("debug", "populate db");
76-
examen.populateDbFromJson();
77-
} catch (IOException | JSONException e) {
78-
e.printStackTrace();
79-
}
80-
}
81-
8272
examen.genrateQuestions();
8373
runOnUiThread(new Runnable() {
8474
@Override
@@ -114,7 +104,7 @@ private void setupControls() {
114104
timerTextView = findViewById(R.id.timerTextView);
115105

116106
// desactivate showResponces fx
117-
if(!showResponces){
107+
if (!showResponces) {
118108
reponseQButton.setEnabled(false);
119109
}
120110
// setup event time limit
@@ -125,7 +115,19 @@ private void setupControls() {
125115
//examTime * 60000, 1000
126116
@Override
127117
public void onTick(long millisUntilFinished) {
128-
timerTextView.setText(String.valueOf((millisUntilFinished / 1000) / 60) + ":" + String.valueOf((int) ((millisUntilFinished / 1000) % 60)));
118+
int min = (int) (millisUntilFinished / 1000) / 60;
119+
int sec = (int) (millisUntilFinished / 1000) % 60;
120+
if(min == 1 && sec == 0){
121+
timerTextView.setTextColor(Color.WHITE);
122+
timerTextView.setBackgroundColor(Color.RED);
123+
}
124+
String secStr;
125+
if(sec <= 9){
126+
secStr = "0" + sec;
127+
}else {
128+
secStr = String.valueOf(sec);
129+
}
130+
timerTextView.setText(String.valueOf(min) + "'" + secStr + '"');
129131
timeLeft = millisUntilFinished;
130132
}
131133

@@ -392,6 +394,7 @@ private void showQuestion() {
392394

393395
/**
394396
* boutton vers le cours sur internet de f6kgl
397+
*
395398
* @param view
396399
*/
397400
public void gotoCours(View view) {
@@ -401,6 +404,7 @@ public void gotoCours(View view) {
401404

402405
/**
403406
* ajoute le menu top bar
407+
*
404408
* @param menu
405409
* @return
406410
*/
@@ -414,6 +418,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
414418

415419
/**
416420
* envenment top bar
421+
*
417422
* @param item
418423
* @return
419424
*/
@@ -431,7 +436,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
431436
* fini l'examen et envois vers la page des résultats
432437
*/
433438
public void stopExam() {
434-
if(examTimerEnable){
439+
if (examTimerEnable) {
435440
countDownTimer.cancel();
436441
}
437442
//Log.w("debug", "examen terminé");

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.github.mikephil.charting.data.PieData;
1717
import com.github.mikephil.charting.data.PieDataSet;
1818
import com.github.mikephil.charting.data.PieEntry;
19+
import com.google.android.flexbox.AlignItems;
1920
import com.google.android.flexbox.FlexboxLayout;
2021

2122
import java.util.ArrayList;
@@ -87,7 +88,13 @@ private void printResults() {
8788
examConcluTextView.setText(getResources().getString(R.string.exam_Conclu, resultCalculator.getNbrOfQuestions()));
8889
totalTextView.setText(getResources().getString(R.string.total, resultCalculator.pointCalculation(), resultCalculator.maxPoints()));
8990

90-
totalTempsTextView.setText(getResources().getString(R.string.temps, (int) (timeSpent / 60), (int) (timeSpent % 60)));
91+
String sec;
92+
if(timeSpent % 60 <= 9){
93+
sec = "0" + (timeSpent%60);
94+
}else {
95+
sec = String.valueOf(timeSpent % 60);
96+
}
97+
totalTempsTextView.setText(getResources().getString(R.string.temps, (int) (timeSpent / 60), sec));
9198
totalLegTextView.setText(getResources().getString(R.string.totalLeg, resultCalculator.pointsLegislation()));
9299
totalTechTextView.setText(getResources().getString(R.string.totalTech, resultCalculator.pointsTechnique()));
93100
totalCorrectTextView.setText(getResources().getString(R.string.corrects, resultCalculator.getNbrOfCorrect()));
@@ -130,14 +137,13 @@ private void printQuestions() {
130137
final Question question = resultCalculator.getQuestion(i);
131138

132139
TextView textView = new TextView(this);
133-
//textView.setId(i);
134140

135141
FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(px, px);
136-
137142
layoutParams.setMargins(marginpx, marginTppx, marginpx, 0);
138143
textView.setLayoutParams(layoutParams);
139144
textView.setText(String.valueOf(i + 1));
140145
textView.setTextColor(Color.WHITE);
146+
141147
textView.setContentDescription(question.getQuestion());
142148
textView.setOnClickListener(new View.OnClickListener() {
143149
@Override
@@ -183,7 +189,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
183189

184190
@Override
185191
public void onBackPressed() {
186-
// empeche de retouner au question d'examens
192+
// empeche de retouner aux question d'examens
187193
//super.onBackPressed();
188194
}
189195
}

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import android.os.Bundle;
1818
import com.google.gson.Gson;
1919
import com.google.gson.reflect.TypeToken;
20+
import org.json.JSONException;
2021

22+
import java.io.IOException;
2123
import java.util.ArrayList;
2224
import java.util.List;
2325

@@ -36,7 +38,6 @@ public class MainActivity extends AppCompatActivity {
3638

3739
ArrayList<Integer> ThemeList;
3840
SharedPreferences sharedPref = null;
39-
boolean firstRun = false;
4041

4142
@Override
4243
protected void onCreate(Bundle savedInstanceState) {
@@ -54,12 +55,31 @@ protected void onResume() {
5455
super.onResume();
5556
if (sharedPref.getBoolean("firstrun", true)) {
5657
// first run
57-
Log.i("debug", "first run!");
58-
firstRun = true;
58+
Log.w("debug", "first run!");
59+
startButton.setEnabled(false);
60+
startButton.setText(R.string.chargementEnCours);
61+
AsyncTask.execute(new Runnable() {
62+
@Override
63+
public void run() {
64+
//Log.w("debug", "populate start");
65+
try {
66+
DbPopulator dbPopulator = new DbPopulator(getApplicationContext());
67+
} catch (IOException | JSONException e) {
68+
e.printStackTrace();
69+
}
70+
//Log.w("debug", "populate end");
71+
runOnUiThread(new Runnable() {
72+
@Override
73+
public void run() {
74+
startButton.setEnabled(true);
75+
startButton.setText(R.string.commencer);
76+
}
77+
});
78+
}
79+
});
5980
sharedPref.edit().putBoolean("firstrun", false).commit();
60-
} else {
61-
firstRun = false;
6281
}
82+
6383
}
6484

6585
private void setupControls() {
@@ -100,6 +120,7 @@ private void setupControls() {
100120
resistancesCouleursCheckBox = findViewById(R.id.resistancesCouleursCheckBox);
101121
electriciteCheckBox = findViewById(R.id.electriciteCheckBox);
102122
condoBobCheckBox = findViewById(R.id.condoBobCheckBox);
123+
Log.w("debug", "setup controls");
103124

104125

105126
// dropdown prefs
@@ -384,8 +405,6 @@ public void onClick(View v) {
384405
intent.putExtra("numberOfQuestions", Integer.parseInt(nbrQSpinner.getSelectedItem().toString()));
385406
intent.putExtra("examTimerEnable", timerEnable);
386407
intent.putExtra("timer", examTime);
387-
intent.putExtra("firstRun", firstRun);
388-
389408
startActivity(intent);
390409
} else {
391410
Toast.makeText(MainActivity.this, "Selectionnez un theme", Toast.LENGTH_SHORT).show();
7.63 KB
Loading

app/src/main/res/layout/activity_examen_results.xml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,18 @@
7777
<com.github.mikephil.charting.charts.PieChart
7878
android:id="@+id/pieChart" android:layout_height="250sp"
7979
android:layout_width="match_parent"/>
80-
<com.google.android.flexbox.FlexboxLayout android:layout_height="match_parent"
81-
android:layout_width="match_parent"
82-
app:flexWrap="wrap" app:alignItems="center" app:alignContent="space_around"
83-
app:flexDirection="row" android:id="@+id/flexResult">
80+
<LinearLayout
81+
android:orientation="vertical"
82+
android:layout_width="match_parent"
83+
android:layout_height="match_parent" android:gravity="center_horizontal|center_vertical">
84+
<com.google.android.flexbox.FlexboxLayout android:layout_height="match_parent"
85+
android:layout_width="match_parent"
86+
app:flexWrap="wrap" app:alignItems="center"
87+
app:alignContent="space_around"
88+
app:flexDirection="row" android:id="@+id/flexResult">
8489

85-
</com.google.android.flexbox.FlexboxLayout>
90+
</com.google.android.flexbox.FlexboxLayout>
91+
</LinearLayout>
8692
</LinearLayout>
8793
</ScrollView>
8894
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<string name="tps_lim">Temps limite</string>
4242
<string name="min">min</string>
4343
<string name="num20">20</string>
44+
<string name="chargementEnCours">Chargement en cours…</string>
4445

4546
<string name="numQ_invalid">Entrez un nombre de question valide</string>
4647

@@ -111,7 +112,7 @@
111112
<string name="total">Total : %1$d / %2$d</string>
112113
<string name="totalLeg">Total legislation : %1$d</string>
113114
<string name="totalTech">Total technique : %1$d</string>
114-
<string name="temps">Temps : %1$d: %2$d</string>
115+
<string name="temps">Temps : %1$d\' %2$s"</string>
115116
<string name="corrects">Corrects : %1$d</string>
116117
<string name="incorrects">Incorrect : %1$d</string>
117118
<string name="ss_Rep">Sans réponse : %1$d</string>

0 commit comments

Comments
 (0)