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
class MemoryCacheManager<Key: Hashable, Value> { | |
private var cache: [Key: (value: Value, expiry: Date)] | |
private let defaultExpiryTime: TimeInterval | |
init(defaultExpiryTime: TimeInterval = 3600 * 6) { // Default expiry time is 6 hours | |
self.cache = [:] | |
self.defaultExpiryTime = defaultExpiryTime | |
} | |
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
struct PressActions: ViewModifier { | |
var onPress: () -> Void | |
var onRelease: () -> Void | |
func body(content: Content) -> some View { | |
content | |
.simultaneousGesture( | |
DragGesture(minimumDistance: 0) | |
.onChanged({ _ in | |
onPress() |
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
webView.scrollView.isScrollEnabled = false | |
webView.navigationDelegate = self | |
webView.loadHTMLString(htmlString, baseURL: nil) | |
scrollView.addSubview(webView) | |
... | |
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | |
if webView.isLoading == false { |
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
import Foundation | |
func damerauLevenshteinDistance(s1: String, s2: String) -> Int { | |
let inf = s1.count + s2.count | |
var dp = [[Int]](repeating: [Int](repeating: inf, count: s2.count + 1), count: s1.count + 1) | |
for i in 0...s1.count { | |
dp[i][0] = i | |
} | |
for j in 0...s2.count { | |
dp[0][j] = j |
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
data class Node(val name: String) { | |
val neighbors = mutableListOf<Edge>() | |
} | |
data class Edge(val neighbor: Node) | |
class Graph { | |
private val nodes = mutableListOf<Node>() | |
fun addNode(name: String) { |
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
class LatestNewsViewModel( | |
private val newsRepository: NewsRepository | |
) : ViewModel() { | |
private val _data = MutableLiveData<List<ArticleHeadline>>() | |
val data: LiveData<List<GetNewsDetails>> = _data | |
fun getLatestNews() { | |
viewModelScope.launch { | |
val list = mutableListOf<ArticleHeadline>() |
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
class NewsRepository( | |
private val newsRemoteDataSource: NewsRemoteDataSource, | |
private val userData: UserData | |
) { | |
/** | |
* Returns the favorite latest news applying transformations on the flow. | |
* These operations are lazy and don't trigger the flow. They just transform | |
* the current value emitted by the flow at that point in time. | |
*/ | |
val favoriteLatestNews: Flow<List<ArticleHeadline>> = |
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
class NewsRemoteDataSource( | |
private val newsApi: NewsApi, | |
private val refreshIntervalMs: Long = 5000 | |
) { | |
val latestNews: Flow<List<ArticleHeadline>> = flow { | |
while(true) { | |
val latestNews = newsApi.fetchLatestNews() | |
emit(latestNews) // Emits the result of the request to the flow | |
delay(refreshIntervalMs) // Suspends the coroutine for some time | |
} |
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
class PodcastViewModel : ViewModel() { | |
private val _podcasts = MutableStateFlow(listOf<Podcast>()) | |
val podcasts: StateFlow<List<Podcast>> get() = _podcasts | |
private val downloadQueue: MutableMap<Int, Flow<Int>> = mutableMapOf() | |
init { | |
getPodcasts() | |
} |
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
@Composable | |
fun EmbeddedAndroidViewDemo() { | |
Column { | |
// Holds state | |
val state = remember { mutableStateOf(0) } | |
// widget.ImageView | |
AndroidView(factory = { ctx -> |
NewerOlder