Ads toggle for premium users

This commit is contained in:
Ilya Laktyushin
2024-04-04 14:14:39 +04:00
parent 06d5377bd4
commit 91f2f567c1
2 changed files with 43 additions and 3 deletions

View File

@@ -11891,3 +11891,4 @@ Sorry for the inconvenience.";
"Business.AdsTitle" = "ADS IN CHANNELS";
"Business.DontHideAds" = "Do Not Hide Ads";
"Business.AdsInfo" = "As a Premium subscriber, you don't see any ads on Telegram, but you can turn them on, for example, to view your own ads that you launched on the [Telegram Ad Platform >]()";
"Business.AdsInfo_URL" = "https://promote.telegram.org";

View File

@@ -1463,6 +1463,7 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent {
var isPremium: Bool?
var peer: EnginePeer?
var adsEnabled = false
private var disposable: Disposable?
private(set) var configuration = PremiumIntroConfiguration.defaultValue
@@ -1470,6 +1471,7 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent {
private var stickersDisposable: Disposable?
private var newPerksDisposable: Disposable?
private var preloadDisposableSet = DisposableSet()
private var adsEnabledDisposable: Disposable?
var price: String? {
return self.products?.first(where: { $0.id == self.selectedProductId })?.price
@@ -1491,6 +1493,8 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent {
}
}
var cachedChevronImage: (UIImage, PresentationTheme)?
init(
context: AccountContext,
source: PremiumSource,
@@ -1581,6 +1585,15 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent {
self.newPerks = newPerks
self.updated()
})
self.adsEnabledDisposable = (context.engine.data.subscribe(TelegramEngine.EngineData.Item.Peer.AdsEnabled(id: context.account.peerId))
|> deliverOnMainQueue).start(next: { [weak self] adsEnabled in
guard let self else {
return
}
self.adsEnabled = adsEnabled
self.updated()
})
}
deinit {
@@ -1588,6 +1601,7 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent {
self.preloadDisposableSet.dispose()
self.stickersDisposable?.dispose()
self.newPerksDisposable?.dispose()
self.adsEnabledDisposable?.dispose()
}
private var updatedPeerStatus: PeerEmojiStatus?
@@ -2491,13 +2505,27 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent {
maximumNumberOfLines: 1
))),
], alignment: .left, spacing: 2.0)),
accessory: .toggle(ListActionItemComponent.Toggle(style: .regular, isOn: false, action: { [weak state] value in
accessory: .toggle(ListActionItemComponent.Toggle(style: .regular, isOn: state.adsEnabled, action: { [weak state] value in
let _ = accountContext.engine.accountData.updateAdMessagesEnabled(enabled: value).startStandalone()
state?.updated(transition: .immediate)
})),
action: nil
))))
let adsInfoString = NSMutableAttributedString(attributedString: parseMarkdownIntoAttributedString(environment.strings.Business_AdsInfo, attributes: termsMarkdownAttributes, textAlignment: .natural
))
if state.cachedChevronImage == nil || state.cachedChevronImage?.1 !== theme {
state.cachedChevronImage = (generateTintedImage(image: UIImage(bundleImageName: "Contact List/SubtitleArrow"), color: environment.theme.list.itemAccentColor)!, theme)
}
if let range = adsInfoString.string.range(of: ">"), let chevronImage = state.cachedChevronImage?.0 {
adsInfoString.addAttribute(.attachment, value: chevronImage, range: NSRange(range, in: adsInfoString.string))
}
let controller = environment.controller
let adsInfoTapActionImpl: ([NSAttributedString.Key: Any]) -> Void = { _ in
if let controller = controller() as? PremiumIntroScreen {
controller.context.sharedContext.openExternalUrl(context: controller.context, urlContext: .generic, url: environment.strings.Business_AdsInfo_URL, forceExternal: true, presentationData: controller.context.sharedContext.currentPresentationData.with({$0}), navigationController: nil, dismissInput: {})
}
}
let adsSettingsSection = adsSettingsSection.update(
component: ListSectionComponent(
theme: environment.theme,
@@ -2510,8 +2538,19 @@ private final class PremiumIntroScreenContentComponent: CombinedComponent {
maximumNumberOfLines: 0
)),
footer: AnyComponent(MultilineTextComponent(
text: .markdown(text: environment.strings.Business_AdsInfo, attributes: termsMarkdownAttributes),
maximumNumberOfLines: 0
text: .plain(adsInfoString),
maximumNumberOfLines: 0,
highlightColor: environment.theme.list.itemAccentColor.withAlphaComponent(0.2),
highlightAction: { attributes in
if let _ = attributes[NSAttributedString.Key(rawValue: TelegramTextAttributes.URL)] {
return NSAttributedString.Key(rawValue: TelegramTextAttributes.URL)
} else {
return nil
}
},
tapAction: { attributes, _ in
adsInfoTapActionImpl(attributes)
}
)),
items: adsSettingsItems
),