1 // Copyright 2022-present 650 Industries. All rights reserved.
2 
3 import ExpoModulesCore
4 import SDWebImage
5 import SDWebImageWebPCoder
6 import SDWebImageAVIFCoder
7 import SDWebImageSVGCoder
8 
9 public final class ImageModule: Module {
10   lazy var prefetcher = SDWebImagePrefetcher.shared
11 
12   public func definition() -> ModuleDefinition {
13     Name("ExpoImage")
14 
15     OnCreate {
16       ImageModule.registerCoders()
17     }
18 
19     View(ImageView.self) {
20       Events(
21         "onLoadStart",
22         "onProgress",
23         "onError",
24         "onLoad"
25       )
26 
27       Prop("source") { (view, sourceSet: Either<ImageSource, [ImageSource]>) in
28         if let source: ImageSource = sourceSet.get() {
29           view.sources = [source]
30         }
31         if let sources: [ImageSource] = sourceSet.get() {
32           view.sources = sources
33         }
34       }
35 
36       Prop("contentFit") { (view, contentFit: ContentFit?) in
37         view.contentFit = contentFit ?? .cover
38       }
39 
40       Prop("contentPosition") { (view, contentPosition: ContentPosition?) in
41         view.contentPosition = contentPosition ?? .center
42       }
43 
44       Prop("transition") { (view, transition: ImageTransition?) in
45         view.transition = transition
46       }
47 
48       Prop("blurRadius") { (view, blurRadius: Double?) in
49         view.blurRadius = blurRadius ?? .zero
50       }
51 
52       Prop("tintColor") { (view, tintColor: UIColor?) in
53         view.imageTintColor = tintColor ?? .clear
54       }
55 
56       OnViewDidUpdateProps { view in
57         view.reload()
58       }
59     }
60 
61     Function("clearMemoryCache") {
62       SDImageCache.shared.clearMemory()
63     }
64 
65     Function("clearDiskCache") {
66       SDImageCache.shared.clearDisk()
67     }
68   }
69 
70   static func registerCoders() {
71     SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
72     SDImageCodersManager.shared.addCoder(SDImageAVIFCoder.shared)
73     SDImageCodersManager.shared.addCoder(SDImageSVGCoder.shared)
74     SDImageCodersManager.shared.addCoder(SDImageHEICCoder.shared)
75   }
76 }
77