Skip to content

Commit

Permalink
news: ensure context is available (fixes #3228) (#3231)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Mar 12, 2024
1 parent ce00a48 commit ebe9712
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
59 changes: 34 additions & 25 deletions app/src/main/java/org/ole/planet/myplanet/ui/news/NewsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,43 @@ class NewsFragment : BaseNewsFragment() {
}

override fun setData(list: List<RealmNews?>?) {
changeLayoutManager(resources.configuration.orientation, fragmentNewsBinding.rvNews)
val resourceIds: MutableList<String> = 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<String> = 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<String>()
val settings = requireActivity().getSharedPreferences(SyncActivity.PREFS_NAME, Context.MODE_PRIVATE)
val stringArray: Array<String?> = resourceIds.toTypedArray()
val lib: List<RealmMyLibrary?> = 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<String>()
val settings = requireActivity().getSharedPreferences(SyncActivity.PREFS_NAME, Context.MODE_PRIVATE)
val stringArray: Array<String?> = resourceIds.toTypedArray()
val lib: List<RealmMyLibrary?> = 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) {
Expand Down

0 comments on commit ebe9712

Please sign in to comment.