wip: reading file from either local or web
need-to: parse a file - we may need to decide on a format?
This commit is contained in:
parent
e7f88752ff
commit
d0ebae4566
3 changed files with 47 additions and 3 deletions
|
@ -2,6 +2,8 @@
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".MemoApplication"
|
android:name=".MemoApplication"
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
|
|
@ -49,6 +49,9 @@ import fr.uparis.diamantkennel.memorisationapplication.ui.ActionHome
|
||||||
import fr.uparis.diamantkennel.memorisationapplication.ui.ActionImport
|
import fr.uparis.diamantkennel.memorisationapplication.ui.ActionImport
|
||||||
import fr.uparis.diamantkennel.memorisationapplication.ui.ErrorsAjout
|
import fr.uparis.diamantkennel.memorisationapplication.ui.ErrorsAjout
|
||||||
import fr.uparis.diamantkennel.memorisationapplication.ui.HomeViewModel
|
import fr.uparis.diamantkennel.memorisationapplication.ui.HomeViewModel
|
||||||
|
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||||
|
import kotlinx.coroutines.GlobalScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
@ -210,7 +213,7 @@ fun CreationDialog(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class, DelicateCoroutinesApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun ImportDialog(
|
fun ImportDialog(
|
||||||
dismiss: () -> Unit,
|
dismiss: () -> Unit,
|
||||||
|
@ -289,7 +292,7 @@ fun ImportDialog(
|
||||||
confirmButton = {
|
confirmButton = {
|
||||||
Button(
|
Button(
|
||||||
// TODO : on charge le jeu de question dans le sujet
|
// TODO : on charge le jeu de question dans le sujet
|
||||||
onClick = { model.import(lien) },
|
onClick = { GlobalScope.launch { model.import(lien) } },
|
||||||
content = { Text("Importer") }
|
content = { Text("Importer") }
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
|
@ -8,7 +8,14 @@ import androidx.lifecycle.viewModelScope
|
||||||
import fr.uparis.diamantkennel.memorisationapplication.MemoApplication
|
import fr.uparis.diamantkennel.memorisationapplication.MemoApplication
|
||||||
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.flowOn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import java.io.BufferedReader
|
||||||
|
import java.io.File
|
||||||
|
import java.io.InputStreamReader
|
||||||
|
import java.net.HttpURLConnection
|
||||||
|
import java.net.URL
|
||||||
|
|
||||||
class HomeViewModel(application: Application) : AndroidViewModel(application) {
|
class HomeViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
private val dao = (application as MemoApplication).database.memoDao()
|
private val dao = (application as MemoApplication).database.memoDao()
|
||||||
|
@ -129,7 +136,39 @@ class HomeViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
error.value = null
|
error.value = null
|
||||||
}
|
}
|
||||||
|
|
||||||
fun import(path: String) {
|
suspend fun import(path: String) {
|
||||||
|
val data =
|
||||||
|
flow {
|
||||||
|
if (path.startsWith("content://")) {
|
||||||
|
// Local file
|
||||||
|
val bufferedReader: BufferedReader = File(path).bufferedReader()
|
||||||
|
|
||||||
|
emit(bufferedReader.use { it.readText() })
|
||||||
|
} else {
|
||||||
|
// File from internet
|
||||||
|
val url = URL(path)
|
||||||
|
val connection = url.openConnection() as HttpURLConnection
|
||||||
|
connection.requestMethod = "GET"
|
||||||
|
|
||||||
|
val inputStream = connection.inputStream
|
||||||
|
val reader = BufferedReader(InputStreamReader(inputStream))
|
||||||
|
|
||||||
|
var content = ""
|
||||||
|
var line: String? = reader.readLine()
|
||||||
|
while (line != null) {
|
||||||
|
content += line
|
||||||
|
line = reader.readLine()
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.disconnect()
|
||||||
|
|
||||||
|
emit(content)
|
||||||
|
}
|
||||||
|
}.flowOn(Dispatchers.IO)
|
||||||
|
|
||||||
dismissImportation()
|
dismissImportation()
|
||||||
|
|
||||||
|
// `data`
|
||||||
|
// Correspond au contenu du fichier qu'il faut ajouter à la bdd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue