Skip to content

Commit

Permalink
Show NoConnectionView for eligible sites with 0 connections
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdchr committed Jul 12, 2023
1 parent db4ddaa commit bd6c671
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,6 @@ private extension PrepublishingAutoSharingView {
/// The value-type data model that drives the `PrepublishingAutoSharingView`.
struct PrepublishingAutoSharingViewModel {

// MARK: Helper Models

/// A value-type representation of `PublicizeService` that's simplified for the needs of the auto-sharing view.
struct Service: Hashable {
let serviceName: PublicizeService.ServiceName
let connections: [Connection]

/// Whether the icon for this service should be opaque or transparent.
/// If at least one account is enabled, an opaque version should be shown.
var usesOpaqueIcon: Bool {
connections.reduce(false) { partialResult, connection in
return partialResult || connection.enabled
}
}

var enabledConnections: [Connection] {
connections.filter { $0.enabled }
}
}

struct Connection: Hashable {
let account: String
let enabled: Bool
}

// MARK: Properties

let services: [Service]
Expand Down Expand Up @@ -160,6 +135,31 @@ struct PrepublishingAutoSharingViewModel {
return String()
}
}

// MARK: Helper Models

/// A value-type representation of `PublicizeService` that's simplified for the needs of the auto-sharing view.
struct Service: Hashable {
let serviceName: PublicizeService.ServiceName
let connections: [Connection]

/// Whether the icon for this service should be opaque or transparent.
/// If at least one account is enabled, an opaque version should be shown.
var usesOpaqueIcon: Bool {
connections.reduce(false) { partialResult, connection in
return partialResult || connection.enabled
}
}

var enabledConnections: [Connection] {
connections.filter { $0.enabled }
}
}

struct Connection: Hashable {
let account: String
let enabled: Bool
}
}

private extension PrepublishingAutoSharingViewModel {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// Encapsulates logic related to Jetpack Social in the pre-publishing sheet.
///
extension PrepublishingViewController {

/// Determines whether the account and the post's blog is eligible to see auto-sharing options.
Expand All @@ -12,26 +14,46 @@ extension PrepublishingViewController {
}

func configureSocialCell(_ cell: UITableViewCell) {
// TODO:
// - Show the NoConnectionView if user has 0 connections.
let autoSharingView = UIView.embedSwiftUIView(PrepublishingAutoSharingView(model: makeAutoSharingViewModel()))
cell.contentView.addSubview(autoSharingView)

// Pin constraints to the cell's layoutMarginsGuide so that the content is properly aligned.
NSLayoutConstraint.activate([
autoSharingView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
autoSharingView.topAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.topAnchor),
autoSharingView.bottomAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.bottomAnchor),
autoSharingView.trailingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.trailingAnchor)
])
cell.accessoryType = .disclosureIndicator // TODO: only for autoSharingView.
if hasExistingConnections {
configureAutoSharingView(for: cell)
} else {
configureNoConnectionView(for: cell)
}
}
}

// MARK: - Helper Methods

private extension PrepublishingViewController {

var hasExistingConnections: Bool {
coreDataStack.performQuery { [postObjectID = post.objectID] context in
guard let post = (try? context.existingObject(with: postObjectID)) as? Post,
let connections = post.blog.connections as? Set<PublicizeConnection> else {
return false
}
return !connections.isEmpty
}
}

// MARK: Auto Sharing View

func configureAutoSharingView(for cell: UITableViewCell) {
let viewModel = makeAutoSharingViewModel()
let viewToEmbed = UIView.embedSwiftUIView(PrepublishingAutoSharingView(model: viewModel))
cell.contentView.addSubview(viewToEmbed)

// Pin constraints to the cell's layoutMarginsGuide so that the content is properly aligned.
NSLayoutConstraint.activate([
viewToEmbed.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
viewToEmbed.topAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.topAnchor),
viewToEmbed.bottomAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.bottomAnchor),
viewToEmbed.trailingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.trailingAnchor)
])

cell.accessoryType = .disclosureIndicator
}

func makeAutoSharingViewModel() -> PrepublishingAutoSharingViewModel {
return coreDataStack.performQuery { [postObjectID = post.objectID] context in
guard let post = (try? context.existingObject(with: postObjectID)) as? Post,
Expand Down Expand Up @@ -70,4 +92,36 @@ private extension PrepublishingViewController {
}
}

// MARK: - No Connection View

func configureNoConnectionView(for cell: UITableViewCell) {
let viewModel = makeNoConnectionViewModel()
guard let viewToEmbed = JetpackSocialNoConnectionView.createHostController(with: viewModel).view else {
return
}

cell.contentView.addSubview(viewToEmbed)
cell.contentView.pinSubviewToSafeArea(viewToEmbed)
}

func makeNoConnectionView() -> UIView {
let viewModel = makeNoConnectionViewModel()
let controller = JetpackSocialNoConnectionView.createHostController(with: viewModel)
return controller.view
}

func makeNoConnectionViewModel() -> JetpackSocialNoConnectionViewModel {
return coreDataStack.performQuery { [weak self] context in
guard let services = try? PublicizeService.allSupportedServices(in: context) else {
return .init()
}

// TODO: Tap actions
return .init(services: services, preferredBackgroundColor: self?.tableView.backgroundColor)
}
}

enum Constants {
static let socialCellBackgroundColor = UIColor(light: .listForeground, dark: .listBackground)
}
}

0 comments on commit bd6c671

Please sign in to comment.