-
Notifications
You must be signed in to change notification settings - Fork 937
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync Bookmarks limit, handle and surface errors
- Loading branch information
1 parent
6ca7b18
commit 3e772cd
Showing
28 changed files
with
650 additions
and
44 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
121 changes: 121 additions & 0 deletions
121
...ved-sites-impl/src/main/java/com/duckduckgo/savedsites/impl/sync/SaveSiteRateLimitView.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,121 @@ | ||
/* | ||
* 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.savedsites.impl.sync | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.util.AttributeSet | ||
import android.widget.FrameLayout | ||
import androidx.core.view.isVisible | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.ViewTreeLifecycleOwner | ||
import androidx.lifecycle.findViewTreeViewModelStoreOwner | ||
import com.duckduckgo.anvil.annotations.InjectWith | ||
import com.duckduckgo.browser.api.ui.BrowserScreens.BookmarksScreenNoParams | ||
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.saved.sites.impl.R | ||
import com.duckduckgo.saved.sites.impl.databinding.ViewSaveSiteRateLimitWarningBinding | ||
import com.duckduckgo.savedsites.impl.sync.SavedSiteRateLimitViewModel.Command | ||
import com.duckduckgo.savedsites.impl.sync.SavedSiteRateLimitViewModel.Command.NavigateToBookmarks | ||
import com.duckduckgo.savedsites.impl.sync.SavedSiteRateLimitViewModel.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 SaveSiteRateLimitView @JvmOverloads constructor( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
defStyle: Int = 0, | ||
) : FrameLayout(context, attrs, defStyle) { | ||
|
||
@Inject | ||
lateinit var globalActivityStarter: GlobalActivityStarter | ||
|
||
@Inject | ||
lateinit var viewModelFactory: SavedSiteRateLimitViewModel.Factory | ||
|
||
private var coroutineScope: CoroutineScope? = null | ||
|
||
private var job: ConflatedJob = ConflatedJob() | ||
|
||
private val binding: ViewSaveSiteRateLimitWarningBinding by viewBinding() | ||
|
||
private val viewModel: SavedSiteRateLimitViewModel by lazy { | ||
ViewModelProvider(findViewTreeViewModelStoreOwner()!!, viewModelFactory)[SavedSiteRateLimitViewModel::class.java] | ||
} | ||
|
||
override fun onAttachedToWindow() { | ||
AndroidSupportInjection.inject(this) | ||
super.onAttachedToWindow() | ||
|
||
ViewTreeLifecycleOwner.get(this)?.lifecycle?.addObserver(viewModel) | ||
|
||
configureViewListeners() | ||
|
||
@SuppressLint("NoHardcodedCoroutineDispatcher") | ||
coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main) | ||
|
||
viewModel.viewState() | ||
.onEach { render(it) } | ||
.launchIn(coroutineScope!!) | ||
|
||
job += viewModel.commands() | ||
.onEach { processCommands(it) } | ||
.launchIn(coroutineScope!!) | ||
} | ||
|
||
override fun onDetachedFromWindow() { | ||
super.onDetachedFromWindow() | ||
ViewTreeLifecycleOwner.get(this)?.lifecycle?.removeObserver(viewModel) | ||
coroutineScope?.cancel() | ||
job.cancel() | ||
coroutineScope = null | ||
} | ||
|
||
private fun processCommands(command: Command) { | ||
when (command) { | ||
NavigateToBookmarks -> navigateToBookmarks() | ||
} | ||
} | ||
|
||
private fun render(viewState: ViewState) { | ||
this.isVisible = viewState.warningVisible | ||
} | ||
|
||
private fun configureViewListeners() { | ||
binding.saveSiteRateLimitWarning.setClickableLink( | ||
"manage_bookmarks", | ||
context.getText(R.string.saved_site_limit_warning), | ||
onClick = { | ||
viewModel.onWarningActionClicked() | ||
}, | ||
) | ||
} | ||
|
||
private fun navigateToBookmarks() { | ||
globalActivityStarter.start(this.context, BookmarksScreenNoParams) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../src/main/java/com/duckduckgo/savedsites/impl/sync/SaveSitesRateLimitSyncMessagePlugin.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,31 @@ | ||
/* | ||
* 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.savedsites.impl.sync | ||
|
||
import android.content.Context | ||
import android.view.View | ||
import com.duckduckgo.di.scopes.ActivityScope | ||
import com.duckduckgo.sync.api.SyncMessagePlugin | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import javax.inject.Inject | ||
|
||
@ContributesMultibinding(scope = ActivityScope::class) | ||
class SaveSitesRateLimitSyncMessagePlugin @Inject constructor() : SyncMessagePlugin { | ||
override fun getView(context: Context): View { | ||
return SaveSiteRateLimitView(context) | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...tes-impl/src/main/java/com/duckduckgo/savedsites/impl/sync/SavedSiteRateLimitViewModel.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,83 @@ | ||
/* | ||
* 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.savedsites.impl.sync | ||
|
||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import com.duckduckgo.common.ui.notifyme.NotifyMeViewModel.Command | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.savedsites.impl.sync.DisplayModeViewModel.ViewState | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.channels.BufferOverflow.DROP_OLDEST | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import kotlinx.coroutines.flow.stateIn | ||
import kotlinx.coroutines.launch | ||
|
||
class SavedSiteRateLimitViewModel( | ||
private val savedSitesSyncStore: SavedSitesSyncStore, | ||
private val dispatcherProvider: DispatcherProvider, | ||
) : ViewModel(), DefaultLifecycleObserver { | ||
|
||
data class ViewState( | ||
val warningVisible: Boolean = false, | ||
) | ||
|
||
sealed class Command { | ||
data object NavigateToBookmarks : Command() | ||
} | ||
|
||
private val command = Channel<Command>(1, DROP_OLDEST) | ||
|
||
fun viewState(): Flow<ViewState> = savedSitesSyncStore.isSyncPausedFlow() | ||
.map { syncPaused -> | ||
ViewState( | ||
warningVisible = syncPaused, | ||
) | ||
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), ViewState()) | ||
|
||
fun commands(): Flow<Command> = command.receiveAsFlow() | ||
|
||
fun onWarningActionClicked() { | ||
viewModelScope.launch { | ||
command.send(Command.NavigateToBookmarks) | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
class Factory @Inject constructor( | ||
private val savedSitesSyncStore: SavedSitesSyncStore, | ||
private val dispatcherProvider: DispatcherProvider, | ||
) : ViewModelProvider.NewInstanceFactory() { | ||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
return with(modelClass) { | ||
when { | ||
isAssignableFrom(SavedSiteRateLimitViewModel::class.java) -> SavedSiteRateLimitViewModel( | ||
savedSitesSyncStore, | ||
dispatcherProvider, | ||
) | ||
else -> throw IllegalArgumentException("Unknown ViewModel class: ${modelClass.name}") | ||
} | ||
} as T | ||
} | ||
} | ||
} |
Oops, something went wrong.