add favorite list to home screen
This commit is contained in:
parent
a8dc243d0e
commit
474b72df49
|
@ -42,6 +42,7 @@ import org.mosad.teapod.ui.fragments.HomeFragment
|
|||
import org.mosad.teapod.ui.fragments.LibraryFragment
|
||||
import org.mosad.teapod.ui.fragments.SearchFragment
|
||||
import org.mosad.teapod.ui.fragments.LoadingFragment
|
||||
import org.mosad.teapod.util.CacheHelper
|
||||
import org.mosad.teapod.util.Media
|
||||
import org.mosad.teapod.util.TMDBApiController
|
||||
import kotlin.system.measureTimeMillis
|
||||
|
@ -111,6 +112,8 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS
|
|||
showLoginDialog(true)
|
||||
}
|
||||
|
||||
CacheHelper.load(this)
|
||||
|
||||
// TODO save last loginSuccess, if false show login dialog even if credentials are present
|
||||
// running login and list in parallel does not bring any speed improvements
|
||||
val time = measureTimeMillis {
|
||||
|
|
|
@ -5,11 +5,23 @@ import android.view.LayoutInflater
|
|||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import kotlinx.android.synthetic.main.fragment_home.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.mosad.teapod.MainActivity
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.util.CacheHelper
|
||||
import org.mosad.teapod.util.adapter.MediaItemAdapter
|
||||
import org.mosad.teapod.util.decoration.MediaItemDecoration
|
||||
|
||||
class HomeFragment : Fragment() {
|
||||
|
||||
private lateinit var adapter: MediaItemAdapter
|
||||
private lateinit var layoutManager: LinearLayoutManager
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.fragment_home, container, false)
|
||||
|
@ -18,6 +30,28 @@ class HomeFragment : Fragment() {
|
|||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
text_home.text = "This is the home fragment"
|
||||
GlobalScope.launch {
|
||||
if (AoDParser.mediaList.isEmpty()) {
|
||||
AoDParser().listAnimes()
|
||||
}
|
||||
|
||||
val myListMedia = AoDParser.mediaList.filter { CacheHelper.myList.contains(it.link) }
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
context?.let {
|
||||
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
||||
adapter = MediaItemAdapter(myListMedia)
|
||||
adapter.onItemClick = { media, _ ->
|
||||
(activity as MainActivity).showMediaFragment(media)
|
||||
}
|
||||
|
||||
recycler_my_list.layoutManager = layoutManager
|
||||
recycler_my_list.adapter = adapter
|
||||
recycler_my_list.addItemDecoration(MediaItemDecoration(9))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -5,7 +5,6 @@ import android.view.LayoutInflater
|
|||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import kotlinx.android.synthetic.main.fragment_library.*
|
||||
import kotlinx.coroutines.*
|
||||
import org.mosad.teapod.MainActivity
|
||||
|
@ -17,7 +16,6 @@ import org.mosad.teapod.util.adapter.MediaItemAdapter
|
|||
class LibraryFragment : Fragment() {
|
||||
|
||||
private lateinit var adapter: MediaItemAdapter
|
||||
private lateinit var layoutManager: GridLayoutManager
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.fragment_library, container, false)
|
||||
|
@ -35,13 +33,11 @@ class LibraryFragment : Fragment() {
|
|||
// create and set the adapter, needs context
|
||||
withContext(Dispatchers.Main) {
|
||||
context?.let {
|
||||
layoutManager = GridLayoutManager(context, 2)
|
||||
adapter = MediaItemAdapter(AoDParser.mediaList)
|
||||
adapter.onItemClick = { media, _ ->
|
||||
(activity as MainActivity).showMediaFragment(media)
|
||||
}
|
||||
|
||||
recycler_media_library.layoutManager = layoutManager
|
||||
recycler_media_library.adapter = adapter
|
||||
recycler_media_library.addItemDecoration(MediaItemDecoration(9))
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ import kotlinx.android.synthetic.main.fragment_media.*
|
|||
import org.mosad.teapod.MainActivity
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.util.CacheHelper
|
||||
import org.mosad.teapod.util.DataTypes.MediaType
|
||||
import org.mosad.teapod.util.adapter.EpisodeItemAdapter
|
||||
import org.mosad.teapod.util.Media
|
||||
|
@ -59,6 +60,7 @@ class MediaFragment(private val media: Media, private val tmdb: TMDBResponse) :
|
|||
text_year.text = media.info.year.toString()
|
||||
text_age.text = media.info.age.toString()
|
||||
text_overview.text = media.info.shortDesc
|
||||
check_my_list.isChecked = CacheHelper.myList.contains(media.link)
|
||||
|
||||
// specific gui
|
||||
if (media.type == MediaType.TVSHOW) {
|
||||
|
@ -81,9 +83,6 @@ class MediaFragment(private val media: Media, private val tmdb: TMDBResponse) :
|
|||
|
||||
private fun initActions() {
|
||||
button_play.setOnClickListener {
|
||||
println(media.episodes)
|
||||
|
||||
|
||||
when (media.type) {
|
||||
MediaType.MOVIE -> playStream(media.episodes.first().streamUrl)
|
||||
MediaType.TVSHOW -> playStream(media.episodes.first().streamUrl)
|
||||
|
@ -91,6 +90,17 @@ class MediaFragment(private val media: Media, private val tmdb: TMDBResponse) :
|
|||
}
|
||||
}
|
||||
|
||||
// add or remove media from myList
|
||||
check_my_list.setOnCheckedChangeListener { buttonView, isChecked ->
|
||||
if (isChecked) {
|
||||
CacheHelper.myList.add(media.link)
|
||||
} else {
|
||||
CacheHelper.myList.remove(media.link)
|
||||
}
|
||||
CacheHelper.saveMyList(requireContext())
|
||||
// TODO notify home fragment on change
|
||||
}
|
||||
|
||||
// set onItemClick only in adapter is initialized
|
||||
if (this::adapterRecEpisodes.isInitialized) {
|
||||
adapterRecEpisodes.onItemClick = { _, position ->
|
||||
|
|
|
@ -6,7 +6,6 @@ import android.view.View
|
|||
import android.view.ViewGroup
|
||||
import android.widget.SearchView
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import kotlinx.android.synthetic.main.fragment_search.*
|
||||
import kotlinx.coroutines.*
|
||||
import org.mosad.teapod.MainActivity
|
||||
|
@ -18,8 +17,6 @@ import org.mosad.teapod.util.adapter.MediaItemAdapter
|
|||
class SearchFragment : Fragment() {
|
||||
|
||||
private var adapter : MediaItemAdapter? = null
|
||||
private lateinit var layoutManager: GridLayoutManager
|
||||
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.fragment_search, container, false)
|
||||
|
@ -36,14 +33,12 @@ class SearchFragment : Fragment() {
|
|||
// create and set the adapter, needs context
|
||||
withContext(Dispatchers.Main) {
|
||||
context?.let {
|
||||
layoutManager = GridLayoutManager(context, 2)
|
||||
adapter = MediaItemAdapter(AoDParser.mediaList)
|
||||
adapter!!.onItemClick = { media, _ ->
|
||||
search_text.clearFocus()
|
||||
(activity as MainActivity).showMediaFragment(media)
|
||||
}
|
||||
|
||||
recycler_media_search.layoutManager = layoutManager
|
||||
recycler_media_search.adapter = adapter
|
||||
recycler_media_search.addItemDecoration(MediaItemDecoration(9))
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24"
|
||||
android:tint="?attr/colorControlNormal">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
|
||||
</vector>
|
|
@ -0,0 +1,10 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24"
|
||||
android:tint="?attr/colorControlNormal">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M16.5,3c-1.74,0 -3.41,0.81 -4.5,2.09C10.91,3.81 9.24,3 7.5,3 4.42,3 2,5.42 2,8.5c0,3.78 3.4,6.86 8.55,11.54L12,21.35l1.45,-1.32C18.6,15.36 22,12.28 22,8.5 22,5.42 19.58,3 16.5,3zM12.1,18.55l-0.1,0.1 -0.1,-0.1C7.14,14.24 4,11.39 4,8.5 4,6.5 5.5,5 7.5,5c1.54,0 3.04,0.99 3.57,2.36h1.87C13.46,5.99 14.96,5 16.5,5c2,0 3.5,1.5 3.5,3.5 0,2.89 -3.14,5.74 -7.9,10.05z"/>
|
||||
</vector>
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<selector xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item
|
||||
android:drawable="@drawable/ic_baseline_favorite_border_24"
|
||||
android:state_checked="false" />
|
||||
<item
|
||||
android:drawable="@drawable/ic_baseline_favorite_24"
|
||||
android:state_checked="true" />
|
||||
<item
|
||||
android:drawable="@drawable/ic_baseline_favorite_border_24" />
|
||||
</selector>
|
|
@ -7,6 +7,41 @@
|
|||
android:background="#f5f5f5"
|
||||
tools:context=".ui.fragments.HomeFragment">
|
||||
|
||||
<ScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical" >
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="5dp"
|
||||
android:text="My List"
|
||||
android:textSize="16sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recycler_my_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="horizontal"
|
||||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
|
||||
tools:listitem="@layout/item_media" />
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_home"
|
||||
android:layout_width="match_parent"
|
||||
|
|
|
@ -13,9 +13,13 @@
|
|||
android:layout_height="match_parent"
|
||||
android:clipToPadding="false"
|
||||
android:padding="3dp"
|
||||
android:orientation="vertical"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
|
||||
app:spanCount="2"
|
||||
tools:listitem="@layout/item_media" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
@ -113,6 +113,20 @@
|
|||
android:layout_marginEnd="12dp"
|
||||
android:text="@string/text_overview_ex" />
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/check_my_list"
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="48dp"
|
||||
|
||||
android:layout_marginStart="31dp"
|
||||
android:layout_marginTop="7dp"
|
||||
android:layout_marginEnd="7dp"
|
||||
android:layout_marginBottom="7dp"
|
||||
android:button="@drawable/sl_favourite_24dp"
|
||||
android:scaleX="2.0"
|
||||
android:scaleY="2.0"
|
||||
app:buttonTint="#4A4141" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recycler_episodes"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -120,7 +134,8 @@
|
|||
android:layout_marginStart="7dp"
|
||||
android:layout_marginTop="17dp"
|
||||
android:layout_marginEnd="7dp"
|
||||
tools:layout_editor_absoluteY="298dp" />
|
||||
tools:layout_editor_absoluteY="298dp"
|
||||
tools:listitem="@layout/item_episode" />
|
||||
</LinearLayout>
|
||||
</androidx.core.widget.NestedScrollView>
|
||||
|
||||
|
|
|
@ -28,10 +28,14 @@
|
|||
android:layout_height="0dp"
|
||||
android:clipToPadding="false"
|
||||
android:padding="3dp"
|
||||
android:orientation="vertical"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/search_text">
|
||||
app:layout_constraintTop_toBottomOf="@+id/search_text"
|
||||
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
|
||||
app:spanCount="2"
|
||||
tools:listitem="@layout/item_media">
|
||||
|
||||
</androidx.recyclerview.widget.RecyclerView>
|
||||
|
||||
|
|
|
@ -1,26 +1,28 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_width="195dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:backgroundTint="#FFFFFF"
|
||||
android:visibility="visible"
|
||||
app:cardCornerRadius="7dp"
|
||||
app:cardElevation="4dp">
|
||||
|
||||
<LinearLayout
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/image_poster"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="112.5dp"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
android:contentDescription="@string/media_poster_desc"
|
||||
android:maxWidth="200dp"
|
||||
android:scaleType="centerCrop"
|
||||
app:layout_constraintBottom_toTopOf="@+id/text_title"
|
||||
app:layout_constraintDimensionRatio="H,16:9"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:srcCompat="@color/md_disabled_text_dark_theme" />
|
||||
|
||||
<TextView
|
||||
|
@ -33,6 +35,8 @@
|
|||
android:padding="3dp"
|
||||
android:text="@string/text_title_ex"
|
||||
android:textAlignment="center"
|
||||
android:textSize="15sp" />
|
||||
</LinearLayout>
|
||||
android:textSize="15sp"
|
||||
app:layout_constraintTop_toBottomOf="@+id/image_poster" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
Loading…
Reference in New Issue