Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOS-2634 HomeWidgets | hide bin for read-only #1192

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,25 @@ struct HomeWidgetsView: View {
HomeTopShadow()
} content: {
VStack(spacing: 12) {
SpaceWidgetView {
model.onSpaceSelected()
}
if #available(iOS 17.0, *) {
WidgetSwipeTipView()
}
ForEach(model.models) { rowModel in
rowModel.provider.view
}
BinLinkWidgetView(spaceId: model.spaceId, homeState: $model.homeState, output: model.submoduleOutput())
if model.dataLoaded {
HomeEditButton(text: Loc.Widgets.Actions.editWidgets) {
SpaceWidgetView {
model.onSpaceSelected()
}
if #available(iOS 17.0, *) {
WidgetSwipeTipView()
}
ForEach(model.models) { rowModel in
rowModel.provider.view
}
BinLinkWidgetView(spaceId: model.spaceId, homeState: $model.homeState, output: model.submoduleOutput())
HomeEditButton(text: Loc.Widgets.Actions.editWidgets, homeState: model.homeState) {
model.onEditButtonTap()
}
.opacity(model.homeState.isReadWrite ? 1 : 0)
.animation(.default, value: model.homeState)
}
AnytypeNavigationSpacer()
}
.padding(.horizontal, 20)
.padding(.top, 12)
.opacity(model.dataLoaded ? 1 : 0)
.fitIPadToReadableContentGuide()
}
.animation(.default, value: model.models.count)
Expand All @@ -42,6 +39,9 @@ struct HomeWidgetsView: View {
model.onCreateWidgetSelected()
}
}
.task {
await model.startWidgetObjectTask()
}
.task {
await model.startParticipantTask()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation
import AnytypeCore
import Services
import Combine
import SwiftUI

@MainActor
final class HomeWidgetsViewModel: ObservableObject {
Expand All @@ -28,7 +29,6 @@ final class HomeWidgetsViewModel: ObservableObject {
@Published var wallpaper: BackgroundType = .default

var spaceId: String { info.accountSpaceId }
private var objectSubscriptions = [AnyCancellable]()

init(
info: AccountInfo,
Expand Down Expand Up @@ -57,6 +57,19 @@ final class HomeWidgetsViewModel: ObservableObject {
setupInitialState()
}

func startWidgetObjectTask() async {
for await _ in widgetObject.syncPublisher.values {
let blocks = widgetObject.children.filter(\.isWidget)
recentStateManagerProtocol.setupRecentStateIfNeeded(blocks: blocks, widgetObject: widgetObject)
let providers = registry.providers(blocks: blocks, widgetObject: widgetObject)

guard providers != models else { continue }

models = providers
dataLoaded = true
}
}

func startParticipantTask() async {
for await canEdit in accountParticipantStorage.canEditPublisher(spaceId: info.accountSpaceId).values {
stateManager.setHomeState(canEdit ? .readwrite : .readonly)
Expand Down Expand Up @@ -108,21 +121,6 @@ final class HomeWidgetsViewModel: ObservableObject {
// MARK: - Private

private func setupInitialState() {
widgetObject.syncPublisher
.map { [weak self] _ -> [HomeWidgetSubmoduleModel] in
guard let self else { return [] }
let blocks = self.widgetObject.children.filter(\.isWidget)
recentStateManagerProtocol.setupRecentStateIfNeeded(blocks: blocks, widgetObject: self.widgetObject)
return registry.providers(blocks: blocks, widgetObject: widgetObject)
}
.removeDuplicates()
.receiveOnMain()
.sink { [weak self] models in
self?.models = models
self?.dataLoaded = true
}
.store(in: &objectSubscriptions)

stateManager.homeStatePublisher
.receiveOnMain()
.assign(to: &$homeState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SwiftUI
struct HomeEditButton: View {

let text: String
let homeState: HomeWidgetsState
let action: () -> Void

var body: some View {
Expand All @@ -17,5 +18,7 @@ struct HomeEditButton: View {
)
.background(.thinMaterial)
.cornerRadius(8, style: .continuous)
.opacity(homeState.isReadWrite ? 1 : 0)
.animation(.default, value: homeState)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ private struct BinLinkWidgetViewInternal: View {
}

var body: some View {
if homeState.isReadWrite || homeState.isEditWidgets {
content
}
}

var content: some View {
WidgetContainerViewNew(
title: Loc.bin,
icon: .Widget.bin,
Expand Down
Loading