From ebe9712712efe78268dfdbadada1ac53f09875aa Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Tue, 12 Mar 2024 19:23:13 +0300 Subject: [PATCH] news: ensure context is available (fixes #3228) (#3231) Co-authored-by: dogi --- app/build.gradle | 4 +- .../planet/myplanet/base/BaseNewsFragment.kt | 10 ++-- .../planet/myplanet/ui/news/NewsFragment.kt | 59 +++++++++++-------- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 14fc0b4575..6692f2b688 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1409 - versionName "0.14.9" + versionCode 1410 + versionName "0.14.10" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.kt b/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.kt index 80eaca930d..a4e471ce18 100644 --- a/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.kt @@ -118,10 +118,12 @@ abstract class BaseNewsFragment : BaseContainerFragment(), OnNewsItemClickListen } fun changeLayoutManager(orientation: Int, recyclerView: RecyclerView) { - if (orientation == Configuration.ORIENTATION_LANDSCAPE) { - recyclerView.layoutManager = GridLayoutManager(activity, 2) - } else { - recyclerView.layoutManager = LinearLayoutManager(activity) + activity?.let { act -> + if (orientation == Configuration.ORIENTATION_LANDSCAPE) { + recyclerView.layoutManager = GridLayoutManager(act, 2) + } else { + recyclerView.layoutManager = LinearLayoutManager(act) + } } } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsFragment.kt index 31bc77ed33..9c720c4fb9 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsFragment.kt @@ -150,34 +150,43 @@ class NewsFragment : BaseNewsFragment() { } override fun setData(list: List?) { - changeLayoutManager(resources.configuration.orientation, fragmentNewsBinding.rvNews) - val resourceIds: MutableList = ArrayList() - list?.forEach { news -> - if ((news?.imagesArray?.size() ?: 0) > 0) { - val ob = news?.imagesArray?.get(0)?.asJsonObject - val resourceId = getString("resourceId", ob?.asJsonObject) - resourceId.let { - resourceIds.add(it) + if (isAdded) { + changeLayoutManager(resources.configuration.orientation, fragmentNewsBinding.rvNews) + val resourceIds: MutableList = ArrayList() + list?.forEach { news -> + if ((news?.imagesArray?.size() ?: 0) > 0) { + val ob = news?.imagesArray?.get(0)?.asJsonObject + val resourceId = getString("resourceId", ob?.asJsonObject) + resourceId.let { + resourceIds.add(it) + } } } + val urls = ArrayList() + val settings = requireActivity().getSharedPreferences(SyncActivity.PREFS_NAME, Context.MODE_PRIVATE) + val stringArray: Array = resourceIds.toTypedArray() + val lib: List = mRealm.where(RealmMyLibrary::class.java) + .`in`("_id", stringArray) + .findAll() + getUrlsAndStartDownload(lib, settings, urls) + adapterNews = activity?.let { + AdapterNews( + it, + list?.toMutableList() ?: mutableListOf(), + user, + null + ) + } + adapterNews?.setmRealm(mRealm) + adapterNews?.setFromLogin(requireArguments().getBoolean("fromLogin")) + adapterNews?.setListener(this) + adapterNews?.registerAdapterDataObserver(observer) + fragmentNewsBinding.rvNews.adapter = adapterNews + adapterNews?.let { showNoData(fragmentNewsBinding.tvMessage, it.itemCount) } + fragmentNewsBinding.llAddNews.visibility = View.GONE + fragmentNewsBinding.btnAddStory.text = getString(R.string.add_story) + adapterNews?.notifyDataSetChanged() } - val urls = ArrayList() - val settings = requireActivity().getSharedPreferences(SyncActivity.PREFS_NAME, Context.MODE_PRIVATE) - val stringArray: Array = resourceIds.toTypedArray() - val lib: List = mRealm.where(RealmMyLibrary::class.java) - .`in`("_id", stringArray) - .findAll() - getUrlsAndStartDownload(lib, settings, urls) - adapterNews = activity?.let { AdapterNews(it, list?.toMutableList() ?: mutableListOf(), user, null) } - adapterNews?.setmRealm(mRealm) - adapterNews?.setFromLogin(requireArguments().getBoolean("fromLogin")) - adapterNews?.setListener(this) - adapterNews?.registerAdapterDataObserver(observer) - fragmentNewsBinding.rvNews.adapter = adapterNews - adapterNews?.let { showNoData(fragmentNewsBinding.tvMessage, it.itemCount) } - fragmentNewsBinding.llAddNews.visibility = View.GONE - fragmentNewsBinding.btnAddStory.text = getString(R.string.add_story) - adapterNews?.notifyDataSetChanged() } override fun onConfigurationChanged(newConfig: Configuration) {