add favorite list to home screen
This commit is contained in:
47
app/src/main/java/org/mosad/teapod/util/CacheHelper.kt
Normal file
47
app/src/main/java/org/mosad/teapod/util/CacheHelper.kt
Normal file
@ -0,0 +1,47 @@
|
||||
package org.mosad.teapod.util
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* myList should be saved in a db
|
||||
*/
|
||||
object CacheHelper {
|
||||
|
||||
private const val fileNameMyList = "my_list.json"
|
||||
|
||||
val myList = ArrayList<String>() // a list of saved links
|
||||
|
||||
fun load(context: Context) {
|
||||
val file = File(context.filesDir, fileNameMyList)
|
||||
|
||||
if (!file.exists()) runBlocking { saveMyList(context).join() }
|
||||
|
||||
myList.clear()
|
||||
myList.addAll(
|
||||
GsonBuilder().create().fromJson(file.readText(), ArrayList<String>().javaClass)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fun saveMyList(context: Context): Job {
|
||||
val file = File(context.filesDir, fileNameMyList)
|
||||
|
||||
return GlobalScope.launch(Dispatchers.IO) {
|
||||
file.writeText(Gson().toJson(myList))
|
||||
}
|
||||
}
|
||||
|
||||
private fun save(file: File, text: String) {
|
||||
try {
|
||||
file.writeText(text)
|
||||
} catch (ex: Exception) {
|
||||
Log.e(javaClass.name, "failed to write file \"${file.absoluteFile}\"", ex)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -11,9 +11,8 @@ import kotlinx.android.synthetic.main.item_media.view.*
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.util.Media
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class MediaItemAdapter(private val media: ArrayList<Media>) : RecyclerView.Adapter<MediaItemAdapter.ViewHolder>(), Filterable {
|
||||
class MediaItemAdapter(private val media: List<Media>) : RecyclerView.Adapter<MediaItemAdapter.ViewHolder>(), Filterable {
|
||||
|
||||
var onItemClick: ((Media, Int) -> Unit)? = null
|
||||
private val filter = MediaFilter()
|
||||
@ -72,7 +71,7 @@ class MediaItemAdapter(private val media: ArrayList<Media>) : RecyclerView.Adapt
|
||||
* suppressing unchecked cast is safe, since we only use Media
|
||||
*/
|
||||
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
|
||||
filteredMedia = results?.values as java.util.ArrayList<Media>
|
||||
filteredMedia = results?.values as List<Media>
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user