I implemented LiveData & ViewModel to mimic AsyncTaskLoader.
I load file names from the camera directory in DCIM, and then i attach a fileObserver to Observe when a
I have here an example @Micklo_Nerd but it does not use your problem of getting the files deleted but it gives you the idea for what you need to do. In my example the user insert a name and after clicking a button, the list is changed.
activity_main.xml
MyRepository (In your example is the MyLiveData)
In here you must do the work of getting the filenames in the folder, and put the in the MutableLiveData.
class MyRepository {
fun loadFileNames(liveData : MutableLiveData>, filename: String){
var fileList : MutableList? = liveData.value
if(test == null)
fileList = MutableList(1){ filename }
else
fileList.add(filename)
liveData.value = fileList
}
}
MyViewModel
In here, I have two methods: one to update the list as I click the button and another to get the list of file names. You should probably only need the one that gets the list
class MyViewModel : ViewModel() {
val repo: MyRepository
var mutableLiveData : MutableLiveData>
init {
repo = MyRepository()
mutableLiveData = MutableLiveData()
}
fun changeList(filename: String){
repo.loadFileNames(mutableLiveData, filename)
}
fun getFileList() : MutableLiveData>{
return mutableLiveData
}
}
MainActivity
In here, you see I am observing the method that returns the list of filenames, which is what you need to do, because that is what is going to change.
class MainActivity : AppCompatActivity(), View.OnClickListener {
private val TAG = "MyFragment"
private lateinit var viewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProviders.of(this).get(MyViewModel::class.java)
viewModel.getFileList().observe(this, Observer> { fileNames ->
Log.d(TAG, "New Live Data Dispatch")
textView.text = ""
for ((index, name) in fileNames!!.withIndex()) {
textView.append("the element at $index is $name\n")
}
})
buttonAdd.setOnClickListener(this)
}
override fun onClick(v: View?) {
when(v!!.id){
R.id.buttonAdd -> {
viewModel.changeList(filename.text.toString())
filename.text.clear()
}
}
}
}
Hope this helps.