Bastian van de Wetering пре 6 година
родитељ
комит
3896a8e1aa

+ 6 - 4
deltachat-ios/Extensions/Extensions.swift

@@ -24,7 +24,7 @@ extension String {
 
 		var j = 0
 
-		var foundIndexes:[Int] = []
+		var foundIndexes: [Int] = []
 
 		for (index, char) in str.enumerated() {
 			if j == sub.count {
@@ -40,16 +40,18 @@ extension String {
 		return foundIndexes.count == sub.count ? foundIndexes : []
 	}
 
-
 	func subScript(_ i: Int) -> Character {
 		return self[index(startIndex, offsetBy: i)]
 	}
 
-	func bold(indexes: [Int], fontSize: CGFloat?) -> NSAttributedString {
+	func boldAt(indexes: [Int], fontSize: CGFloat) -> NSAttributedString {
 		let attributedText = NSMutableAttributedString.init(string: self)
 
 		for index in indexes {
-			attributedText.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: fontSize ?? 18), range: NSMakeRange(index, 1))
+			if index < 0 || self.count <= index {
+				break
+			}
+			attributedText.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: fontSize), range: NSMakeRange(index, 1))
 		}
 		return attributedText
 	}

+ 13 - 13
deltachat-ios/NewChatViewController.swift

@@ -32,12 +32,13 @@ class NewChatViewController: UITableViewController {
 		}
 	}
 
-	var contacts:[ContactWithHighlight] {
-		return contactIds.map({ return ContactWithHighlight(contact: MRContact(id: $0), indexesToHighlight: [])})
+	// contactWithSearchResults.indexesToHightLight empty by default
+	var contacts:[ContactWithSearchResults] {
+		return contactIds.map({ return ContactWithSearchResults(contact: MRContact(id: $0), indexesToHighlight: [])})
 	}
 
 	// used when seachbar is active
-	var filteredContacts: [ContactWithHighlight] = []
+	var filteredContacts: [ContactWithSearchResults] = []
 
 
 	// searchBar active?
@@ -211,7 +212,7 @@ class NewChatViewController: UITableViewController {
 				} else {
 					cell = ContactCell(style: .default, reuseIdentifier: "contactCell")
 				}
-				let contact: ContactWithHighlight = isFiltering() ? filteredContacts[row] : contacts[row]
+				let contact: ContactWithSearchResults = isFiltering() ? filteredContacts[row] : contacts[row]
 				updateContactCell(cell: cell, contactWithHighlight: contact)
 				return cell
 			} else {
@@ -233,7 +234,7 @@ class NewChatViewController: UITableViewController {
 				cell = ContactCell(style: .default, reuseIdentifier: "contactCell")
 			}
 
-			let contact: ContactWithHighlight = isFiltering() ? filteredContacts[row] : contacts[row]
+			let contact: ContactWithSearchResults = isFiltering() ? filteredContacts[row] : contacts[row]
 			updateContactCell(cell: cell, contactWithHighlight: contact)
 			return cell
 		}
@@ -300,19 +301,18 @@ class NewChatViewController: UITableViewController {
     }
   }
 
-	private func updateContactCell(cell: ContactCell, contactWithHighlight: ContactWithHighlight) {
+	private func updateContactCell(cell: ContactCell, contactWithHighlight: ContactWithSearchResults) {
 
 		let contact = contactWithHighlight.contact
 
-
 		if let nameHighlightedIndexes = contactWithHighlight.indexesToHighlight.filter({$0.contactDetail == .NAME}).first,
 			let emailHighlightedIndexes = contactWithHighlight.indexesToHighlight.filter({$0.contactDetail == .EMAIL}).first {
-			// gets here when contact is a result of search -> highlights relevant indexes
+			// gets here when contact is a result of current search -> highlights relevant indexes
 			let nameLabelFontSize = cell.nameLabel.font.pointSize
 			let emailLabelFontSize = cell.emailLabel.font.pointSize
 
-			cell.nameLabel.attributedText = contact.name.bold(indexes: nameHighlightedIndexes.indexes, fontSize: nameLabelFontSize)
-			cell.emailLabel.attributedText = contact.email.bold(indexes: emailHighlightedIndexes.indexes, fontSize: emailLabelFontSize)
+			cell.nameLabel.attributedText = contact.name.boldAt(indexes: nameHighlightedIndexes.indexes, fontSize: nameLabelFontSize)
+			cell.emailLabel.attributedText = contact.email.boldAt(indexes: emailHighlightedIndexes.indexes, fontSize: emailLabelFontSize)
 		} else {
 			cell.nameLabel.text = contact.name
 			cell.emailLabel.text = contact.email
@@ -328,9 +328,9 @@ class NewChatViewController: UITableViewController {
 
 	private func filterContentForSearchText(_ searchText: String, scope: String = "All") {
 
-		let contactsWithHighlights:[ContactWithHighlight] = contacts.map({contact in
+		let contactsWithHighlights:[ContactWithSearchResults] = contacts.map({contact in
 			let indexes = contact.contact.contains(searchText: searchText)
-			return ContactWithHighlight(contact: contact.contact, indexesToHighlight: indexes)
+			return ContactWithSearchResults(contact: contact.contact, indexesToHighlight: indexes)
 		})
 
 		filteredContacts = contactsWithHighlights.filter({!$0.indexesToHighlight.isEmpty})
@@ -416,7 +416,7 @@ enum ContactDetail {
 	case EMAIL
 }
 
-struct ContactWithHighlight {
+struct ContactWithSearchResults {
 	let contact: MRContact
 	let indexesToHighlight:[ContactHighlights]
 }

+ 8 - 8
deltachat-iosTests/deltachat_iosTests.swift

@@ -11,14 +11,14 @@ import XCTest
 
 class DeltachatTests: XCTestCase {
 
-	var appleseedContact: MRContact!
+	var testContact: MRContact?
 
 	override func setUp() {
 		let contactIds = Utils.getContactIds()
 
 		let contacts = contactIds.map({ return MRContact(id: $0) })
 
-		appleseedContact = contacts.filter({$0.name == "John Appleseed"}).first
+		testContact = contacts.filter({$0.name == "John Appleseed"}).first
 		// Put setup code here. This method is called before the invocation of each test method in the class.
 	}
 
@@ -27,6 +27,12 @@ class DeltachatTests: XCTestCase {
 	}
 
 	func testContactSearchForSubsequences() {
+
+		// this test will only success if run on a simulator (assuming one of the sample contacts is named John Appleseed)
+		guard let appleseedContact = testContact else {
+			return
+		}
+
 		XCTAssert(appleseedContact.name == "John Appleseed", "Test contacts name is John Appleseed")
 		XCTAssert(appleseedContact.email == "John-Appleseed@mac.com", "Test contacts email is john.appleseed@mac.com")
 
@@ -47,12 +53,6 @@ class DeltachatTests: XCTestCase {
 
 		let indexDetailD = appleseedContact.contains(searchText: "joz")
 		XCTAssert(indexDetailD.isEmpty)
-
-
-
-
-
-
 	}