Skip to content

Commit 531dd6f

Browse files
committed
refactor: make updates and models
1 parent ee66c4c commit 531dd6f

7 files changed

Lines changed: 56 additions & 47 deletions

File tree

Binary file not shown.

Sources/EmojiPicker/Models/Category.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ struct Category: Decodable {
2424
/// Type-safe category type.
2525
let type: CategoryType
2626
/// Identifiers of emojis.
27-
let emojis: [String]
27+
let identifiers: [Emoji.ID]
2828

2929
enum CodingKeys: String, CodingKey {
3030
case type = "id"
31-
case emojis
31+
case identifiers = "emojis"
3232
}
3333
}
3434

Sources/EmojiPicker/Models/Emoji.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ struct Emoji: Decodable, Identifiable {
3131
let skins: [Skin]
3232
/// Version in which the emoji appeared.
3333
let version: Double
34+
/// Skin tone number. We save it so user can use the skin he chose.
35+
var skinToneIndex = 0
3436
}
3537

3638
extension Emoji {
37-
/// Main emoji.
39+
/// String emoji. For example: 😄
3840
///
39-
/// This property is used to get the main emoji to show it in the collection view.
41+
/// Shows in the collection view.
4042
var emoji: String {
41-
return skins[0].native
43+
return skins[skinToneIndex].native
4244
}
4345
}

Sources/EmojiPicker/ViewModel/EmojiPickerViewModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ final class EmojiPickerViewModel: EmojiPickerViewModelProtocol {
6565
}
6666

6767
func numberOfItems(in section: Int) -> Int {
68-
return emojiSet.categories[section].emojis.count
68+
return emojiSet.categories[section].identifiers.count
6969
}
7070

7171
func emoji(at indexPath: IndexPath) -> String {
72-
let name = emojiSet.categories[indexPath.section].emojis[indexPath.row]
73-
return emojiSet.emojis[name]?.emoji ?? ""
72+
let name = emojiSet.categories[indexPath.section].identifiers[indexPath.row]
73+
return emojiSet.emojis[name]?.emoji ?? ""
7474
}
7575

7676
func sectionHeaderViewModel(for section: Int) -> String {

Tests/EmojiPickerTests/CategoryTests.swift

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,29 @@ class CategoryTests: XCTestCase {
1717
}
1818

1919
func test_decodeCategory_success() throws {
20-
let result = try? JSONDecoder().decode(EmojiPicker.Category.self, from: category1)
20+
category = try? JSONDecoder().decode(EmojiPicker.Category.self, from: category1)
2121

22-
XCTAssertNotNil(result)
23-
XCTAssertEqual(result?.emojis, ["grinning", "smiley", "smile"])
22+
XCTAssertNotNil(category, "The result of decoding should be successful")
23+
XCTAssertEqual(category.type, CategoryType.people)
24+
XCTAssertEqual(category?.identifiers, ["grinning", "smiley", "smile"])
2425
}
2526

26-
func test_decodeCategory_arraySuccess() throws {
27-
let result = try? JSONDecoder().decode([EmojiPicker.Category].self, from: category3)
27+
func test_decodeCategory_wrongCodingKeys1() throws {
28+
category = try? JSONDecoder().decode(EmojiPicker.Category.self, from: category2)
2829

29-
XCTAssertNotNil(result)
30-
XCTAssertEqual(result?.count, 2)
30+
XCTAssertNil(category)
3131
}
3232

33-
func test_decodeCategory_wrongCodingKeys() throws {
34-
let result = try? JSONDecoder().decode(EmojiPicker.Category.self, from: category2)
33+
func test_decodeCategory_wrongCodingKeys2() throws {
34+
category = try? JSONDecoder().decode(EmojiPicker.Category.self, from: category3)
3535

36-
XCTAssertNil(result)
36+
XCTAssertNil(category)
37+
}
38+
39+
func test_decodeCategory_wrongCodingKeys3() throws {
40+
category = try? JSONDecoder().decode(EmojiPicker.Category.self, from: category4)
41+
42+
XCTAssertNil(category)
3743
}
3844
}
3945

@@ -51,7 +57,7 @@ fileprivate let category1 = Data("""
5157
fileprivate let category2 = Data("""
5258
{
5359
"type": "people",
54-
"emojis": [
60+
"identifiers": [
5561
"grinning",
5662
"smiley",
5763
"smile",
@@ -60,22 +66,23 @@ fileprivate let category2 = Data("""
6066
""".utf8)
6167

6268
fileprivate let category3 = Data("""
63-
[
64-
{
65-
"id": "people",
66-
"emojis": [
69+
{
70+
"id": "people",
71+
"identifiers": [
6772
"grinning",
73+
"smiley",
6874
"smile",
69-
"zzz"
70-
]
71-
},
72-
{
73-
"id": "nature",
74-
"emojis": [
75-
"monkey",
76-
"leaves",
77-
"dog"
78-
]
79-
}
80-
]
75+
]
76+
}
77+
""".utf8)
78+
79+
fileprivate let category4 = Data("""
80+
{
81+
"type": "people",
82+
"emojis": [
83+
"grinning",
84+
"smiley",
85+
"smile",
86+
]
87+
}
8188
""".utf8)

Tests/EmojiPickerTests/EmojiPickerViewModelTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class EmojiPickerViewModelTests: XCTestCase {
4141

4242
let result = viewModel.numberOfItems(in: 0)
4343

44-
XCTAssertEqual(result, emojiManagerStub.emojiSet.categories[section].emojis.count)
44+
XCTAssertEqual(result, emojiManagerStub.emojiSet.categories[section].identifiers.count)
4545
}
4646

4747
func testEmojiAtIndexPathMethod() throws {
@@ -50,7 +50,7 @@ class EmojiPickerViewModelTests: XCTestCase {
5050
let result = viewModel.emoji(at: indexPath)
5151

5252
let expectedResult = emojiManagerStub.emojiSet.emojis[
53-
emojiManagerStub.emojiSet.categories[indexPath.section].emojis[indexPath.row]
53+
emojiManagerStub.emojiSet.categories[indexPath.section].identifiers[indexPath.row]
5454
]?.emoji
5555
XCTAssertEqual(result, expectedResult)
5656
}

Tests/EmojiPickerTests/Stubs/EmojiManagerStub.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,18 @@
99

1010
class EmojiManagerStub: EmojiManagerProtocol {
1111

12-
var deviceVersion: Double {
13-
11.1
14-
}
15-
1612
func provideEmojis() -> EmojiSet {
1713
emojiSet
1814
}
1915

2016
var emojiSet = EmojiSet(
2117
categories: [
22-
Category(type: .people, emojis: [
18+
Category(type: .people, identifiers: [
2319
"smile",
2420
"laughing",
2521
"grin"
2622
]),
27-
Category(type: .foods, emojis: [
23+
Category(type: .foods, identifiers: [
2824
"peach"
2925
]),
3026
],
@@ -36,31 +32,35 @@ class EmojiManagerStub: EmojiManagerProtocol {
3632
skins: [
3733
Skin(unified: "1f604", native: "😄")
3834
],
39-
version: 1.0),
35+
version: 1.0
36+
),
4037
"laughing": Emoji(
4138
id: "laughing",
4239
name: "Grinning Squinting Face",
4340
keywords: [],
4441
skins: [
4542
Skin(unified: "1f606", native: "😆")
4643
],
47-
version: 1.0),
44+
version: 1.0
45+
),
4846
"grin": Emoji(
4947
id: "grin",
5048
name: "Beaming Face with Smiling Eyes",
5149
keywords: [],
5250
skins: [
5351
Skin(unified: "1f601", native: "😁")
5452
],
55-
version: 1.0),
53+
version: 1.0
54+
),
5655
"peach": Emoji(
5756
id: "peach",
5857
name: "Peach",
5958
keywords: [],
6059
skins: [
6160
Skin(unified: "1f351", native: "🍑")
6261
],
63-
version: 1.0),
62+
version: 1.0
63+
),
6464
],
6565
aliases: [:]
6666
)

0 commit comments

Comments
 (0)