Skip to content

Commit

Permalink
- introduced koin so that the generic data can be used better
Browse files Browse the repository at this point in the history
 - On my Pixel 4xl, it would crash sometimes because of genericInfo being null (when its set as Delegates.NotNull) because the RecentFragment.kt was calling it too early. Koin should fix this.
  • Loading branch information
jakepurple13 committed May 25, 2021
1 parent 1b1608e commit fdd0918
Show file tree
Hide file tree
Showing 24 changed files with 521 additions and 409 deletions.
2 changes: 2 additions & 0 deletions UIViews/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ dependencies {

implementation 'com.github.pwittchen:reactivenetwork-rx2:3.0.8'

implementation koin.koin

implementation project(':Models')
implementation project(':favoritesdatabase')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AllFragment : BaseListFragment() {

binding.allList.apply {
adapter = this@AllFragment.adapter
layoutManager = createLayoutManager(this@AllFragment.requireContext())
layoutManager = info.createLayoutManager(this@AllFragment.requireContext())
addOnScrollListener(object : EndlessScrollingListener(layoutManager!!) {
override fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView?) {
if (sourcePublish.value!!.canScroll && binding.searchInfo.text.isNullOrEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import android.os.Bundle
import android.view.View
import androidx.annotation.CallSuper
import androidx.recyclerview.widget.RecyclerView
import org.koin.android.ext.android.inject

abstract class BaseListFragment : BaseFragment(), GenericInfo by BaseMainActivity.genericInfo {
abstract class BaseListFragment : BaseFragment() {
protected lateinit var adapter: ItemListAdapter<RecyclerView.ViewHolder>

protected val info: GenericInfo by inject()

@CallSuper
override fun viewCreated(view: View, savedInstanceState: Bundle?) {
adapter = createAdapter(this@BaseListFragment.requireContext(), this)
adapter = info.createAdapter(this@BaseListFragment.requireContext(), this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ import com.squareup.okhttp.Request
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import kotlinx.coroutines.launch
import kotlin.properties.Delegates
import org.koin.android.ext.android.inject

abstract class BaseMainActivity : AppCompatActivity(), GenericInfo {
abstract class BaseMainActivity : AppCompatActivity() {

protected val disposable = CompositeDisposable()

private var currentNavController: LiveData<NavController>? = null

protected val genericInfo: GenericInfo by inject()

protected abstract fun onCreate()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
genericInfo = this
setContentView(R.layout.base_main_activity)

toSource(currentService.orEmpty())?.let { sourcePublish.onNext(it) }
genericInfo.toSource(currentService.orEmpty())?.let { sourcePublish.onNext(it) }

if (savedInstanceState == null) {
setupBottomNavBar()
Expand Down Expand Up @@ -112,8 +113,4 @@ abstract class BaseMainActivity : AppCompatActivity(), GenericInfo {
super.onDestroy()
}

companion object {
var genericInfo by Delegates.notNull<GenericInfo>()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.BehaviorSubject
import me.zhanghai.android.fastscroll.FastScrollerBuilder
import org.koin.android.ext.android.inject

class DetailsFragment : Fragment() {

Expand All @@ -51,7 +52,7 @@ class DetailsFragment : Fragment() {

private val disposable = CompositeDisposable()

private val adapter by lazy { ChapterAdapter(requireContext(), BaseMainActivity.genericInfo, dao) }
private val adapter by lazy { ChapterAdapter(requireContext(), inject<GenericInfo>().value, dao) }

private val itemListener = FirebaseDb.FirebaseListener()
private val chapterListener = FirebaseDb.FirebaseListener()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ import io.reactivex.rxkotlin.Flowables
import io.reactivex.rxkotlin.addTo
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.BehaviorSubject
import org.koin.android.ext.android.inject
import java.util.concurrent.TimeUnit

class FavoriteFragment : BaseFragment() {

private val dao by lazy { ItemDatabase.getInstance(requireContext()).itemDao() }
private val disposable = CompositeDisposable()

private val sources by lazy { BaseMainActivity.genericInfo.sourceList() }
private val genericInfo by inject<GenericInfo>()
private val sources by lazy { genericInfo.sourceList() }
private val sourcePublisher = BehaviorSubject.createDefault(sources.toMutableList())
private var sourcesList by behaviorDelegate(sourcePublisher)
private val adapter by lazy { FavoriteAdapter() }
Expand Down Expand Up @@ -164,12 +166,12 @@ class FavoriteFragment : BaseFragment() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FavoriteHolder =
FavoriteHolder(FavoriteItemBinding.inflate(requireContext().layoutInflater, parent, false))

override fun FavoriteHolder.onBind(item: Pair<String, List<DbModel>>, position: Int) = bind(item.second)
override fun FavoriteHolder.onBind(item: Pair<String, List<DbModel>>, position: Int) = bind(item.second, genericInfo)
}

class FavoriteHolder(private val binding: FavoriteItemBinding) : RecyclerView.ViewHolder(binding.root) {

fun bind(info: List<DbModel>) {
fun bind(info: List<DbModel>, genericInfo: GenericInfo) {
binding.show = info.random()
Glide.with(itemView.context)
.asBitmap()
Expand All @@ -183,13 +185,13 @@ class FavoriteFragment : BaseFragment() {

binding.root.setOnClickListener {
if (info.size == 1) {
val item = info.firstOrNull()?.let { BaseMainActivity.genericInfo.toSource(it.source)?.let { it1 -> it.toItemModel(it1) } }
val item = info.firstOrNull()?.let { genericInfo.toSource(it.source)?.let { it1 -> it.toItemModel(it1) } }
binding.root.findNavController().navigate(FavoriteFragmentDirections.actionFavoriteFragmentToDetailsFragment(item))
} else {
MaterialAlertDialogBuilder(itemView.context)
.setTitle(R.string.chooseASource)
.setItems(info.map { "${it.source} - ${it.title}" }.toTypedArray()) { d, i ->
val item = info[i].let { BaseMainActivity.genericInfo.toSource(it.source)?.let { it1 -> it.toItemModel(it1) } }
val item = info[i].let { genericInfo.toSource(it.source)?.let { it1 -> it.toItemModel(it1) } }
binding.root.findNavController().navigate(FavoriteFragmentDirections.actionFavoriteFragmentToDetailsFragment(item))
d.dismiss()
}
Expand Down
8 changes: 8 additions & 0 deletions UIViews/src/main/java/com/programmersbox/uiviews/OtakuApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import com.programmersbox.helpfulutils.createNotificationGroup
import com.programmersbox.loggingutils.Loged
import com.programmersbox.uiviews.utils.shouldCheck
import io.reactivex.plugins.RxJavaPlugins
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import java.util.concurrent.TimeUnit

abstract class OtakuApp : Application() {
Expand All @@ -35,6 +38,11 @@ abstract class OtakuApp : Application() {
//FirebaseCrashlytics.getInstance().recordException(it)
}

startKoin {
androidLogger()
androidContext(this@OtakuApp)
}

onCreated()

val work = WorkManager.getInstance(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class RecentFragment : BaseListFragment() {

binding.recentList.apply {
adapter = this@RecentFragment.adapter
layoutManager = createLayoutManager(this@RecentFragment.requireContext())
layoutManager = info.createLayoutManager(this@RecentFragment.requireContext())
addOnScrollListener(object : EndlessScrollingListener(layoutManager!!) {
override fun onLoadMore(page: Int, totalItemsCount: Int, view: RecyclerView?) {
if (sourcePublish.value!!.canScroll) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import io.reactivex.rxkotlin.addTo
import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.*
import org.koin.android.ext.android.inject
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
Expand All @@ -56,11 +57,11 @@ class SettingsFragment : PreferenceFragmentCompat() {

private val disposable: CompositeDisposable = CompositeDisposable()

private val genericInfo: GenericInfo by inject()

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)

val genericInfo = BaseMainActivity.genericInfo

accountPreferences()
generalPreferences(genericInfo)
aboutPreferences(genericInfo)
Expand Down
2 changes: 2 additions & 0 deletions animeworld/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,6 @@ dependencies {

//Custom Libraries
implementation jakepurple13Tools.helpfultools

implementation koin.koin
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ import com.tonyodev.fetch2.*
import com.tonyodev.fetch2.Fetch.Impl.setDefaultInstanceConfiguration
import com.tonyodev.fetch2core.Downloader
import com.tonyodev.fetch2core.deleteFile
import org.koin.core.context.loadKoinModules
import java.net.HttpURLConnection
import javax.net.ssl.*

class AnimeApp : OtakuApp() {
override fun onCreated() {

loadKoinModules(appModule)

logo = R.mipmap.ic_launcher
notificationLogo = R.mipmap.ic_launcher_foreground

Expand Down
Loading

0 comments on commit fdd0918

Please sign in to comment.