Skip to content

Commit

Permalink
Merge pull request #10 from Merseyside/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Merseyside authored Jun 17, 2022
2 parents cbfed06 + 1da9788 commit b2523a1
Show file tree
Hide file tree
Showing 72 changed files with 1,267 additions and 937 deletions.
1 change: 1 addition & 0 deletions archy-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {
}

android {
namespace = "com.merseyside.merseyLib.archy.android"
compileSdk = Application.compileSdk

defaultConfig {
Expand Down
3 changes: 1 addition & 2 deletions archy-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.merseyside.merseyLib.archy.android"/>
<manifest/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.merseyside.merseyLib.archy.android.di

import androidx.fragment.app.Fragment
import com.merseyside.merseyLib.archy.core.di.SharedScopeHolder
import org.koin.core.scope.ScopeID

fun Fragment.findParentSharedScopeHolder(scopeID: ScopeID? = null): SharedScopeHolder? {
val parent = parentFragment

return if (parent != null) {
if (parent !is SharedScopeHolder) {
parent.findParentSharedScopeHolder(scopeID)
} else if (scopeID != null) {
if (parent.scopeID != scopeID) {
parent.findParentSharedScopeHolder(scopeID)
} else parent
} else parent
} else null
}

fun Fragment.requireParentSharedScopeHolder(scopeID: ScopeID): SharedScopeHolder {
return findParentSharedScopeHolder(scopeID) ?: throw NullPointerException("Scope required but returned null!")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.merseyside.merseyLib.archy.android.di

import com.merseyside.merseyLib.kotlin.extensions.isZero
import com.merseyside.merseyLib.kotlin.extensions.logMsg
import org.koin.core.scope.Scope

interface SharedScopeInstance {

val scope: Scope
val sharedScope: Scope
var linkedScopesCount: Int

private fun linkToScope() {
scope.linkTo(sharedScope)
linkedScopesCount++
}

private fun unlinkScope() {
scope.unlink(sharedScope)
val count = ++linkedScopesCount

if (count.isZero()) {
sharedScope.close().also { logMsg("${this::class.simpleName}", "Scope closed!") }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.merseyside.merseyLib.archy.android.di

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner

class SharedScopeLifecycleHandler(private val lifecycleOwner: LifecycleOwner): DefaultLifecycleObserver {

init {
lifecycleOwner.lifecycle.addObserver(this)
}

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.merseyside.merseyLib.archy.android.di.state

import android.os.Bundle
import com.merseyside.merseyLib.archy.core.di.state.KoinStateHolder
import com.merseyside.merseyLib.archy.core.di.state.toSavedState
import com.merseyside.merseyLib.utils.core.state.SavedState
import com.merseyside.merseyLib.archy.core.di.state.toBundle
import org.koin.android.scope.AndroidScopeComponent
import org.koin.core.context.loadKoinModules
import org.koin.core.qualifier.Qualifier
import org.koin.dsl.module

interface AndroidKoinScopeState : AndroidScopeComponent {

fun initKoinState(outState: Bundle?, toScope: Qualifier) {
loadKoinModules(getKoinStateModule(outState, toScope))
}

fun saveKoinState(outState: Bundle) {
val koinState = getKoinStateHolder().performSaveState()
outState.putBundle(koinStateKey, koinState.toBundle())
}

private fun getKoinSavedState(outState: Bundle?): SavedState {
return outState?.getBundle(koinStateKey).toSavedState()
}

private fun getKoinStateModule(outState: Bundle?, toScope: Qualifier) = module {
scope(toScope) {
scoped { KoinStateHolder(getKoin(), getKoinSavedState(outState)) }
}
}

// private fun getKoinStateScope(): Scope {
// return getKoin().getOrCreateScope(
// scopeId = linkableScope.id,
// qualifier = qualifier
// )
// }

fun getKoinStateHolder(): KoinStateHolder = scope.get()

}

private const val koinStateKey = "koin_state"

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.merseyside.merseyLib.archy.android.presentation.activity

import android.content.Context
import android.os.Bundle
import androidx.annotation.CallSuper
import androidx.databinding.ViewDataBinding
import com.merseyside.archy.presentation.activity.BaseBindingActivity
import com.merseyside.merseyLib.archy.core.di.state.getStateKey
import com.merseyside.merseyLib.archy.core.presentation.viewModel.BaseViewModel
import com.merseyside.merseyLib.archy.core.di.state.saveState
import com.merseyside.merseyLib.kotlin.Logger
import com.merseyside.merseyLib.utils.core.state.StateSaver
import com.merseyside.utils.reflection.ReflectionUtils
import org.koin.androidx.viewmodel.ext.android.getViewModel
import org.koin.core.context.loadKoinModules
import org.koin.core.module.Module
import org.koin.core.parameter.parametersOf
import kotlin.reflect.KClass

abstract class VMActivity<Binding : ViewDataBinding, Model : BaseViewModel>
: BaseBindingActivity<Binding>() {

protected lateinit var viewModel: Model

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setBindingVariable()
}

abstract fun getBindingVariable(): Int

private fun setBindingVariable() {
requireBinding().apply {
setVariable(getBindingVariable(), viewModel)
executePendingBindings()
}
}

protected open fun provideViewModel(bundle: Bundle?, vararg params: Any): Model {
return getViewModel(
clazz = getViewModelClass(),
parameters = { parametersOf(bundle, *params) }
)
}

@CallSuper
override fun performInjection(bundle: Bundle?, vararg params: Any) {
loadKoinModules(getKoinModules(bundle, *params))
viewModel = provideViewModel(bundle, *params)
}

open fun getKoinModules(bundle: Bundle?, vararg params: Any): List<Module> {
return emptyList<Module>().also {
Logger.logInfo("VMFragment", "Empty fragment's koin modules")
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
(viewModel as? StateSaver)?.saveState(outState, getStateKey(getViewModelClass()))
}

override fun handleError(throwable: Throwable): Boolean {
return viewModel.onError(throwable)
}

override fun updateLanguage(context: Context) {
//viewModel.updateLanguage(context)
}

protected abstract fun loadingObserver(isLoading: Boolean)

@Suppress("UNCHECKED_CAST")
protected fun getViewModelClass(): KClass<Model> {
return ReflectionUtils.getGenericParameterClass(
this.javaClass,
VMActivity::class.java,
1
).kotlin as KClass<Model>
}
}
Loading

0 comments on commit b2523a1

Please sign in to comment.