Implements extremely basic file reading
This commit is contained in:
parent
c45d852aa0
commit
04165e0068
2 changed files with 56 additions and 1 deletions
|
|
@ -1,19 +1,50 @@
|
|||
package com.dnsc.plaindo
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import com.dnsc.plaindo.data.TodoFileRepository
|
||||
import com.dnsc.plaindo.ui.theme.PlaindoTheme
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private lateinit var todoFileRepository: TodoFileRepository
|
||||
private lateinit var openDocumentLauncher: ActivityResultLauncher<Array<String>>
|
||||
private val TAG = "MainActivity"
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
// Set up dependencies
|
||||
todoFileRepository = TodoFileRepository(this)
|
||||
|
||||
// Draw content
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
PlaindoTheme {
|
||||
PlaindoApp()
|
||||
}
|
||||
}
|
||||
|
||||
// Handle init tasks
|
||||
openDocumentLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.OpenDocument()
|
||||
) { uri ->
|
||||
if (uri != null) {
|
||||
// TODO: Store uri in shared preferences
|
||||
// TODO: Persist access to URI
|
||||
todoFileRepository.load(uri)
|
||||
} else {
|
||||
// TODO: Handle user cancelled selection
|
||||
Log.d(TAG, "No document selected")
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Move to / listen to button click for this function
|
||||
openDocumentLauncher.launch(arrayOf("text/plain"))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
package com.dnsc.plaindo.data
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
|
||||
class TodoFileRepository(val context: Context) {
|
||||
fun load(uri: Uri) {
|
||||
context.contentResolver.openInputStream(uri)?.use { inputStream ->
|
||||
BufferedReader(InputStreamReader(inputStream)).use { reader ->
|
||||
var line: String? = reader.readLine()
|
||||
while (line != null) {
|
||||
Log.d("Plaindo", line)
|
||||
|
||||
// TODO: Add task to data structure
|
||||
|
||||
line = reader.readLine()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in a new issue