Skip to content

Commit

Permalink
+ Change db population way
Browse files Browse the repository at this point in the history
+ UI upgrades
+ bug fixes
  • Loading branch information
Maxime-Favier committed Jun 3, 2020
1 parent 7f502d5 commit ddaa8c3
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 93 deletions.
62 changes: 0 additions & 62 deletions app/src/main/java/dev/favier/exam1radioamateur/Examen.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,61 +68,6 @@ public ResultCalculator getResults(){
return new ResultCalculator(questions, themesList, nbrQuestionParTheme, malusEnable);
}

/**
* Ajoute les questions dans la bdd depuis un json
* @throws IOException
* @throws JSONException
*/
/*public void populateDbFromJson() throws IOException, JSONException {
//AppDatabase appDb = AppDatabase.getInstance(context);
appDb.questionDao().clearQuestions();
InputStream is = context.getResources().openRawResource(R.raw.questions);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
is.close();
}
JSONObject mainJSObject = new JSONObject(writer.toString());
JSONArray questionsJsonObject = mainJSObject.getJSONArray("questions");
Question question;
for (int i = 0; i < questionsJsonObject.length(); i++) {
JSONObject questionObj = questionsJsonObject.getJSONObject(i);
question = new Question();
question.setNumero(questionObj.getInt("num"));
question.setQuestion(questionObj.getString("question"));
JSONArray propositionArray = questionObj.getJSONArray("propositions");
for (int j = 0; j < propositionArray.length(); j++) {
question.addProposition(propositionArray.getString(j));
}
question.setReponse(questionObj.getInt("reponse"));
question.setThemeID(questionObj.getInt("themeNum"));
question.setCommentaire(questionObj.getString("commentaire"));
question.setCoursUrl(questionObj.getString("cours"));
//question.demo();
appDb.questionDao().insertQuestion(question);
}
//appDb.close();
}*/

/**
* Genène les question de l'examen aléatoirement
*/
Expand All @@ -135,13 +80,6 @@ public void genrateQuestions() {
Log.w("debug", "nbr de questions " + String.valueOf(questions.size()));
}

/*public void debug() {
ArrayList<Question> tes = new ArrayList<>();
tes = (ArrayList<Question>) appDb.questionDao().getRandomQuestion(303, 5);
for (Question q : tes) {
q.demo();
}
}*/

/**
* defini la réponse donné par l'utilisateur d'une question de l"examen
Expand Down
33 changes: 19 additions & 14 deletions app/src/main/java/dev/favier/exam1radioamateur/ExamenActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class ExamenActivity extends AppCompatActivity {
int indexQuestion;
int indexMaxQuestion;
String coursUrl;
boolean firstRun;
boolean examTimerEnable;
boolean showResponces;
int examTime;
Expand All @@ -58,7 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
int numberOfQuestionParTheme = (indexMaxQuestion + 1) / themeList.size();

boolean malusEnable = bundle.getBoolean("malusEnable");
firstRun = bundle.getBoolean("firstRun");
//firstRun = bundle.getBoolean("firstRun");
examTimerEnable = bundle.getBoolean("examTimerEnable");
examTime = bundle.getInt("timer");
showResponces = bundle.getBoolean("showResponces");
Expand All @@ -70,15 +69,6 @@ protected void onCreate(Bundle savedInstanceState) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
if (firstRun) {
try {
Log.i("debug", "populate db");
examen.populateDbFromJson();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}

examen.genrateQuestions();
runOnUiThread(new Runnable() {
@Override
Expand Down Expand Up @@ -114,7 +104,7 @@ private void setupControls() {
timerTextView = findViewById(R.id.timerTextView);

// desactivate showResponces fx
if(!showResponces){
if (!showResponces) {
reponseQButton.setEnabled(false);
}
// setup event time limit
Expand All @@ -125,7 +115,19 @@ private void setupControls() {
//examTime * 60000, 1000
@Override
public void onTick(long millisUntilFinished) {
timerTextView.setText(String.valueOf((millisUntilFinished / 1000) / 60) + ":" + String.valueOf((int) ((millisUntilFinished / 1000) % 60)));
int min = (int) (millisUntilFinished / 1000) / 60;
int sec = (int) (millisUntilFinished / 1000) % 60;
if(min == 1 && sec == 0){
timerTextView.setTextColor(Color.WHITE);
timerTextView.setBackgroundColor(Color.RED);
}
String secStr;
if(sec <= 9){
secStr = "0" + sec;
}else {
secStr = String.valueOf(sec);
}
timerTextView.setText(String.valueOf(min) + "'" + secStr + '"');
timeLeft = millisUntilFinished;
}

Expand Down Expand Up @@ -392,6 +394,7 @@ private void showQuestion() {

/**
* boutton vers le cours sur internet de f6kgl
*
* @param view
*/
public void gotoCours(View view) {
Expand All @@ -401,6 +404,7 @@ public void gotoCours(View view) {

/**
* ajoute le menu top bar
*
* @param menu
* @return
*/
Expand All @@ -414,6 +418,7 @@ public boolean onCreateOptionsMenu(Menu menu) {

/**
* envenment top bar
*
* @param item
* @return
*/
Expand All @@ -431,7 +436,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
* fini l'examen et envois vers la page des résultats
*/
public void stopExam() {
if(examTimerEnable){
if (examTimerEnable) {
countDownTimer.cancel();
}
//Log.w("debug", "examen terminé");
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/dev/favier/exam1radioamateur/ExamenResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexboxLayout;

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

totalTempsTextView.setText(getResources().getString(R.string.temps, (int) (timeSpent / 60), (int) (timeSpent % 60)));
String sec;
if(timeSpent % 60 <= 9){
sec = "0" + (timeSpent%60);
}else {
sec = String.valueOf(timeSpent % 60);
}
totalTempsTextView.setText(getResources().getString(R.string.temps, (int) (timeSpent / 60), sec));
totalLegTextView.setText(getResources().getString(R.string.totalLeg, resultCalculator.pointsLegislation()));
totalTechTextView.setText(getResources().getString(R.string.totalTech, resultCalculator.pointsTechnique()));
totalCorrectTextView.setText(getResources().getString(R.string.corrects, resultCalculator.getNbrOfCorrect()));
Expand Down Expand Up @@ -130,14 +137,13 @@ private void printQuestions() {
final Question question = resultCalculator.getQuestion(i);

TextView textView = new TextView(this);
//textView.setId(i);

FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(px, px);

layoutParams.setMargins(marginpx, marginTppx, marginpx, 0);
textView.setLayoutParams(layoutParams);
textView.setText(String.valueOf(i + 1));
textView.setTextColor(Color.WHITE);

textView.setContentDescription(question.getQuestion());
textView.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -183,7 +189,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {

@Override
public void onBackPressed() {
// empeche de retouner au question d'examens
// empeche de retouner aux question d'examens
//super.onBackPressed();
}
}
33 changes: 26 additions & 7 deletions app/src/main/java/dev/favier/exam1radioamateur/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import android.os.Bundle;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

ArrayList<Integer> ThemeList;
SharedPreferences sharedPref = null;
boolean firstRun = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -54,12 +55,31 @@ protected void onResume() {
super.onResume();
if (sharedPref.getBoolean("firstrun", true)) {
// first run
Log.i("debug", "first run!");
firstRun = true;
Log.w("debug", "first run!");
startButton.setEnabled(false);
startButton.setText(R.string.chargementEnCours);
AsyncTask.execute(new Runnable() {
@Override
public void run() {
//Log.w("debug", "populate start");
try {
DbPopulator dbPopulator = new DbPopulator(getApplicationContext());
} catch (IOException | JSONException e) {
e.printStackTrace();
}
//Log.w("debug", "populate end");
runOnUiThread(new Runnable() {
@Override
public void run() {
startButton.setEnabled(true);
startButton.setText(R.string.commencer);
}
});
}
});
sharedPref.edit().putBoolean("firstrun", false).commit();
} else {
firstRun = false;
}

}

private void setupControls() {
Expand Down Expand Up @@ -100,6 +120,7 @@ private void setupControls() {
resistancesCouleursCheckBox = findViewById(R.id.resistancesCouleursCheckBox);
electriciteCheckBox = findViewById(R.id.electriciteCheckBox);
condoBobCheckBox = findViewById(R.id.condoBobCheckBox);
Log.w("debug", "setup controls");


// dropdown prefs
Expand Down Expand Up @@ -384,8 +405,6 @@ public void onClick(View v) {
intent.putExtra("numberOfQuestions", Integer.parseInt(nbrQSpinner.getSelectedItem().toString()));
intent.putExtra("examTimerEnable", timerEnable);
intent.putExtra("timer", examTime);
intent.putExtra("firstRun", firstRun);

startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "Selectionnez un theme", Toast.LENGTH_SHORT).show();
Expand Down
Binary file modified app/src/main/res/drawable/empty_question.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 11 additions & 5 deletions app/src/main/res/layout/activity_examen_results.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,18 @@
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pieChart" android:layout_height="250sp"
android:layout_width="match_parent"/>
<com.google.android.flexbox.FlexboxLayout android:layout_height="match_parent"
android:layout_width="match_parent"
app:flexWrap="wrap" app:alignItems="center" app:alignContent="space_around"
app:flexDirection="row" android:id="@+id/flexResult">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="center_horizontal|center_vertical">
<com.google.android.flexbox.FlexboxLayout android:layout_height="match_parent"
android:layout_width="match_parent"
app:flexWrap="wrap" app:alignItems="center"
app:alignContent="space_around"
app:flexDirection="row" android:id="@+id/flexResult">

</com.google.android.flexbox.FlexboxLayout>
</com.google.android.flexbox.FlexboxLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="tps_lim">Temps limite</string>
<string name="min">min</string>
<string name="num20">20</string>
<string name="chargementEnCours">Chargement en cours…</string>

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

Expand Down Expand Up @@ -111,7 +112,7 @@
<string name="total">Total : %1$d / %2$d</string>
<string name="totalLeg">Total legislation : %1$d</string>
<string name="totalTech">Total technique : %1$d</string>
<string name="temps">Temps : %1$d: %2$d</string>
<string name="temps">Temps : %1$d\' %2$s"</string>
<string name="corrects">Corrects : %1$d</string>
<string name="incorrects">Incorrect : %1$d</string>
<string name="ss_Rep">Sans réponse : %1$d</string>
Expand Down

0 comments on commit ddaa8c3

Please sign in to comment.