Skip to content

Instantly share code, notes, and snippets.

@asilturk
Last active April 23, 2021 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asilturk/9466caaf953101779e0067a7ea1d8aab to your computer and use it in GitHub Desktop.
Save asilturk/9466caaf953101779e0067a7ea1d8aab to your computer and use it in GitHub Desktop.
Adapter Pattern
import UIKit
// MARK: - Base Protocol
typealias Result = ((Bool) -> Void)
typealias Err = ((Error?) -> Void)
protocol Share {
func post(image: UIImage?, text: String?, result: Result, error: Err?)
}
// MARK: - Share Helpers
class FacebookShareHelper: Share {
func post(image: UIImage?, text: String?, result: Result, error: Err?) {
/// .. sharing instructions
print("successful - shared: \(text)")
result(true)
}
}
class TwitterShareHelper: Share {
func post(image: UIImage?, text: String?, result: Result, error: Err?) {
/// .. sharing instructions
print("successful - shared: \(text)")
result(true)
}
}
class InstagramShareHelper {
func share(image: UIImage?) -> Bool {
/// .. sharing instructions
print("successful - shared")
return true
}
}
// MARK: - Adapter
class InstagramShareAdapter: Share {
private let instance = InstagramShareHelper()
func post(image: UIImage?, text: String?, result: Result, error: Err?) {
let success = instance.share(image: image)
result(success)
}
}
// MARK: - Base Class
enum Platform {
case facebook
case twitter
case instagram
case whatsapp
var platformName: String {
switch self {
case .facebook: return "Facebook"
case .twitter: return "Twitter"
case .instagram: return "Instagram"
case .whatsapp: return "Whatsapp"
}
}
}
// MARK: - Implementation
class ShareHelper {
private let services: [Platform: Share] = [
.facebook: FacebookShareHelper(),
.twitter: TwitterShareHelper(),
.instagram: InstagramShareAdapter(), // Implemented by Adapter Pattern Class
.whatsapp: WhatsappShareHelper(), // Implemented by Extension
]
func share(image: UIImage?, message: String?, platform: Platform) {
guard let service = services[platform] else {
return
}
service.post(image: image, text: message) { (result) in
print("Success: \(result) platform: \(platform.platformName)")
} error: { (err) in
print("error occured: \(err?.localizedDescription)")
}
}
}
// MARK: - Instance
let helper = ShareHelper()
helper.share(image: nil, message: "Burak", platform: .instagram)
helper.share(image: nil, message: "Furkan", platform: .twitter)
helper.share(image: nil, message: "Asilturk", platform: .facebook)
// MARK: - Adapter Extension
class WhatsappShareHelper {
func share(text: String?, image: UIImage?, userId: Int) -> Bool {
/// .. sharing instructions
print("successful - shared: \(text)")
return true
}
}
extension WhatsappShareHelper: Share {
var defaultUserId: Int { 0 }
func post(image: UIImage?, text: String?, result: (Bool) -> Void, error: Err?) {
let success = self.share(text: text, image: image, userId: defaultUserId)
result(success)
}
}
helper.share(image: nil, message: "Much more pretty.", platform: .whatsapp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment