Remove viewmodel/fragment references

This commit is contained in:
ganfra 2023-01-13 18:20:10 +01:00
parent ec1bbdeb9c
commit 7a29ce1d8d
21 changed files with 0 additions and 544 deletions

View file

@ -14,5 +14,4 @@ dependencies {
api(libs.dagger)
api(libs.appyx.core)
api(libs.androidx.lifecycle.runtime)
api(libs.mavericks.compose)
}

View file

@ -1,24 +0,0 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.x.architecture.viewmodel
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.MavericksViewModel
interface AssistedViewModelFactory<VM : MavericksViewModel<S>, S : MavericksState> {
fun create(initialState: S): VM
}

View file

@ -1,74 +0,0 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.x.architecture.viewmodel
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.MavericksViewModel
import com.airbnb.mvrx.MavericksViewModelFactory
import com.airbnb.mvrx.ViewModelContext
import io.element.android.x.architecture.bindings
/**
* To connect Mavericks ViewModel creation with Anvil's dependency injection, add the following to your MavericksViewModel.
*
* Example:
*
* @ContributesViewModel(YourScope::class)
* class MyViewModel @AssistedInject constructor(
* @Assisted initialState: MyState,
* ,
* ): MavericksViewModel<MyState>(...) {
*
*
* companion object : MavericksViewModelFactory<MyViewModel, MyState> by daggerMavericksViewModelFactory()
* }
*/
inline fun <reified VM : MavericksViewModel<S>, S : MavericksState> daggerMavericksViewModelFactory() = DaggerMavericksViewModelFactory(VM::class.java)
/**
* A [MavericksViewModelFactory] makes it easy to create instances of a ViewModel
* using its AssistedInject Factory. This class should be implemented by the companion object
* of every ViewModel which uses AssistedInject via [daggerMavericksViewModelFactory].
*
* @param VM The ViewModel type
* @param S The ViewState type
* @param viewModelClass The [Class] of the ViewModel being requested for creation
*
* This class accesses the map of ViewModel class to [AssistedViewModelFactory]s from the nearest [DaggerComponentOwner] and
* uses it to retrieve the requested ViewModel's factory class. It then creates an instance of this ViewModel
* using the retrieved factory and returns it.
* @see daggerMavericksViewModelFactory
*/
class DaggerMavericksViewModelFactory<VM : MavericksViewModel<S>, S : MavericksState>(
private val viewModelClass: Class<VM>
) : MavericksViewModelFactory<VM, S> {
override fun create(viewModelContext: ViewModelContext, state: S): VM {
val bindings: DaggerMavericksBindings = viewModelContext.activity.bindings()
val viewModelFactoryMap = bindings.viewModelFactories()
val viewModelFactory = viewModelFactoryMap[viewModelClass] ?: error("Cannot find ViewModelFactory for ${viewModelClass.name}.")
@Suppress("UNCHECKED_CAST")
val castedViewModelFactory = viewModelFactory as? AssistedViewModelFactory<VM, S>
val viewModel = castedViewModelFactory?.create(state)
return viewModel as VM
}
}
interface DaggerMavericksBindings {
fun viewModelFactories(): Map<Class<out MavericksViewModel<*>>, AssistedViewModelFactory<*, *>>
}

View file

@ -1,26 +0,0 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.x.architecture.viewmodel
import com.airbnb.mvrx.MavericksViewModel
import dagger.MapKey
import kotlin.reflect.KClass
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
@MapKey
annotation class ViewModelKey(val value: KClass<out MavericksViewModel<*>>)

View file

@ -1,121 +0,0 @@
package io.element.android.x.architecture.viewmodel
import android.os.Bundle
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.DEFAULT_ARGS_KEY
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.HasDefaultViewModelProviderFactory
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.SAVED_STATE_REGISTRY_OWNER_KEY
import androidx.lifecycle.SavedStateViewModelFactory
import androidx.lifecycle.VIEW_MODEL_STORE_OWNER_KEY
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStore
import androidx.lifecycle.ViewModelStoreOwner
import androidx.lifecycle.enableSavedStateHandles
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.MutableCreationExtras
import androidx.savedstate.SavedStateRegistry
import androidx.savedstate.SavedStateRegistryController
import androidx.savedstate.SavedStateRegistryOwner
import com.bumble.appyx.core.modality.BuildContext
import com.bumble.appyx.core.node.Node
import com.bumble.appyx.core.plugin.Plugin
fun viewModelSupportNode(buildContext: BuildContext, plugins: List<Plugin> = emptyList(), composable: @Composable (Modifier) -> Unit): Node =
ViewModelSupportNode(buildContext, plugins, composable)
class ViewModelSupportNode(
buildContext: BuildContext,
plugins: List<Plugin> = emptyList(),
private val composable: @Composable (Modifier) -> Unit,
) : Node(
buildContext, plugins = plugins
), ViewModelStoreOwner, SavedStateRegistryOwner {
private val viewModelSupport = ViewModelSupport(
lifecycle,
buildContext.savedStateMap?.get("SAVED_STATE_REGISTRY") as Bundle?,
)
override fun getViewModelStore(): ViewModelStore {
return viewModelSupport.viewModelStore
}
override val savedStateRegistry: SavedStateRegistry
get() = viewModelSupport.savedStateRegistry
@Composable
override fun View(modifier: Modifier) {
composable(modifier)
}
}
private class ViewModelSupport(
private val lifecycle: Lifecycle,
private val initialSavedState: Bundle?,
val defaultArgs: Bundle? = null,
) : ViewModelStoreOwner, HasDefaultViewModelProviderFactory, SavedStateRegistryOwner {
private val viewModelStore = ViewModelStore()
private val savedStateRegistryController: SavedStateRegistryController =
SavedStateRegistryController.create(this)
//Don't replace the initial saved state until we have at least started
private var canSaveState: Boolean = false
init {
savedStateRegistryController.performAttach()
// We copy the bundle because the `savedStateRegistryController` will modify it.
// We don't want to modify `initialSavedState` since we may need to return that as our
// state in `saveState`.
savedStateRegistryController.performRestore(initialSavedState?.let { Bundle(it) })
enableSavedStateHandles()
lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) {
canSaveState = true
}
override fun onDestroy(owner: LifecycleOwner) {
viewModelStore.clear()
}
})
}
override fun getViewModelStore(): ViewModelStore {
return viewModelStore
}
override fun getDefaultViewModelProviderFactory(): ViewModelProvider.Factory {
return SavedStateViewModelFactory(null, this, defaultArgs)
}
override fun getDefaultViewModelCreationExtras(): CreationExtras {
val extras = MutableCreationExtras()
extras[SAVED_STATE_REGISTRY_OWNER_KEY] = this
extras[VIEW_MODEL_STORE_OWNER_KEY] = this
defaultArgs?.let { args ->
extras[DEFAULT_ARGS_KEY] = args
}
return extras
}
override val savedStateRegistry: SavedStateRegistry
get() = savedStateRegistryController.savedStateRegistry
override fun getLifecycle(): Lifecycle {
return lifecycle
}
fun saveState(): Bundle? {
return if (canSaveState) {
Bundle().also(savedStateRegistryController::performSave)
} else {
initialSavedState
}
}
}

View file

@ -1,49 +0,0 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.x.matrix.ui.viewmodels.user
import com.airbnb.mvrx.MavericksViewModel
import com.airbnb.mvrx.MavericksViewModelFactory
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import io.element.android.x.anvilannotations.ContributesViewModel
import io.element.android.x.architecture.viewmodel.daggerMavericksViewModelFactory
import io.element.android.x.designsystem.components.avatar.AvatarSize
import io.element.android.x.di.SessionScope
import io.element.android.x.matrix.MatrixClient
import io.element.android.x.matrix.ui.MatrixItemHelper
@ContributesViewModel(SessionScope::class)
class UserViewModel @AssistedInject constructor(
client: MatrixClient,
@Assisted initialState: UserViewState
) : MavericksViewModel<UserViewState>(initialState) {
companion object : MavericksViewModelFactory<UserViewModel, UserViewState> by daggerMavericksViewModelFactory()
private val matrixUserHelper = MatrixItemHelper(client)
init {
handleInit()
}
private fun handleInit() {
matrixUserHelper.getCurrentUserData(avatarSize = AvatarSize.SMALL).execute {
copy(user = it)
}
}
}

View file

@ -1,26 +0,0 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.x.matrix.ui.viewmodels.user
import com.airbnb.mvrx.Async
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.Uninitialized
import io.element.android.x.matrix.ui.model.MatrixUser
data class UserViewState(
val user: Async<MatrixUser> = Uninitialized,
) : MavericksState