implemented a base for the file export to json

This commit is contained in:
Faraphel 2024-06-13 15:02:51 +02:00
parent e12339abb5
commit 3799233522
3 changed files with 64 additions and 4 deletions

View file

@ -17,7 +17,6 @@ class MainActivity : ComponentActivity() {
private var bwdManager: BwdManager? = null ///< the WiFi-Direct helper
private lateinit var database: TaskDatabase ///< the database manager
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

View file

@ -39,7 +39,6 @@ abstract class BaseClientApi<Entity: BaseEntity>(
*/
fun getAll(): List<Entity> {
// try to obtain the list of validations
Log.i("base-api", this.getEndpoint())
val response = client.get(this.getEndpoint())
// in case of error, notify it
@ -47,7 +46,6 @@ abstract class BaseClientApi<Entity: BaseEntity>(
throw HttpException(response.code)
val data = response.body.string()
Log.i("base-api", data)
// parse the list of validations
return parser.fromJson(

View file

@ -1,8 +1,13 @@
package com.faraphel.tasks_valider.ui.screen.task
import android.app.Activity
import android.app.Activity.RESULT_OK
import android.content.Intent
import android.os.Environment
import android.util.Log
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Button
import androidx.compose.material3.Text
@ -16,7 +21,10 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.faraphel.tasks_valider.connectivity.task.TaskClient
import com.faraphel.tasks_valider.connectivity.task.session.TaskRole
import com.faraphel.tasks_valider.database.entities.PersonEntity
import com.faraphel.tasks_valider.utils.parser
import java.io.File
@Composable
@ -102,12 +110,14 @@ fun TaskSessionScreen(
Button(onClick = { controller.navigate("quick_validation") }) {
Text("Quick Validation")
}
Button(onClick = { Thread { exportToFile(activity, client) }.start() }) {
Text("Export")
}
}
}
}
// TODO(Faraphel): template this function ?
fun refreshStudents(
activity: Activity,
client: TaskClient,
@ -124,3 +134,56 @@ fun refreshStudents(
}
}
}
fun exportToFile(
activity: Activity,
client: TaskClient,
) {
// get all the values to export
val allPersons = client.personApi.getAll()
val allStudents = allPersons.filter { student -> student.role == TaskRole.STUDENT }
val allRelationsStudentSessionSubject = client.relationPersonSessionSubjectApi.getAll()
val allSubjects = client.subjectApi.getAll()
val allTasks = client.taskApi.getAll()
val allValidations = client.validationApi.getAll()
// for each student
val data = allStudents.map { student ->
// fetch their subject
val relationStudentSessionObject = allRelationsStudentSessionSubject.first { relation ->
relation.studentId == student.id
}
val subject = allSubjects.first { subject -> subject.id == relationStudentSessionObject.subjectId }
// get the tasks of this person
val tasks = allTasks.filter { task -> task.subjectId == subject.id }
// for all the tasks
val studentTasksData = tasks.map { task ->
// if a validation match
val isValidated = allValidations.any { validation -> validation.taskId == task.id }
// return the pair of the task title and its validation
mapOf(
"name" to task.title,
"validated" to isValidated,
)
}
// return the name of the student associated to his data
mapOf(
"name" to student.fullName(),
"subject" to subject.name,
"tasks" to studentTasksData,
)
}
val dataJson = parser.toJson(data)
Log.i("export", dataJson)
// write all the data in a json file
val file = File(activity.filesDir, "test.json")
file.writeText(dataJson)
// TODO(Faraphel): prompt to open the file ?
}