Commit 739fc69e authored by emrcftci's avatar emrcftci Committed by Amritpal Sandhu
Browse files

update(*): Refactor iOS App

 - Delete redundant selfs

 - Move delegate methods to extension

 - Fix early-exit guards

 - Delete redundant semicolons

 - Update for loops with forEach blocks
parent 79b72e41
......@@ -8,7 +8,7 @@
import UIKit
class AllStickerPacksViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
class AllStickerPacksViewController: UIViewController {
@IBOutlet private weak var stickerPacksTableView: UITableView!
......@@ -22,15 +22,15 @@ class AllStickerPacksViewController: UIViewController, UITableViewDataSource, UI
navigationItem.largeTitleDisplayMode = .automatic
}
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.alpha = 0.0;
navigationController?.navigationBar.alpha = 0.0
stickerPacksTableView.register(UINib(nibName: "StickerPackTableViewCell", bundle: nil), forCellReuseIdentifier: "StickerPackCell")
stickerPacksTableView.tableFooterView = UIView(frame: .zero)
stickerPacksTableView.tableFooterView = UIView()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let selectedIndex = self.selectedIndex {
if let selectedIndex = selectedIndex {
stickerPacksTableView.deselectRow(at: selectedIndex, animated: true)
}
}
......@@ -43,19 +43,19 @@ class AllStickerPacksViewController: UIViewController, UITableViewDataSource, UI
self.needsFetchStickerPacks = false
self.fetchStickerPacks()
}))
present(alert, animated: true, completion:nil)
present(alert, animated: true)
}
}
private func fetchStickerPacks() {
let loadingAlert: UIAlertController = UIAlertController(title: "Loading sticker packs", message: "\n\n", preferredStyle: .alert)
let loadingAlert = UIAlertController(title: "Loading sticker packs", message: "\n\n", preferredStyle: .alert)
loadingAlert.addSpinner()
present(loadingAlert, animated: true, completion: nil)
present(loadingAlert, animated: true)
do {
try StickerPackManager.fetchStickerPacks(fromJSON: StickerPackManager.stickersJSON(contentsOfFile: "sticker_packs")) { stickerPacks in
loadingAlert.dismiss(animated: false, completion: {
self.navigationController?.navigationBar.alpha = 1.0;
loadingAlert.dismiss(animated: false) {
self.navigationController?.navigationBar.alpha = 1.0
if stickerPacks.count > 1 {
self.stickerPacks = stickerPacks
......@@ -63,7 +63,7 @@ class AllStickerPacksViewController: UIViewController, UITableViewDataSource, UI
} else {
self.show(stickerPack: stickerPacks[0], animated: false)
}
})
}
}
} catch StickerPackError.fileNotFound {
fatalError("sticker_packs.wasticker not found.")
......@@ -87,37 +87,9 @@ class AllStickerPacksViewController: UIViewController, UITableViewDataSource, UI
}
}
// MARK: Tableview
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stickerPacks.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: StickerPackTableViewCell = tableView.dequeueReusableCell(withIdentifier: "StickerPackCell") as! StickerPackTableViewCell
cell.stickerPack = stickerPacks[indexPath.row]
let addButton: UIButton = UIButton(type: .contactAdd)
addButton.tag = indexPath.row
addButton.isEnabled = Interoperability.canSend()
addButton.addTarget(self, action: #selector(addButtonTapped(button:)), for: .touchUpInside)
cell.accessoryView = addButton
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedIndex = indexPath
show(stickerPack: stickerPacks[indexPath.row], animated: true)
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if let navigationBar = navigationController?.navigationBar {
let contentInset: UIEdgeInsets = {
if #available(iOS 11.0, *) {
return scrollView.adjustedContentInset
......@@ -125,6 +97,7 @@ class AllStickerPacksViewController: UIViewController, UITableViewDataSource, UI
return scrollView.contentInset
}
}()
if scrollView.contentOffset.y <= -contentInset.top {
navigationBar.shadowImage = UIImage()
} else {
......@@ -136,10 +109,47 @@ class AllStickerPacksViewController: UIViewController, UITableViewDataSource, UI
@objc func addButtonTapped(button: UIButton) {
let loadingAlert: UIAlertController = UIAlertController(title: "Sending to WhatsApp", message: "\n\n", preferredStyle: .alert)
loadingAlert.addSpinner()
present(loadingAlert, animated: true, completion: nil)
present(loadingAlert, animated: true)
stickerPacks[button.tag].sendToWhatsApp { completed in
loadingAlert.dismiss(animated: true, completion: nil)
loadingAlert.dismiss(animated: true)
}
}
}
// MARK: - UITableViewDelegate
extension AllStickerPacksViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedIndex = indexPath
show(stickerPack: stickerPacks[indexPath.row], animated: true)
}
}
// MARK: - UITableViewDataSource
extension AllStickerPacksViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stickerPacks.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell: StickerPackTableViewCell = tableView.dequeueReusableCell(withIdentifier: "StickerPackCell") as? StickerPackTableViewCell else { return UITableViewCell() }
cell.stickerPack = stickerPacks[indexPath.row]
let addButton = UIButton(type: .contactAdd)
addButton.tag = indexPath.row
addButton.isEnabled = Interoperability.canSend()
addButton.addTarget(self, action: #selector(addButtonTapped(button:)), for: .touchUpInside)
cell.accessoryView = addButton
return cell
}
}
......@@ -17,27 +17,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
......@@ -17,7 +17,7 @@ class RoundedButton: UIButton {
layer.masksToBounds = true
layer.cornerRadius = 10.0
titleLabel?.font = UIFont.boldSystemFont(ofSize: titleLabel!.font.pointSize)
titleLabel?.font = .boldSystemFont(ofSize: titleLabel!.font.pointSize)
}
required init?(coder aDecoder: NSCoder) {
......@@ -41,11 +41,10 @@ class AquaButton: RoundedButton {
override var isEnabled: Bool {
didSet{
if self.isEnabled {
imageView!.tintColor = .white
}
else{
imageView!.tintColor = .lightGray
if isEnabled {
imageView!.tintColor = .white
} else {
imageView!.tintColor = .lightGray
}
}
}
......@@ -85,11 +84,10 @@ class GrayRoundedButton: RoundedButton {
override var isEnabled: Bool {
didSet{
if self.isEnabled {
self.tintColor = UIColor.white
}
else{
self.tintColor = UIColor.gray
if isEnabled {
tintColor = UIColor.white
} else{
tintColor = UIColor.gray
}
}
}
......
......@@ -79,7 +79,7 @@ class ImageData {
* Returns a UIImage of the current image data. If data is corrupt, nil will be returned.
*/
lazy var image: UIImage? = {
if type == ImageDataExtension.webp {
if type == .webp {
return WebPManager.shared.decode(webPData: data)
} else {
return UIImage(data: data)
......@@ -90,9 +90,7 @@ class ImageData {
* Returns an image with the new size.
*/
func image(withSize size: CGSize) -> UIImage? {
guard let image = self.image else {
return nil
}
guard let image = image else { return nil }
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
image.draw(in: CGRect(origin: .zero, size: size))
......
......@@ -22,13 +22,11 @@ struct Interoperability {
}
static func send(json: [String: Any]) -> Bool {
if let bundleIdentifier = Bundle.main.bundleIdentifier {
if bundleIdentifier.contains(DefaultBundleIdentifier) {
fatalError("Your bundle identifier must not include the default one.");
}
if Bundle.main.bundleIdentifier?.contains(DefaultBundleIdentifier) == true {
fatalError("Your bundle identifier must not include the default one.")
}
let pasteboard: UIPasteboard = UIPasteboard.general
let pasteboard: UIPasteboard = .general
var jsonWithAppStoreLink: [String: Any] = json
jsonWithAppStoreLink["ios_app_store_link"] = iOSAppStoreLink
......@@ -37,15 +35,17 @@ struct Interoperability {
guard let dataToSend = try? JSONSerialization.data(withJSONObject: jsonWithAppStoreLink, options: []) else {
return false
}
if #available(iOS 10.0, *) {
pasteboard.setItems([[PasteboardStickerPackDataType: dataToSend]], options: [UIPasteboardOption.localOnly: true, UIPasteboardOption.expirationDate: NSDate(timeIntervalSinceNow: PasteboardExpirationSeconds)])
} else {
pasteboard.setData(dataToSend, forPasteboardType: PasteboardStickerPackDataType)
}
DispatchQueue.main.async {
if canSend() {
if #available(iOS 10.0, *) {
UIApplication.shared.open(WhatsAppURL, options: [:], completionHandler: nil)
UIApplication.shared.open(WhatsAppURL)
} else {
UIApplication.shared.openURL(WhatsAppURL)
}
......
......@@ -9,29 +9,30 @@
import UIKit
struct StickerEmojis {
static func canonicalizedEmojis(rawEmojis: [String]?) throws -> [String]?{
if let rawEmojis = rawEmojis {
guard rawEmojis.count <= Limits.MaxEmojisCount else {
throw StickerPackError.tooManyEmojis
}
var canonicalizedEmojis: [String] = []
static func canonicalizedEmojis(rawEmojis: [String]?) throws -> [String]? {
guard let rawEmojis = rawEmojis else { return nil }
for rawEmoji in rawEmojis {
var emojiToAdd = canonicalizedEmoji(emoji: rawEmoji)
if rawEmojis.count > Limits.MaxEmojisCount {
throw StickerPackError.tooManyEmojis
}
// If the emoji somehow isn't canonicalized, we'll use the original emoji
if (emojiToAdd == "") {
emojiToAdd = rawEmoji
}
var canonicalizedEmojis: [String] = []
canonicalizedEmojis.append(emojiToAdd)
}
rawEmojis.forEach { rawEmoji in
return canonicalizedEmojis
var emojiToAdd = canonicalizedEmoji(emoji: rawEmoji)
// If the emoji somehow isn't canonicalized, we'll use the original emoji
if emojiToAdd.isEmpty {
emojiToAdd = rawEmoji
}
canonicalizedEmojis.append(emojiToAdd)
}
return nil
return canonicalizedEmojis
}
private static func canonicalizedEmoji(emoji: String) -> String {
......@@ -48,18 +49,15 @@ struct StickerEmojis {
0x1F900...0x1F9FF, // Supplemental symbols and pictographs
0x200D: // Zero-width joiner
nonExtensionUnicodes.append(Character(UnicodeScalar(scalar.value)!))
break
default:
continue
}
}
var canonicalizedEmoji: String = ""
for nonExtensionUnicode in nonExtensionUnicodes {
canonicalizedEmoji.append(nonExtensionUnicode)
}
var canonicalizedEmoji = ""
nonExtensionUnicodes.forEach { canonicalizedEmoji.append($0) }
return canonicalizedEmoji
}
}
......
......@@ -40,9 +40,7 @@ class StickerPack {
var bytesSize: Int64 {
var totalBytes: Int64 = Int64(name.utf8.count + publisher.utf8.count + trayImage.data.count)
for sticker in stickers {
totalBytes += sticker.bytesSize
}
stickers.forEach { totalBytes += $0.bytesSize }
return totalBytes
}
......
......@@ -20,6 +20,7 @@ class StickerPackInfoViewController: UITableViewController {
After adding this sticker pack to WhatsApp, you will be able to send these stickers to anyone \
in WhatsApp. To delete the sticker pack, go to the "My Stickers" panel in WhatsApp.
"""
private var footerHeight: CGFloat {
var insets: UIEdgeInsets = .zero
if #available(iOS 11.0, *) {
......@@ -38,18 +39,17 @@ class StickerPackInfoViewController: UITableViewController {
if stickerPack.publisherWebsite != nil {
websiteIndexes[.publisher] = index
index += 1
}
if stickerPack.privacyPolicyWebsite != nil {
websiteIndexes[.privacyPolicy] = index
index += 1
}
if stickerPack.licenseAgreementWebsite != nil {
websiteIndexes[.licenseAgreement] = index
index += 1
}
index += 1
}
override func viewDidLoad() {
......@@ -62,7 +62,7 @@ class StickerPackInfoViewController: UITableViewController {
}
@IBAction func donePressed(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
dismiss(animated: true)
}
var footerView: FooterView = FooterView(frame: CGRect.zero)
......@@ -79,7 +79,7 @@ class StickerPackInfoViewController: UITableViewController {
label.translatesAutoresizingMaskIntoConstraints = false;
label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.font = UIFont.systemFont(ofSize: 14)
label.font = .systemFont(ofSize: 14)
label.text = footerString
label.textColor = .gray
label.setContentHuggingPriority(.defaultHigh, for: .horizontal)
......@@ -196,7 +196,7 @@ class StickerPackInfoViewController: UITableViewController {
if let websiteURL = urlToOpen {
if UIApplication.shared.canOpenURL(websiteURL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(websiteURL, options: [:], completionHandler: nil)
UIApplication.shared.open(websiteURL)
} else {
UIApplication.shared.openURL(websiteURL)
}
......@@ -205,11 +205,7 @@ class StickerPackInfoViewController: UITableViewController {
}
override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
if indexPath.row <= 2 {
return false
}
return true
return !(indexPath.row <= 2)
}
}
......@@ -25,8 +25,8 @@ class StickerPackManager {
static func stickersJSON(contentsOfFile filename: String) throws -> [String: Any] {
if let path = Bundle.main.path(forResource: filename, ofType: "wasticker") {
let data: Data = try Data(contentsOf: URL(fileURLWithPath: path), options: Data.ReadingOptions.alwaysMapped)
return try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
let data: Data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
return try JSONSerialization.jsonObject(with: data) as! [String: Any]
}
throw StickerPackError.fileNotFound
......@@ -78,6 +78,7 @@ class StickerPackManager {
}
var stickerPack: StickerPack?
do {
stickerPack = try StickerPack(identifier: packIdentifier!, name: packName, publisher: packPublisher, trayImageFileName: packTrayImageFileName, publisherWebsite: packPublisherWebsite, privacyPolicyWebsite: packPrivacyPolicyWebsite, licenseAgreementWebsite: packLicenseAgreementWebsite)
} catch StickerPackError.fileNotFound {
......@@ -126,8 +127,8 @@ class StickerPackManager {
}
}
guard stickers.count >= Limits.MinStickersPerPack else {
fatalError("Sticker count smaller that the allowable limit (\(Limits.MinStickersPerPack) stickers per pack).")
if stickers.count < Limits.MinStickersPerPack {
fatalError("Sticker count smaller that the allowable limit (\(Limits.MinStickersPerPack) stickers per pack).")
}
stickerPacks.append(stickerPack!)
......
......@@ -61,9 +61,7 @@ final class StickerPackTableViewCell: UITableViewCell, UICollectionViewDataSourc
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let stickerPack = stickerPack else {
return 0
}
guard let stickerPack = stickerPack else { return 0 }
return stickerPack.stickers.count
}
......
......@@ -28,7 +28,8 @@ class StickerPackViewController: UIViewController, UICollectionViewDataSource, U
private var topDivider: UIView = UIView()
private var portraitOrientation: Bool {
return UIDevice.current.orientation == .portrait || UIDevice.current.orientation == .faceUp || UIDevice.current.orientation == .faceDown || UIDevice.current.orientation == .portraitUpsideDown
let currentOrientation = UIDevice.current.orientation
return currentOrientation == .portrait || currentOrientation == .faceUp || currentOrientation == .faceDown || currentOrientation == .portraitUpsideDown
}
var stickerPack: StickerPack!
......@@ -158,13 +159,8 @@ class StickerPackViewController: UIViewController, UICollectionViewDataSource, U
}
private func changeConstraints() {
for constraint in portraitConstraints {
constraint.isActive = portraitOrientation
}
for constraint in landscapeConstraints {
constraint.isActive = !portraitOrientation
}
portraitConstraints.forEach { $0.isActive = portraitOrientation }
landscapeConstraints.forEach { $0.isActive = !portraitOrientation }
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
......@@ -226,7 +222,8 @@ class StickerPackViewController: UIViewController, UICollectionViewDataSource, U
// MARK: Targets
func showActionSheet(withSticker sticker: Sticker, overCell cell: UICollectionViewCell) {
var emojisString: String? = nil
var emojisString: String?
#if DEBUG
if let emojis = sticker.emojis {
emojisString = emojis.joined(separator: " ")
......@@ -239,31 +236,28 @@ class StickerPackViewController: UIViewController, UICollectionViewDataSource, U
actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection()
actionSheet.popoverPresentationController?.sourceRect = CGRect(x: cell.contentView.bounds.midX, y: cell.contentView.bounds.midY, width: 0, height: 0)
actionSheet.addAction(UIAlertAction(title: "Copy to Clipboard", style: .default, handler: { action in
actionSheet.addAction(UIAlertAction(title: "Copy to Clipboard", style: .default, handler: { _ in
sticker.copyToPasteboardAsImage()
}))
actionSheet.addAction(UIAlertAction(title: "Share via", style: .default, handler: { action in
actionSheet.addAction(UIAlertAction(title: "Share via", style: .default, handler: { _ in
self.showShareSheet(withSticker: sticker)
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel))
if let stickerImage = sticker.imageData.image {
actionSheet.addImageView(withImage: stickerImage)
}
present(actionSheet, animated: true, completion: nil)
present(actionSheet, animated: true)
}
func showShareSheet(withSticker sticker: Sticker) {
guard let image = sticker.imageData.image else {
return
}
guard let image = sticker.imageData.image else { return }
let shareViewController: UIActivityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
shareViewController.popoverPresentationController?.sourceView = self.view
shareViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection()
shareViewController.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
present(shareViewController, animated: true, completion: nil)
present(shareViewController, animated: true)
}
@objc func infoPressed(button: UIButton) {
......@@ -280,10 +274,10 @@ class StickerPackViewController: UIViewController, UICollectionViewDataSource, U
@objc func addButtonPressed(button: AquaButton) {
let loadingAlert: UIAlertController = UIAlertController(title: "Sending to WhatsApp", message: "\n\n", preferredStyle: .alert)
loadingAlert.addSpinner()
present(loadingAlert, animated: true, completion: nil)
present(loadingAlert, animated: true)
stickerPack.sendToWhatsApp { completed in
loadingAlert.dismiss(animated: true, completion: nil)
loadingAlert.dismiss(animated: true)
}
}
......@@ -296,11 +290,11 @@ class StickerPackViewController: UIViewController, UICollectionViewDataSource, U
}
}
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: stickerImages, applicationActivities: nil);
let parentView = button as UIView
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: stickerImages, applicationActivities: nil)
let parentView = button as UIView
activityViewController.popoverPresentationController?.sourceView = parentView
activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection()
activityViewController.popoverPresentationController?.sourceRect = CGRect(x: parentView.bounds.midX, y: parentView.bounds.midY, width: 0, height: 0)
present(activityViewController, animated: true, completion: nil)
present(activityViewController, animated: true)
}
}
......@@ -29,7 +29,7 @@ extension UIAlertController {
}
let stickerImageView: UIImageView = UIImageView(image: image)
stickerImageView.translatesAutoresizingMaskIntoConstraints = false;
stickerImageView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stickerImageView)
stickerImageView.addConstraint(NSLayoutConstraint(item: stickerImageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: stickerImageViewLength))
......
......@@ -13,28 +13,21 @@ class WebPManager {
static let shared: WebPManager = WebPManager()