Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Closes #1568: Change truncated method to use Swift 4 (#1602)
Browse files Browse the repository at this point in the history
* Closes #1568: Changing to truncate using Swift 4 methods, removing unused methods in String Extension and tests

* Correct lint issues

* #1568: Refactoring code into UIConstants

* Resolved lint issues
  • Loading branch information
leschlogl authored and sblatz committed Dec 12, 2018
1 parent 21450b9 commit ed211c2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 100 deletions.
4 changes: 0 additions & 4 deletions Blockzilla.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
050E8B1E2064FECE00DF6090 /* StringExtensionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 050E8B1D2064FECE00DF6090 /* StringExtensionTest.swift */; };
0B0D6BC41F3CDDBB00497D08 /* CollapsedURLTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B0D6BC31F3CDDBB00497D08 /* CollapsedURLTest.swift */; };
0B37F9A61E2FCAD4002DF74B /* SearchProviderTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B37F9A51E2FCAD4002DF74B /* SearchProviderTest.swift */; };
0B70C1631DE6128900CEF7E0 /* WebsiteMemoryTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B70C1621DE6128900CEF7E0 /* WebsiteMemoryTest.swift */; };
Expand Down Expand Up @@ -255,7 +254,6 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
050E8B1D2064FECE00DF6090 /* StringExtensionTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringExtensionTest.swift; sourceTree = "<group>"; };
0B0D6BC31F3CDDBB00497D08 /* CollapsedURLTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollapsedURLTest.swift; sourceTree = "<group>"; };
0B37F9A51E2FCAD4002DF74B /* SearchProviderTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchProviderTest.swift; sourceTree = "<group>"; };
0B4868651F2250A3008C3C09 /* PastenGoTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PastenGoTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1211,7 +1209,6 @@
D8E015601FCF409F00CA3B9F /* Info.plist */,
D831FEE1205247A400EAE19A /* BrowserViewControllerTests.swift */,
D8E0156D1FD9E40F00CA3B9F /* DomainCompletionTests.swift */,
050E8B1D2064FECE00DF6090 /* StringExtensionTest.swift */,
D803D37320EF1A49001F2819 /* UserAgentTests.swift */,
);
path = ClientTests;
Expand Down Expand Up @@ -1873,7 +1870,6 @@
D803D37420EF1A49001F2819 /* UserAgentTests.swift in Sources */,
D831FEE2205247A400EAE19A /* BrowserViewControllerTests.swift in Sources */,
D8E0155F1FCF409F00CA3B9F /* SearchEngineManagerTests.swift in Sources */,
050E8B1E2064FECE00DF6090 /* StringExtensionTest.swift in Sources */,
D8E0156E1FD9E40F00CA3B9F /* DomainCompletionTests.swift in Sources */,
D025F225218B64D600B262D8 /* SearchEngineTests.swift in Sources */,
);
Expand Down
8 changes: 7 additions & 1 deletion Blockzilla/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,13 @@ class BrowserViewController: UIViewController {
}

fileprivate func presentImageActionSheet(title: String, link: String?, saveAction: @escaping () -> Void, copyAction: @escaping () -> Void) {
let alertController = UIAlertController(title: title.truncated(limit: 160, position: .middle), message: nil, preferredStyle: .actionSheet)

var normalizedTitle = title
if title.count > UIConstants.layout.truncateCharactersLimit {
normalizedTitle = String("\(title.prefix(UIConstants.layout.truncateHeadCharactersCount))\(UIConstants.strings.truncateLeader)\(title.suffix(UIConstants.layout.truncateTailCharactersCount))")
}

let alertController = UIAlertController(title: normalizedTitle, message: nil, preferredStyle: .actionSheet)

if let link = link {
alertController.addAction(UIAlertAction(title: UIConstants.strings.copyLink, style: .default) { _ in
Expand Down
39 changes: 0 additions & 39 deletions Blockzilla/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
import Foundation

extension String {
enum TruncationPosition {
case head
case middle
case tail
}

var isUrl: Bool {
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
Expand All @@ -19,38 +14,4 @@ extension String {

return true
}

func truncated(limit: Int, position: TruncationPosition = .tail, leader: String = "...") -> String {
guard count > limit else { return self }

switch position {
case .head:
let truncated = self[index(endIndex, offsetBy: leader.count - limit)...]
return leader + truncated
case .middle:
let headCharactersCount = (limit - leader.count) / 2
let head = self[..<index(startIndex, offsetBy: headCharactersCount)]

let tailCharactersCount = Int(ceil(Double(limit - leader.count) / 2.0))
let tail = self[index(endIndex, offsetBy: -tailCharactersCount)...]

return head + leader + tail
case .tail:
let truncated = self[..<index(startIndex, offsetBy: limit - leader.count)]
return truncated + leader
}
}

public func startsWith(other: String) -> Bool {
// rangeOfString returns nil if other is empty, destroying the analogy with (ordered) sets.
if other.isEmpty {
return true
}

if let range = self.range(of: other, options: .anchored) {
return range.lowerBound == self.startIndex
}

return false
}
}
4 changes: 4 additions & 0 deletions Blockzilla/UIConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ struct UIConstants {
static let shareTrackersHeight: CGFloat = 36
static let homeViewTextOffset: CGFloat = 5
static let homeViewLabelMinimumScale: CGFloat = 0.65
static let truncateCharactersLimit = 160
static let truncateHeadCharactersCount = (truncateCharactersLimit - UIConstants.strings.truncateLeader.count) / 2
static let truncateTailCharactersCount = Int(ceil(Double(truncateCharactersLimit - UIConstants.strings.truncateLeader.count) / 2.0))
}

struct strings {
Expand Down Expand Up @@ -451,5 +454,6 @@ struct UIConstants {
static let sumoTopicUsageData = "usage-data"
static let encodingNameUTF8 = "utf-8"
static let googleAmpURLPrefix = "https://www.google.com/amp/s/"
static let truncateLeader = "..."
}
}
56 changes: 0 additions & 56 deletions ClientTests/StringExtensionTest.swift

This file was deleted.

0 comments on commit ed211c2

Please sign in to comment.