-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Second iteration for settings (#3956)
Task/Issue URL: https://app.asana.com/0/0/1205820670085181/f ### Description See task ### Steps to test this PR See task
- Loading branch information
1 parent
3434b08
commit 5f8f3a3
Showing
16 changed files
with
531 additions
and
79 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
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
116 changes: 116 additions & 0 deletions
116
...ons-impl/src/main/java/com/duckduckgo/subscriptions/impl/settings/views/PirSettingView.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,116 @@ | ||
/* | ||
* 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.subscriptions.impl.settings.views | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.util.AttributeSet | ||
import android.widget.FrameLayout | ||
import android.widget.Toast | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.ViewTreeLifecycleOwner | ||
import androidx.lifecycle.findViewTreeViewModelStoreOwner | ||
import com.duckduckgo.anvil.annotations.InjectWith | ||
import com.duckduckgo.common.ui.view.gone | ||
import com.duckduckgo.common.ui.view.show | ||
import com.duckduckgo.common.ui.viewbinding.viewBinding | ||
import com.duckduckgo.common.utils.ConflatedJob | ||
import com.duckduckgo.di.scopes.ViewScope | ||
import com.duckduckgo.navigation.api.GlobalActivityStarter | ||
import com.duckduckgo.subscriptions.impl.databinding.ViewPirSettingsBinding | ||
import com.duckduckgo.subscriptions.impl.settings.views.PirSettingViewModel.Command | ||
import com.duckduckgo.subscriptions.impl.settings.views.PirSettingViewModel.Command.OpenPir | ||
import com.duckduckgo.subscriptions.impl.settings.views.PirSettingViewModel.Factory | ||
import com.duckduckgo.subscriptions.impl.settings.views.PirSettingViewModel.ViewState | ||
import dagger.android.support.AndroidSupportInjection | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
|
||
@InjectWith(ViewScope::class) | ||
class PirSettingView @JvmOverloads constructor( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
defStyle: Int = 0, | ||
) : FrameLayout(context, attrs, defStyle) { | ||
|
||
@Inject | ||
lateinit var viewModelFactory: Factory | ||
|
||
@Inject | ||
lateinit var globalActivityStarter: GlobalActivityStarter | ||
|
||
private var coroutineScope: CoroutineScope? = null | ||
|
||
private val binding: ViewPirSettingsBinding by viewBinding() | ||
|
||
private val viewModel: PirSettingViewModel by lazy { | ||
ViewModelProvider(findViewTreeViewModelStoreOwner()!!, viewModelFactory)[PirSettingViewModel::class.java] | ||
} | ||
|
||
private var job: ConflatedJob = ConflatedJob() | ||
|
||
override fun onAttachedToWindow() { | ||
AndroidSupportInjection.inject(this) | ||
super.onAttachedToWindow() | ||
|
||
ViewTreeLifecycleOwner.get(this)?.lifecycle?.addObserver(viewModel) | ||
|
||
binding.pirSettings.setClickListener { | ||
viewModel.onPir() | ||
} | ||
|
||
@SuppressLint("NoHardcodedCoroutineDispatcher") | ||
coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main) | ||
|
||
job += viewModel.commands() | ||
.onEach { processCommands(it) } | ||
.launchIn(coroutineScope!!) | ||
|
||
viewModel.viewState | ||
.onEach { renderView(it) } | ||
.launchIn(coroutineScope!!) | ||
} | ||
|
||
override fun onDetachedFromWindow() { | ||
super.onDetachedFromWindow() | ||
ViewTreeLifecycleOwner.get(this)?.lifecycle?.removeObserver(viewModel) | ||
coroutineScope?.cancel() | ||
job.cancel() | ||
coroutineScope = null | ||
} | ||
|
||
private fun renderView(viewState: ViewState) { | ||
if (viewState.hasSubscription) { | ||
binding.pirSettings.show() | ||
} else { | ||
binding.pirSettings.gone() | ||
} | ||
} | ||
|
||
private fun processCommands(command: Command) { | ||
when (command) { | ||
is OpenPir -> { | ||
Toast.makeText(context, "Open PIR", Toast.LENGTH_SHORT).show() | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.