-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Merseyside/develop
Develop
- Loading branch information
Showing
72 changed files
with
1,267 additions
and
937 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/> |
23 changes: 23 additions & 0 deletions
23
archy-android/src/main/java/com/merseyside/merseyLib/archy/android/di/SharedScopeExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} |
26 changes: 26 additions & 0 deletions
26
archy-android/src/main/java/com/merseyside/merseyLib/archy/android/di/SharedScopeInstance.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") } | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...id/src/main/java/com/merseyside/merseyLib/archy/android/di/SharedScopeLifecycleHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...d/src/main/java/com/merseyside/merseyLib/archy/android/di/state/AndroidKoinStateHolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
123 changes: 0 additions & 123 deletions
123
.../main/java/com/merseyside/merseyLib/archy/android/presentation/activity/BaseVMActivity.kt
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
...java/com/merseyside/merseyLib/archy/android/presentation/activity/BaseVMEventsActivity.kt
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
.../src/main/java/com/merseyside/merseyLib/archy/android/presentation/activity/VMActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} | ||
} |
Oops, something went wrong.