forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
242 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") | ||
|
||
swift_library( | ||
name = "AppLovinAdProvider", | ||
module_name = "AppLovinAdProvider", | ||
srcs = glob([ | ||
"Sources/**/*.swift", | ||
]), | ||
deps = [ | ||
"@AppLovin//:AppLovin", | ||
"@swiftpkg_nicegram_assistant_ios//:Sources_NGAiChat", | ||
], | ||
visibility = ["//visibility:public"], | ||
|
||
) |
157 changes: 157 additions & 0 deletions
157
Nicegram/AppLovinAdProvider/Sources/AppLovinAdProvider.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,157 @@ | ||
import AppLovinSDK | ||
import NGAiChat | ||
import NGCore | ||
import NGRepoUser | ||
|
||
@available(iOS 13.0.0, *) | ||
public class AppLovinAdProvider: NSObject { | ||
|
||
// MARK: - Dependencies | ||
|
||
private let ad: MARewardedAd | ||
private let sdk: ALSdk | ||
private let userRepository: UserRepository | ||
|
||
// MARK: - Logic | ||
|
||
private var adViewId: String? | ||
private var showAdCompletion: ((ShowAdResult) -> Void)? | ||
|
||
// MARK: - Lifecycle | ||
|
||
public init(apiKey: String, adUnitIdentifier: String, userRepository: UserRepository) { | ||
let sdk = ALSdk.shared(withKey: apiKey)! | ||
|
||
self.ad = MARewardedAd.shared( | ||
withAdUnitIdentifier: adUnitIdentifier, | ||
sdk: sdk | ||
) | ||
self.sdk = sdk | ||
self.userRepository = userRepository | ||
|
||
super.init() | ||
|
||
ad.delegate = self | ||
} | ||
} | ||
|
||
@available(iOS 13.0.0, *) | ||
extension AppLovinAdProvider: AdProvider { | ||
public func initialize() { | ||
sdk.mediationProvider = "max" | ||
sdk.initializeSdk() | ||
} | ||
|
||
public func showAd() async -> ShowAdResult { | ||
return await withCheckedContinuation { continuation in | ||
self.showAd { result in | ||
continuation.resume(returning: result) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Private Functions | ||
|
||
@available(iOS 13.0.0, *) | ||
private extension AppLovinAdProvider { | ||
func showAd(completion: @escaping (ShowAdResult) -> Void) { | ||
self.showAdCompletion = completion | ||
|
||
if ad.isReady { | ||
internalShowAd() | ||
} else { | ||
ad.load() | ||
} | ||
} | ||
|
||
func internalShowAd() { | ||
let id = UUID().uuidString | ||
|
||
let data = CustomDataDTO( | ||
ad_view_id: id, | ||
user_token: userRepository.getCurrentUser()?.telegramToken | ||
) | ||
let dataJsonUrlEncoded: String? | ||
if let dataJson = try? JSONEncoder().encode(data), | ||
let dataJsonString = String(data: dataJson, encoding: .utf8) { | ||
dataJsonUrlEncoded = dataJsonString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) | ||
} else { | ||
dataJsonUrlEncoded = nil | ||
} | ||
|
||
self.adViewId = id | ||
self.ad.show( | ||
forPlacement: nil, | ||
customData: dataJsonUrlEncoded | ||
) | ||
} | ||
|
||
func completeSuccessfulAd() { | ||
guard let adViewId else { | ||
completeFailedAd(error: nil) | ||
return | ||
} | ||
self.showAdCompletion?(.success(id: adViewId)) | ||
self.clearCachedData() | ||
} | ||
|
||
func completeFailedAd(error maError: MAError?) { | ||
let error: Error? | ||
if let maError { | ||
error = MessageError(message: maError.message) | ||
} else { | ||
error = nil | ||
} | ||
|
||
self.showAdCompletion?(.error(error)) | ||
self.clearCachedData() | ||
} | ||
|
||
func clearCachedData() { | ||
self.adViewId = nil | ||
self.showAdCompletion = nil | ||
} | ||
} | ||
|
||
// MARK: - MAAdDelegate | ||
|
||
@available(iOS 13.0.0, *) | ||
extension AppLovinAdProvider: MARewardedAdDelegate { | ||
public func didRewardUser(for ad: MAAd, with reward: MAReward) { | ||
completeSuccessfulAd() | ||
} | ||
|
||
public func didLoad(_ ad: MAAd) { | ||
internalShowAd() | ||
} | ||
|
||
public func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) { | ||
completeFailedAd(error: error) | ||
} | ||
|
||
public func didDisplay(_ ad: MAAd) { | ||
// Do nothing | ||
} | ||
|
||
public func didHide(_ ad: MAAd) { | ||
// Do nothing | ||
} | ||
|
||
public func didClick(_ ad: MAAd) { | ||
// Do nothing | ||
} | ||
|
||
public func didFail(toDisplay ad: MAAd, withError error: MAError) { | ||
completeFailedAd(error: error) | ||
} | ||
} | ||
|
||
// MARK: - Helpers | ||
|
||
private struct CustomDataDTO: Encodable { | ||
let ad_view_id: String | ||
let user_token: String? | ||
} | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,16 +42,16 @@ | |
"location" : "[email protected]:mobyrix/nicegram-assistant-ios.git", | ||
"state" : { | ||
"branch" : "develop", | ||
"revision" : "18b31ec099ddf2862fd58498ef9c2476fc898479" | ||
"revision" : "4387374d1e966763ae5add0d439e59ac0113e717" | ||
} | ||
}, | ||
{ | ||
"identity" : "sdwebimage", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/SDWebImage/SDWebImage.git", | ||
"state" : { | ||
"revision" : "20df851f2ae27efbaeeff73e9babdf4fd839a144", | ||
"version" : "5.15.6" | ||
"revision" : "3289629ef6cbf1ad8c3d1dccf0cf09ac97547cd6", | ||
"version" : "5.15.7" | ||
} | ||
}, | ||
{ | ||
|
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
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 |
---|---|---|
|
@@ -36,15 +36,15 @@ def swift_dependencies(): | |
# branch: develop | ||
swift_package( | ||
name = "swiftpkg_nicegram_assistant_ios", | ||
commit = "18b31ec099ddf2862fd58498ef9c2476fc898479", | ||
commit = "4387374d1e966763ae5add0d439e59ac0113e717", | ||
dependencies_index = "@//:swift_deps_index.json", | ||
remote = "[email protected]:mobyrix/nicegram-assistant-ios.git", | ||
) | ||
|
||
# version: 5.15.5 | ||
swift_package( | ||
name = "swiftpkg_sdwebimage", | ||
commit = "20df851f2ae27efbaeeff73e9babdf4fd839a144", | ||
commit = "3289629ef6cbf1ad8c3d1dccf0cf09ac97547cd6", | ||
dependencies_index = "@//:swift_deps_index.json", | ||
remote = "https://github.com/SDWebImage/SDWebImage.git", | ||
) | ||
|
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 |
---|---|---|
|
@@ -380,7 +380,7 @@ | |
"name": "swiftpkg_nicegram_assistant_ios", | ||
"identity": "nicegram-assistant-ios", | ||
"remote": { | ||
"commit": "18b31ec099ddf2862fd58498ef9c2476fc898479", | ||
"commit": "4387374d1e966763ae5add0d439e59ac0113e717", | ||
"remote": "[email protected]:mobyrix/nicegram-assistant-ios.git", | ||
"branch": "develop" | ||
} | ||
|
@@ -389,9 +389,9 @@ | |
"name": "swiftpkg_sdwebimage", | ||
"identity": "sdwebimage", | ||
"remote": { | ||
"commit": "20df851f2ae27efbaeeff73e9babdf4fd839a144", | ||
"commit": "3289629ef6cbf1ad8c3d1dccf0cf09ac97547cd6", | ||
"remote": "https://github.com/SDWebImage/SDWebImage.git", | ||
"version": "5.15.6" | ||
"version": "5.15.7" | ||
} | ||
}, | ||
{ | ||
|
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,15 @@ | ||
load( | ||
"@build_bazel_rules_apple//apple:apple.bzl", | ||
"apple_static_xcframework_import" | ||
) | ||
|
||
apple_static_xcframework_import( | ||
name = "AppLovin", | ||
xcframework_imports = glob([ | ||
"applovin-ios-sdk-11.9.0/AppLovinSDK.xcframework/**", | ||
]), | ||
deps = [ | ||
|
||
], | ||
visibility = ["//visibility:public"], | ||
) |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"app": "1.2.8", | ||
"app": "1.2.9", | ||
"bazel": "5.4.0", | ||
"xcode": "14.2" | ||
} |