Skip to content

Commit

Permalink
Merge pull request #13 from izyumkin/feature/frequentlyUsedSection
Browse files Browse the repository at this point in the history
Welcome frequently used section
  • Loading branch information
izyumkin authored May 23, 2023
2 parents 8d1146b + 3ba7bfc commit 66a9b01
Show file tree
Hide file tree
Showing 10 changed files with 11,464 additions and 11,296 deletions.
4 changes: 2 additions & 2 deletions Example App/MCEmojiPicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = KP4A68PJ98;
DEVELOPMENT_TEAM = "";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "iOS Example App/Resources/Info.plist";
INFOPLIST_KEY_CFBundleDisplayName = MCEmojiPicker;
Expand Down Expand Up @@ -487,7 +487,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = KP4A68PJ98;
DEVELOPMENT_TEAM = "";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = "iOS Example App/Resources/Info.plist";
INFOPLIST_KEY_CFBundleDisplayName = MCEmojiPicker;
Expand Down
6 changes: 3 additions & 3 deletions Sources/MCEmojiPicker/Bindings/Observable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ final class Observable<T> {
/// The `didSet` block ensures that the `Listener` closure is called whenever the value changes.
public var value: T {
didSet {
listener?(value)
listeners.forEach { $0(value) }
}
}

// MARK: - Private Properties

/// Holds a closure that will be called whenever the value changes.
private var listener: Listener?
private var listeners = [Listener]()

// MARK: - Initializers

Expand All @@ -53,6 +53,6 @@ final class Observable<T> {

/// Allows you to set the `Listener` closure.
public func bind(_ listener: @escaping Listener) {
self.listener = listener
self.listeners.append(listener)
}
}
44 changes: 39 additions & 5 deletions Sources/MCEmojiPicker/Model/MCEmoji.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,45 @@ import Foundation
/// The main model for interacting with emojis.
struct MCEmoji {

// MARK: - Types

/// Keys for storage in UserDefaults.
private enum StorageKeys {
case skinTone(_ emoji: MCEmoji)
case usageTimestamps(_ emoji: MCEmoji)

var key: String {
switch self {
case .skinTone(let emoji):
return emoji.emojiKeys.emoji()
case .usageTimestamps(let emoji):
return StorageKeys.skinTone(emoji).key + "-usage-timestamps"
}
}
}

// MARK: - Public Properties

/// A boolean indicating whether the skin for this emoji has been selected before.
public var isSkinBeenSelectedBefore: Bool {
return UserDefaults.standard.integer(forKey: emojiKeys.emoji()) != 0
skinTone != nil
}
/// The current skin tone for this emoji, if one has been selected.
public var skinTone: MCEmojiSkinTone? {
return MCEmojiSkinTone(rawValue: UserDefaults.standard.integer(
forKey: emojiKeys.emoji()
))
let skinToneRawValue = UserDefaults.standard.integer(forKey: StorageKeys.skinTone(self).key)
return MCEmojiSkinTone(rawValue: skinToneRawValue)
}
/// All times when the emoji has been selected.
public var usage: [TimeInterval] {
(UserDefaults.standard.array(forKey: StorageKeys.usageTimestamps(self).key) as? [TimeInterval]) ?? []
}
/// The number of times this emoji has been selected.
public var usageCount: Int {
usage.count
}
/// The last time when this emoji has been selected.
public var lastUsage: TimeInterval {
usage.first ?? .zero
}

/// The string representation of the emoji.
Expand Down Expand Up @@ -79,10 +107,16 @@ struct MCEmoji {
/// - Parameters:
/// - skinToneRawValue: The raw value of the `MCEmojiSkinTone`.
public mutating func set(skinToneRawValue: Int) {
UserDefaults.standard.set(skinToneRawValue, forKey: emojiKeys.emoji())
UserDefaults.standard.set(skinToneRawValue, forKey: StorageKeys.skinTone(self).key)
string = getEmoji()
}

/// Increments the usage count for this emoji.
public func incrementUsageCount() {
let nowTimestamp = Date().timeIntervalSince1970
UserDefaults.standard.set([nowTimestamp] + usage, forKey: StorageKeys.usageTimestamps(self).key)
}

// MARK: - Private Methods

/// Returns the string representation of this smiley. Considering the skin tone, if it has been selected.
Expand Down
4 changes: 4 additions & 0 deletions Sources/MCEmojiPicker/Model/MCEmojiCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import Foundation

/// The main model that is used to configure the main collection.
struct MCEmojiCategory {
var type: MCEmojiCategoryType
var categoryName: String
var emojis: [MCEmoji]
}

/// This enumeration shows a list of categories that are contained in the main collection.
enum MCEmojiCategoryType: Int, CaseIterable {
case frequentlyUsed
case people
case nature
case foodAndDrink
Expand All @@ -42,6 +44,8 @@ enum MCEmojiCategoryType: Int, CaseIterable {
/// A constant key for accessing name localization resources for each category.
var localizeKey: String {
switch self {
case .frequentlyUsed:
return "frequentlyUsed"
case .people:
return "emotionsAndPeople"
case .nature:
Expand Down
Loading

0 comments on commit 66a9b01

Please sign in to comment.