add import

This commit is contained in:
Mylloon 2023-12-19 19:24:26 +01:00
parent d0ebae4566
commit 337f5c6342
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 55 additions and 20 deletions

View file

@ -222,6 +222,7 @@ fun ImportDialog(
val (selectedOption, onOptionSelected) = remember { mutableStateOf(ActionImport.FILE) } val (selectedOption, onOptionSelected) = remember { mutableStateOf(ActionImport.FILE) }
var lien by remember { mutableStateOf("") } var lien by remember { mutableStateOf("") }
var ctx = LocalContext.current
val filePickerLauncher = rememberLauncherForActivityResult( val filePickerLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.StartActivityForResult() ActivityResultContracts.StartActivityForResult()
@ -291,8 +292,7 @@ fun ImportDialog(
}, },
confirmButton = { confirmButton = {
Button( Button(
// TODO : on charge le jeu de question dans le sujet onClick = { GlobalScope.launch { model.import(ctx, lien) } },
onClick = { GlobalScope.launch { model.import(lien) } },
content = { Text("Importer") } content = { Text("Importer") }
) )
}, },

View file

@ -10,7 +10,10 @@ import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface MemoDao { interface MemoDao {
@Insert @Insert
suspend fun insert(set: SetQuestions) suspend fun insert(set: SetQuestions): Long
@Insert
suspend fun insertQuestion(question: Question)
@Transaction @Transaction
@Query("SELECT * FROM SetQuestions") @Query("SELECT * FROM SetQuestions")

View file

@ -1,3 +1,5 @@
package fr.uparis.diamantkennel.memorisationapplication.ui package fr.uparis.diamantkennel.memorisationapplication.ui
enum class ErrorsAjout { BAD_ENTRY, DUPLICATE } enum class ErrorsAjout { BAD_ENTRY, DUPLICATE }
class Duplicate : Exception(ErrorsAjout.DUPLICATE.toString())

View file

@ -1,18 +1,22 @@
package fr.uparis.diamantkennel.memorisationapplication.ui package fr.uparis.diamantkennel.memorisationapplication.ui
import android.app.Application import android.app.Application
import android.content.Context
import android.database.sqlite.SQLiteConstraintException import android.database.sqlite.SQLiteConstraintException
import android.net.Uri
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import fr.uparis.diamantkennel.memorisationapplication.MemoApplication import fr.uparis.diamantkennel.memorisationapplication.MemoApplication
import fr.uparis.diamantkennel.memorisationapplication.data.Question
import fr.uparis.diamantkennel.memorisationapplication.data.SetQuestions import fr.uparis.diamantkennel.memorisationapplication.data.SetQuestions
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.single
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.json.JSONObject
import java.io.BufferedReader import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader import java.io.InputStreamReader
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL import java.net.URL
@ -52,13 +56,10 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
return return
} }
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
try { try {
dao.insert( dao.insert(SetQuestions(name = sujet.value.trim()))
SetQuestions(
name = sujet.value.trim()
)
)
} catch (_: SQLiteConstraintException) { } catch (_: SQLiteConstraintException) {
error.value = ErrorsAjout.DUPLICATE error.value = ErrorsAjout.DUPLICATE
} }
@ -136,14 +137,23 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
error.value = null error.value = null
} }
suspend fun import(path: String) { suspend fun import(ctx: Context, path: String) {
val data = val data =
flow { flow {
if (path.startsWith("content://")) { if (path.startsWith("content://")) {
// Local file // Local file
val bufferedReader: BufferedReader = File(path).bufferedReader() val reader =
BufferedReader(
InputStreamReader(
ctx.contentResolver.openInputStream(
Uri.parse(
path
)
)
)
)
emit(bufferedReader.use { it.readText() }) emit(reader.use { it.readText() })
} else { } else {
// File from internet // File from internet
val url = URL(path) val url = URL(path)
@ -153,12 +163,7 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
val inputStream = connection.inputStream val inputStream = connection.inputStream
val reader = BufferedReader(InputStreamReader(inputStream)) val reader = BufferedReader(InputStreamReader(inputStream))
var content = "" val content = reader.use { it.readText() }
var line: String? = reader.readLine()
while (line != null) {
content += line
line = reader.readLine()
}
connection.disconnect() connection.disconnect()
@ -168,7 +173,32 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
dismissImportation() dismissImportation()
// `data`
// Correspond au contenu du fichier qu'il faut ajouter à la bdd /* Configuration used:
{
"name": "SetNameString",
"questions": [
{
"question": "QuestionString",
"reponse": "ResponseString"
},
...
]
} */
val json = JSONObject(data.single())
val setId = dao.insert(SetQuestions(name = json.getString("name").trim()))
val setQuestions = json.getJSONArray("questions")
for (i in 0 until setQuestions.length()) {
val questionObject = setQuestions.getJSONObject(i)
dao.insertQuestion(
Question(
setId = setId.toInt(),
enonce = questionObject.getString("question"),
reponse = questionObject.getString("reponse")
)
)
}
} }
} }