-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Bosco Ho <[email protected]> (cherry picked from commit 3a2a7c0)
- Loading branch information
Showing
16 changed files
with
301 additions
and
53 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
68 changes: 68 additions & 0 deletions
68
Mlem/Models/Content/Community/CommunityModel+MenuFunctions.swift
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,68 @@ | ||
// | ||
// CommunityModel+MenuFunctions.swift | ||
// Mlem | ||
// | ||
// Created by Sjmarf on 09/11/2023. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
extension CommunityModel { | ||
func subscribeMenuFunction(_ callback: @escaping (_ item: Self) -> Void = { _ in }) throws -> MenuFunction { | ||
guard let subscribed else { | ||
throw CommunityError.noData | ||
} | ||
return .standardMenuFunction( | ||
text: subscribed ? "Unsubscribe" : "Subscribe", | ||
imageName: subscribed ? Icons.unsubscribe : Icons.subscribe, | ||
destructiveActionPrompt: subscribed ? "Are you sure you want to unsubscribe from \(name)?" : nil, | ||
enabled: true, | ||
callback: { | ||
Task { | ||
var new = self | ||
do { | ||
try await new.toggleSubscribe(callback) | ||
} catch { | ||
errorHandler.handle(error) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
|
||
func blockMenuFunction(_ callback: @escaping (_ item: Self) -> Void = { _ in }) throws -> MenuFunction { | ||
guard let blocked else { | ||
throw CommunityError.noData | ||
} | ||
return .standardMenuFunction( | ||
text: blocked ? "Unblock" : "Block", | ||
imageName: blocked ? Icons.show : Icons.hide, | ||
destructiveActionPrompt: blocked ? nil : AppConstants.blockCommunityPrompt, | ||
enabled: true, | ||
callback: { | ||
Task { | ||
var new = self | ||
do { | ||
try await new.toggleBlock(callback) | ||
} catch { | ||
errorHandler.handle(error) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
|
||
func menuFunctions(_ callback: @escaping (_ item: Self) -> Void = { _ in }) -> [MenuFunction] { | ||
var functions: [MenuFunction] = .init() | ||
if let function = try? subscribeMenuFunction(callback) { | ||
functions.append(function) | ||
} | ||
functions.append(.shareMenuFunction(url: communityUrl)) | ||
if let function = try? blockMenuFunction(callback) { | ||
functions.append(function) | ||
} | ||
|
||
return functions | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
Mlem/Models/Content/Community/CommunityModel+SwipeActions.swift
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,56 @@ | ||
// | ||
// CommunityModel+SwipeActions.swift | ||
// Mlem | ||
// | ||
// Created by Sjmarf on 10/11/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
extension CommunityModel { | ||
|
||
func subscribeSwipeAction( | ||
_ callback: @escaping (_ item: Self) -> Void = { _ in }, | ||
confirmDestructive: ((StandardMenuFunction) -> Void)? = nil | ||
) throws -> SwipeAction { | ||
guard let subscribed else { | ||
throw CommunityError.noData | ||
} | ||
let (emptySymbolName, fullSymbolName) = (subscribed) | ||
? (Icons.unsubscribePerson, Icons.unsubscribePersonFill) | ||
: (Icons.subscribePerson, Icons.subscribePersonFill) | ||
return SwipeAction( | ||
symbol: .init(emptyName: emptySymbolName, fillName: fullSymbolName), | ||
color: subscribed ? .red : .green, | ||
action: { | ||
Task { | ||
hapticManager.play(haptic: .lightSuccess, priority: .low) | ||
|
||
if subscribed, let confirmDestructive { | ||
if case .standard(let function) = try? subscribeMenuFunction(callback) { | ||
confirmDestructive(function) | ||
} | ||
} else { | ||
var new = self | ||
do { | ||
try await new.toggleSubscribe(callback) | ||
} catch { | ||
errorHandler.handle(error) | ||
} | ||
} | ||
} | ||
} | ||
) | ||
} | ||
|
||
func swipeActions( | ||
_ callback: @escaping (_ item: Self) -> Void = { _ in }, | ||
confirmDestructive: ((StandardMenuFunction) -> Void)? = nil | ||
) -> SwipeConfiguration { | ||
var trailingActions: [SwipeAction] = [] | ||
if let action = try? subscribeSwipeAction(callback, confirmDestructive: confirmDestructive) { | ||
trailingActions.append(action) | ||
} | ||
return SwipeConfiguration(leadingActions: [], trailingActions: trailingActions) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// UserModel+MenuFunctions.swift | ||
// Mlem | ||
// | ||
// Created by Sjmarf on 10/11/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
extension UserModel { | ||
func blockMenuFunction(_ callback: @escaping (_ item: Self) -> Void = { _ in }) -> MenuFunction { | ||
return .standardMenuFunction( | ||
text: blocked ? "Unblock" : "Block", | ||
imageName: blocked ? Icons.show : Icons.hide, | ||
destructiveActionPrompt: blocked ? nil : AppConstants.blockUserPrompt, | ||
enabled: true, | ||
callback: { | ||
Task { | ||
var new = self | ||
await new.toggleBlock(callback) | ||
} | ||
} | ||
) | ||
} | ||
|
||
func menuFunctions(_ callback: @escaping (_ item: Self) -> Void = { _ in }) -> [MenuFunction] { | ||
var functions: [MenuFunction] = .init() | ||
functions.append(.shareMenuFunction(url: profileUrl)) | ||
functions.append(blockMenuFunction(callback)) | ||
return functions | ||
} | ||
} |
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
Oops, something went wrong.