Advertisement
HXXXXJ

emoji

Mar 18th, 2019
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.75 KB | None | 0 0
  1. import Foundation
  2.  
  3.  
  4. protocol EmojiFetching {
  5.     func fetchEmojiWithSuccesss(_ success: ([String: String]) -> Void, _ failure: (Error) -> Void )
  6. }
  7.  
  8. class Fack : EmojiFetching{
  9.     func fetchEmojiWithSuccesss(_ success: ([String : String]) -> Void, _ failure: (Error) -> Void) {
  10.         let dic = [
  11.             "fire": "🔥",
  12.             "firetruck": "🚒",
  13.             "heart": "❤️",
  14.             "love": "❤️",
  15.         ]
  16.         success(dic)
  17.     }
  18. }
  19.  
  20.  
  21.  
  22. class EmojiAutoCompletor {
  23.     var dic : [String: String]?
  24.     var fetchor : EmojiFetching
  25.     init(fetchor : EmojiFetching) {
  26.         self.fetchor = fetchor
  27.     }
  28.    
  29.     func autocompleteWithPrefix(_ prefix : String, _ success: @escaping ([String]) -> Void, _ failure:@escaping (Error?) -> Void){
  30.         if dic == nil {
  31.             //get dic
  32.             fetchor.fetchEmojiWithSuccesss({ (dict) in
  33.                 self.dic = dict
  34.                 self.findEmojiWithPrefix(prefix, success, failure)
  35.             }) { (error) in
  36.                 failure(error)
  37.                 return
  38.             }
  39.         }
  40.         findEmojiWithPrefix(prefix, success, failure)
  41.     }
  42.     private func findEmojiWithPrefix(_ prefix : String , _ success: ([String]) -> Void, _ failure: (Error?) -> Void){
  43.         guard let dic = dic else {
  44.             failure(nil)
  45.             return
  46.         }
  47.         //find the prefix in all dictionary
  48.         var res = [String]()
  49.         for (key , emoji) in dic{
  50.             if key.hasPrefix(prefix){
  51.                 res.append(emoji)
  52.             }
  53.         }
  54.         success(res)
  55.     }
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. //TEST TEST
  64.  
  65. let fack = Fack()
  66. let emoji = EmojiAutoCompletor(fetchor: fack)
  67.  
  68. emoji.autocompleteWithPrefix("firet", { (list) in
  69.     print(list)
  70. }) { (error) in
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement