forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 72
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
30 changed files
with
502 additions
and
198 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library") | ||
|
||
swift_library( | ||
name = "NGCopyProtectedContent", | ||
module_name = "NGCopyProtectedContent", | ||
srcs = glob([ | ||
"Sources/**/*.swift", | ||
]), | ||
deps = [ | ||
"//Nicegram/NGCore:NGCore", | ||
"//Nicegram/NGData:NGData", | ||
"//Nicegram/NGRemoteConfig:NGRemoteConfig", | ||
"//Nicegram/NGSubscription:NGSubscription", | ||
], | ||
visibility = [ | ||
"//visibility:public", | ||
], | ||
) |
56 changes: 56 additions & 0 deletions
56
Nicegram/NGCopyProtectedContent/Sources/CopyProtectedContent.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 @@ | ||
import Foundation | ||
import NGCore | ||
import NGData | ||
import NGRemoteConfig | ||
import NGSubscription | ||
import Postbox | ||
import TelegramPresentationData | ||
import UIKit | ||
|
||
// MARK: - Logic | ||
|
||
public func shouldShowInterfaceForCopyContent(message: Message) -> Bool { | ||
let isCopyProtectionEnabled = message.isCopyProtected() | ||
return !isCopyProtectionEnabled || allowCopyProtectedContent | ||
} | ||
|
||
public func shouldShowInterfaceForForwardAsCopy(message: Message) -> Bool { | ||
let isCopyProtectionEnabled = message.isCopyProtected() | ||
|
||
if isCopyProtectionEnabled { | ||
let hasMedia = !message.media.isEmpty | ||
return !hasMedia && shouldShowInterfaceForCopyContent(message: message) | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
public func shouldSubscribeToCopyContent(message: Message) -> Bool { | ||
let isCopyProtectionEnabled = message.isCopyProtected() | ||
|
||
if isCopyProtectionEnabled { | ||
return !isPremium() && !getBypassCopyProtection() | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
public func routeToNicegramPremiumForCopyContent(presentationData: PresentationData) { | ||
let c = SubscriptionBuilderImpl(presentationData: presentationData).build() | ||
c.modalPresentationStyle = .fullScreen | ||
if let topViewController = UIApplication.topViewController { | ||
topViewController.present(c, animated: true) | ||
} | ||
} | ||
|
||
// MARK: - Bypass setting (Secret Menu) | ||
|
||
private let bypassCopyProtectionKey = "ng:bypassCopyProtection" | ||
|
||
public func getBypassCopyProtection() -> Bool { | ||
return UserDefaults.standard.bool(forKey: bypassCopyProtectionKey) | ||
} | ||
|
||
public func setBypassCopyProtection(_ value: Bool) { | ||
UserDefaults.standard.set(value, forKey: bypassCopyProtectionKey) | ||
} |
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,37 +1,100 @@ | ||
import NGLoadingIndicator | ||
import EsimUI | ||
import SnapKit | ||
import UIKit | ||
import Lottie | ||
|
||
public class LoadingView: UIView { | ||
|
||
// MARK: - UI Elements | ||
|
||
private let activityIndicator: AnimationView | ||
private let dimmView = UIView() | ||
|
||
// MARK: - Public Properties | ||
|
||
public var isLoading: Bool = false { | ||
didSet { | ||
if isLoading, !isLoadingAnimationInFlight { | ||
loadingInicator.startAnimating(on: containerView) | ||
isLoadingAnimationInFlight = true | ||
} else if !isLoading, isLoadingAnimationInFlight { | ||
loadingInicator.stopAnimating() | ||
isLoadingAnimationInFlight = false | ||
if isLoading { | ||
start() | ||
} else { | ||
stop() | ||
} | ||
} | ||
} | ||
|
||
public var hidesWhenStopped: Bool = true | ||
public var dimmBackground: Bool = false | ||
|
||
// MARK: - Logic | ||
|
||
private let loadingInicator = NGLoadingIndicator() | ||
private var isLoadingAnimationInFlight: Bool = false | ||
private weak var containerView: UIView? | ||
private var isAnimationInFlight: Bool = false | ||
|
||
// MARK: - Lifecycle | ||
|
||
public init(containerView: UIView?) { | ||
self.containerView = containerView | ||
public override init(frame: CGRect) { | ||
self.activityIndicator = AnimationView(name: "NicegramLoader") | ||
|
||
super.init(frame: frame) | ||
|
||
isHidden = true | ||
|
||
activityIndicator.loopMode = .loop | ||
|
||
super.init(frame: .zero) | ||
dimmView.backgroundColor = .black.withAlphaComponent(0.5) | ||
|
||
addSubview(dimmView) | ||
dimmView.snp.makeConstraints { make in | ||
make.edges.equalToSuperview() | ||
} | ||
|
||
addSubview(activityIndicator) | ||
activityIndicator.snp.makeConstraints { make in | ||
make.center.equalTo(safeAreaLayoutGuide) | ||
make.leading.top.greaterThanOrEqualToSuperview() | ||
make.height.equalTo(activityIndicator.snp.width) | ||
make.width.equalTo(80).priority(999) | ||
} | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// MARK: - Privtae Functions | ||
|
||
private func start() { | ||
guard !isAnimationInFlight else { return } | ||
isAnimationInFlight = true | ||
|
||
isHidden = false | ||
dimmView.isHidden = !dimmBackground | ||
UIView.animate(withDuration: 0.2) { [weak self] in | ||
guard let self else { return } | ||
self.activityIndicator.alpha = 1 | ||
} completion: { [weak self] _ in | ||
guard let self else { return } | ||
self.activityIndicator.play() | ||
} | ||
} | ||
|
||
private func stop() { | ||
activityIndicator.stop() | ||
if hidesWhenStopped { | ||
isHidden = true | ||
} | ||
|
||
isAnimationInFlight = false | ||
} | ||
} | ||
|
||
public extension PlaceholderableView { | ||
func showLoading() { | ||
let view = LoadingView() | ||
view.isLoading = true | ||
self.showPlaceholder(view) | ||
} | ||
|
||
func hideLoading() { | ||
self.hidePlaceholder() | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Nicegram/NGCoreUI/Sources/Placeholder/PlaceholderState+Factory.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,22 @@ | ||
import NGLocalization | ||
import UIKit | ||
|
||
public extension PlaceholderState { | ||
static func retry(error: Error, onTap: @escaping () -> Void) -> PlaceholderState { | ||
return retry(message: error.localizedDescription, onTap: onTap) | ||
} | ||
|
||
static func retry(message: String?, onTap: @escaping () -> Void) -> PlaceholderState { | ||
return PlaceholderState( | ||
title: nil, | ||
image: nil, | ||
description: mapErrorDescription(message), | ||
buttonState: .init( | ||
title: ngLocalized("Nicegram.Alert.TryAgain").uppercased(), | ||
image: UIImage(named: "ng.refresh"), | ||
style: .small, | ||
onTap: onTap | ||
) | ||
) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
Nicegram/NGCoreUI/Sources/Placeholder/PlaceholderState.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,34 @@ | ||
import UIKit | ||
|
||
public struct PlaceholderState { | ||
public let title: String? | ||
public let image: UIImage? | ||
public let description: String | ||
public let buttonState: ButtonState? | ||
|
||
public init(title: String?, image: UIImage?, description: String, buttonState: ButtonState?) { | ||
self.title = title | ||
self.image = image | ||
self.description = description | ||
self.buttonState = buttonState | ||
} | ||
|
||
public struct ButtonState { | ||
public let title: String | ||
public let image: UIImage? | ||
public let style: Style | ||
public let onTap: () -> Void | ||
|
||
public init(title: String, image: UIImage? = nil, style: Style = .normal, onTap: @escaping () -> Void) { | ||
self.title = title | ||
self.image = image | ||
self.style = style | ||
self.onTap = onTap | ||
} | ||
|
||
public enum Style { | ||
case normal | ||
case small | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
Nicegram/NGCoreUI/Sources/Placeholder/PlaceholderableView.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,25 @@ | ||
import NGCustomViews | ||
import SnapKit | ||
import UIKit | ||
|
||
public typealias PlaceholderableView = NGCustomViews.PlaceholderableView | ||
|
||
public extension PlaceholderableView { | ||
func showPlaceholder(_ state: PlaceholderState) { | ||
let view = DefaultPlaceholderView() | ||
|
||
switch state.buttonState?.style { | ||
case .small: | ||
view.configureWithSmallButton() | ||
case .normal, .none: | ||
break | ||
} | ||
|
||
view.display(image: state.image, description: state.description, buttonTitle: state.buttonState?.title, buttonImage: state.buttonState?.image) | ||
|
||
view.onButtonClick = state.buttonState?.onTap | ||
|
||
self.showPlaceholder(view) | ||
} | ||
} | ||
|
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.