-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #573 from mltokky/feature/430_cache_speaker_icon_i…
…mage [iOS] Create a cache system for images
- Loading branch information
Showing
5 changed files
with
73 additions
and
33 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
app-ios/Sources/CommonComponents/Timetable/CircularUserIcon.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import Foundation | ||
import SwiftUI | ||
|
||
private actor CircularUserIconInMemoryCache { | ||
static let shared = CircularUserIconInMemoryCache() | ||
private init() {} | ||
|
||
private var cache: [String: Data] = [:] | ||
|
||
func data(urlString: String) -> Data? { | ||
return cache[urlString] | ||
} | ||
|
||
func set(data: Data, urlString: String) { | ||
cache[urlString] = data | ||
} | ||
} | ||
|
||
public struct CircularUserIcon: View { | ||
let urlString: String | ||
@State private var iconData: Data? | ||
|
||
public init(urlString: String) { | ||
self.urlString = urlString | ||
} | ||
|
||
public var body: some View { | ||
Group { | ||
if let data = iconData, | ||
let uiImage = UIImage(data: data) { | ||
Image(uiImage: uiImage) | ||
.resizable() | ||
} else { | ||
Circle().stroke(Color.gray) | ||
} | ||
} | ||
.clipShape(Circle()) | ||
.task { | ||
if let data = await CircularUserIconInMemoryCache.shared.data(urlString: urlString) { | ||
iconData = data | ||
return | ||
} | ||
|
||
guard let url = URL(string: urlString) else { | ||
return | ||
} | ||
let urlRequest = URLRequest(url: url, cachePolicy: .useProtocolCachePolicy) | ||
if let (data, _) = try? await URLSession.shared.data(for: urlRequest) { | ||
iconData = data | ||
await CircularUserIconInMemoryCache.shared.set(data: data, urlString: urlString) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
CircularUserIcon(urlString: "https://avatars.githubusercontent.com/u/10727543?s=96&v=4") | ||
.frame(width: 32, height: 32) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters