update my list on home screen, when changed
This commit is contained in:
		| @ -42,29 +42,32 @@ 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.StorageController | ||||
| import org.mosad.teapod.util.Media | ||||
| import org.mosad.teapod.util.TMDBApiController | ||||
| import kotlin.system.measureTimeMillis | ||||
|  | ||||
| class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemSelectedListener { | ||||
|  | ||||
|     private var activeFragment: Fragment = HomeFragment() // the currently active fragment, home at the start | ||||
|     private var activeBaseFragment: Fragment = HomeFragment() // the currently active fragment, home at the start | ||||
|  | ||||
|     override fun onCreate(savedInstanceState: Bundle?) { | ||||
|         super.onCreate(savedInstanceState) | ||||
|         setContentView(R.layout.activity_main) | ||||
|         val navView: BottomNavigationView = findViewById(R.id.nav_view) | ||||
|         navView.setOnNavigationItemSelectedListener(this) | ||||
|         nav_view.setOnNavigationItemSelectedListener(this) | ||||
|  | ||||
|         load() | ||||
|  | ||||
|         supportFragmentManager.commit { | ||||
|             replace(R.id.nav_host_fragment, activeBaseFragment, activeBaseFragment.javaClass.simpleName) | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     override fun onBackPressed() { | ||||
|         if (supportFragmentManager.backStackEntryCount > 0) { | ||||
|             supportFragmentManager.popBackStack() | ||||
|         } else { | ||||
|             if (activeFragment !is HomeFragment) { | ||||
|             if (activeBaseFragment !is HomeFragment) { | ||||
|                 nav_view.selectedItemId = R.id.navigation_home | ||||
|             } else { | ||||
|                 super.onBackPressed() | ||||
| @ -79,52 +82,56 @@ class MainActivity : AppCompatActivity(), BottomNavigationView.OnNavigationItemS | ||||
|  | ||||
|         val ret = when (item.itemId) { | ||||
|             R.id.navigation_home -> { | ||||
|                 activeFragment = HomeFragment() | ||||
|                 activeBaseFragment = HomeFragment() | ||||
|                 true | ||||
|             } | ||||
|             R.id.navigation_library -> { | ||||
|                 activeFragment = LibraryFragment() | ||||
|                 activeBaseFragment = LibraryFragment() | ||||
|                 true | ||||
|             } | ||||
|             R.id.navigation_search -> { | ||||
|                 activeFragment = SearchFragment() | ||||
|                 activeBaseFragment = SearchFragment() | ||||
|                 true | ||||
|             } | ||||
|             R.id.navigation_account -> { | ||||
|                 activeFragment = AccountFragment() | ||||
|                 activeBaseFragment = AccountFragment() | ||||
|                 true | ||||
|             } | ||||
|             else -> false | ||||
|         } | ||||
|  | ||||
|         supportFragmentManager.commit { | ||||
|             replace(R.id.nav_host_fragment, activeFragment) | ||||
|             replace(R.id.nav_host_fragment, activeBaseFragment, activeBaseFragment.javaClass.simpleName) | ||||
|         } | ||||
|  | ||||
|         return ret | ||||
|     } | ||||
|  | ||||
|     private fun load() { | ||||
|         EncryptedPreferences.readCredentials(this) | ||||
|  | ||||
|         // make sure credentials are set | ||||
|         if (EncryptedPreferences.password.isEmpty()) { | ||||
|             showLoginDialog(true) | ||||
|         } | ||||
|         EncryptedPreferences.readCredentials(this) | ||||
|         if (EncryptedPreferences.password.isEmpty()) showLoginDialog(true) | ||||
|  | ||||
|         CacheHelper.load(this) | ||||
|         StorageController.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 { | ||||
|             // try to login in, as most sites can only bee loaded once loged in | ||||
|             if (!AoDParser().login()) showLoginDialog(false) | ||||
|  | ||||
|             // initially load all media | ||||
|             AoDParser().listAnimes() | ||||
|  | ||||
|             // TODO load home screen, can be parallel to listAnimes | ||||
|         } | ||||
|         Log.i(javaClass.name, "login and list in $time ms") | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * show the media fragment for the selected media | ||||
|      * while loading show the loading fragment | ||||
|      * Show the media fragment for the selected media. | ||||
|      * While loading show the loading fragment. | ||||
|      * The loading and media fragment are not stored in activeBaseFragment, | ||||
|      * as the don't replace a fragment but are added on top of one. | ||||
|      */ | ||||
|     fun showMediaFragment(media: Media) = GlobalScope.launch { | ||||
|         val loadingFragment = LoadingFragment() | ||||
|  | ||||
| @ -14,7 +14,7 @@ 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.StorageController | ||||
| import org.mosad.teapod.util.adapter.MediaItemAdapter | ||||
| import org.mosad.teapod.util.decoration.MediaItemDecoration | ||||
|  | ||||
| @ -35,23 +35,32 @@ class HomeFragment : Fragment() { | ||||
|                 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)) | ||||
|  | ||||
|                     updateMyListMedia() | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     // TODO recreating the adapter on list change is not a good solution | ||||
|     fun updateMyListMedia() { | ||||
|         val myListMedia = StorageController.myList.map { listElement -> | ||||
|             AoDParser.mediaList.first { | ||||
|                 listElement == it.link | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         adapter = MediaItemAdapter(myListMedia) | ||||
|         adapter.onItemClick = { media, _ -> | ||||
|             (activity as MainActivity).showMediaFragment(media) | ||||
|         } | ||||
|  | ||||
|         recycler_my_list.adapter = adapter | ||||
|     } | ||||
| } | ||||
| @ -17,7 +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.StorageController | ||||
| import org.mosad.teapod.util.DataTypes.MediaType | ||||
| import org.mosad.teapod.util.adapter.EpisodeItemAdapter | ||||
| import org.mosad.teapod.util.Media | ||||
| @ -60,7 +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) | ||||
|         check_my_list.isChecked = StorageController.myList.contains(media.link) | ||||
|  | ||||
|         // specific gui | ||||
|         if (media.type == MediaType.TVSHOW) { | ||||
| @ -91,14 +91,18 @@ class MediaFragment(private val media: Media, private val tmdb: TMDBResponse) : | ||||
|         } | ||||
|  | ||||
|         // add or remove media from myList | ||||
|         check_my_list.setOnCheckedChangeListener { buttonView, isChecked -> | ||||
|         check_my_list.setOnCheckedChangeListener { _, isChecked -> | ||||
|             if (isChecked) { | ||||
|                 CacheHelper.myList.add(media.link) | ||||
|                 StorageController.myList.add(media.link) | ||||
|             } else { | ||||
|                 CacheHelper.myList.remove(media.link) | ||||
|                 StorageController.myList.remove(media.link) | ||||
|             } | ||||
|             CacheHelper.saveMyList(requireContext()) | ||||
|             StorageController.saveMyList(requireContext()) | ||||
|  | ||||
|             // TODO notify home fragment on change | ||||
|             parentFragmentManager.findFragmentByTag("HomeFragment")?.let { | ||||
|                 (it as HomeFragment).updateMyListMedia() | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         // set onItemClick only in adapter is initialized | ||||
|  | ||||
| @ -1,16 +1,16 @@ | ||||
| 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 | ||||
|  * This controller contains the logic for permanently saved data. | ||||
|  * On load, it loads the saved files into the variables | ||||
|  */ | ||||
| object CacheHelper { | ||||
| object StorageController { | ||||
| 
 | ||||
|     private const val fileNameMyList = "my_list.json" | ||||
| 
 | ||||
| @ -27,7 +27,6 @@ object CacheHelper { | ||||
|         ) | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     fun saveMyList(context: Context): Job { | ||||
|         val file = File(context.filesDir, fileNameMyList) | ||||
| 
 | ||||
| @ -36,12 +35,4 @@ object CacheHelper { | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     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) | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -25,8 +25,11 @@ | ||||
|                     android:id="@+id/textView" | ||||
|                     android:layout_width="match_parent" | ||||
|                     android:layout_height="wrap_content" | ||||
|                     android:padding="5dp" | ||||
|                     android:text="My List" | ||||
|                     android:paddingStart="10dp" | ||||
|                     android:paddingTop="15dp" | ||||
|                     android:paddingEnd="5dp" | ||||
|                     android:paddingBottom="5dp" | ||||
|                     android:text="@string/my_list" | ||||
|                     android:textSize="16sp" | ||||
|                     android:textStyle="bold" /> | ||||
|  | ||||
|  | ||||
| @ -5,6 +5,9 @@ | ||||
|     <string name="title_search">Suche</string> | ||||
|     <string name="title_account">Account</string> | ||||
|  | ||||
|     <!-- home fragment --> | ||||
|     <string name="my_list">Meine Liste</string> | ||||
|  | ||||
|     <!-- search fragment --> | ||||
|     <string name="search_hint">Suche nach Filmen und Serien</string> | ||||
|  | ||||
|  | ||||
| @ -5,6 +5,9 @@ | ||||
|     <string name="title_search">Search</string> | ||||
|     <string name="title_account">Account</string> | ||||
|  | ||||
|     <!-- home fragment --> | ||||
|     <string name="my_list">My list</string> | ||||
|  | ||||
|     <!-- search fragment --> | ||||
|     <string name="search_hint">Search for movies and series</string> | ||||
|     <string name="media_poster_desc" translatable="false">poster</string> | ||||
|  | ||||
		Reference in New Issue
	
	Block a user