-
Notifications
You must be signed in to change notification settings - Fork 901
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow injecting View ViewModelFactory (#4271)
Task/Issue URL: https://app.asana.com/0/276630244458377/1206794624404182/f ### Description Being able to inject ViewModelFactory for ViewScope Use ContributesViewModel from CustomViews Refactor one CustomView as proof of concept during review ### Steps to test this PR _Feature 1_ - [x] Fresh install of this branch on a device Android >= 33 - [x] during onboarding don't accept notification permissions - [x] Go to downloads - [x] Ensure Notifications permissions is present - [x] smoke test the card ### UI changes | Before | After | | ------ | ----- | !(Upload before screenshot)|(Upload after screenshot)|
- Loading branch information
1 parent
eb278a4
commit 3e86da3
Showing
4 changed files
with
75 additions
and
22 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
37 changes: 37 additions & 0 deletions
37
common/common-utils/src/main/java/com/duckduckgo/common/utils/ViewViewModelFactory.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,37 @@ | ||
/* | ||
* Copyright (c) 2023 DuckDuckGo | ||
* | ||
* 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 com.duckduckgo.common.utils | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.duckduckgo.common.utils.plugins.view_model.ViewViewModelFactoryPluginPoint | ||
import com.duckduckgo.di.scopes.ViewScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import dagger.SingleInstanceIn | ||
import javax.inject.Inject | ||
|
||
@SingleInstanceIn(ViewScope::class) | ||
@ContributesBinding(ViewScope::class) | ||
class ViewViewModelFactory @Inject constructor( | ||
private val viewModelFactoryPluginPoint: ViewViewModelFactoryPluginPoint, | ||
) : ViewModelProvider.NewInstanceFactory() { | ||
|
||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return viewModelFactoryPluginPoint.getPlugins().mapNotNull { it.create(modelClass) } | ||
.first() | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...in/java/com/duckduckgo/common/utils/plugins/view_model/ViewViewModelFactoryPluginPoint.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,32 @@ | ||
/* | ||
* Copyright (c) 2022 DuckDuckGo | ||
* | ||
* 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 com.duckduckgo.common.utils.plugins.view_model | ||
|
||
import com.duckduckgo.common.utils.plugins.PluginPoint | ||
import com.duckduckgo.di.DaggerSet | ||
import com.duckduckgo.di.scopes.ViewScope | ||
import dagger.SingleInstanceIn | ||
import javax.inject.Inject | ||
|
||
@SingleInstanceIn(ViewScope::class) | ||
class ViewViewModelFactoryPluginPoint @Inject constructor( | ||
private val injectorPlugins: DaggerSet<ViewModelFactoryPlugin>, | ||
) : PluginPoint<ViewModelFactoryPlugin> { | ||
override fun getPlugins(): List<ViewModelFactoryPlugin> { | ||
return injectorPlugins.toList() | ||
} | ||
} |