Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- protocol EmojiFetching {
- func fetchEmojiWithSuccesss(_ success: ([String: String]) -> Void, _ failure: (Error) -> Void )
- }
- class Fack : EmojiFetching{
- func fetchEmojiWithSuccesss(_ success: ([String : String]) -> Void, _ failure: (Error) -> Void) {
- let dic = [
- "fire": "🔥",
- "firetruck": "🚒",
- "heart": "❤️",
- "love": "❤️",
- ]
- success(dic)
- }
- }
- class EmojiAutoCompletor {
- var dic : [String: String]?
- var fetchor : EmojiFetching
- init(fetchor : EmojiFetching) {
- self.fetchor = fetchor
- }
- func autocompleteWithPrefix(_ prefix : String, _ success: @escaping ([String]) -> Void, _ failure:@escaping (Error?) -> Void){
- if dic == nil {
- //get dic
- fetchor.fetchEmojiWithSuccesss({ (dict) in
- self.dic = dict
- self.findEmojiWithPrefix(prefix, success, failure)
- }) { (error) in
- failure(error)
- return
- }
- }
- findEmojiWithPrefix(prefix, success, failure)
- }
- private func findEmojiWithPrefix(_ prefix : String , _ success: ([String]) -> Void, _ failure: (Error?) -> Void){
- guard let dic = dic else {
- failure(nil)
- return
- }
- //find the prefix in all dictionary
- var res = [String]()
- for (key , emoji) in dic{
- if key.hasPrefix(prefix){
- res.append(emoji)
- }
- }
- success(res)
- }
- }
- //TEST TEST
- let fack = Fack()
- let emoji = EmojiAutoCompletor(fetchor: fack)
- emoji.autocompleteWithPrefix("firet", { (list) in
- print(list)
- }) { (error) in
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement