Skip to content

Instantly share code, notes, and snippets.

View salmaanahmed's full-sized avatar
💭
Software Engineer with tons of experience in Android | iOS | Flutter | Xamarin

Salmaan Ahmed salmaanahmed

💭
Software Engineer with tons of experience in Android | iOS | Flutter | Xamarin
View GitHub Profile
@salmaanahmed
salmaanahmed / repository.swift
Last active March 22, 2021 19:54
Repository pattern with different strategies
View repository.swift
// The object we will be fetching from local or remote data source
struct Note {
var description: String
}
// Strategy
protocol NoteStrategy {
func getNote(completion: @escaping (Note) -> Void)
}
View LoadMore.swift
func loadMore(_ pageNumber: Int, _ pageSize: Int, onSuccess: ((Bool) -> Void)?, onError: ((Error) -> Void)?) {
// Call your api here
// Send true in onSuccess in case new data exists, sending false will disable pagination
// If page number is first, reset the list
if pageNumber == 1 { self.list = [Model]() }
// else append the data to list
self.list.append(apiResponseList)
View PaginatedDelegate.swift
// Add paginated delegates only
tableView.paginatedDelegate = self
tableView.paginatedDataSource = self
View PaginatedTableView.swift
// Assign custom class to table view in storyboard
@IBOutlet weak var tableView: PaginatedTableView!
View GlideThumbnail.kt
// With thumbnail url
Glide.with(context).load(url)
.thumbnail(Glide.with(context).load(thumbUrl))
.apply(requestOptions).into(imageView)
// Without thumbnail url
// If you know thumbnail size
Glide.with(context).load(url)
.thumbnail(Glide.with(context).load(url).apply(RequestOptions().override(thumbSize)))
View GlideResize.kt
val requestOptions = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.signature(ObjectKey(signature))
.override(100, 100) // resize does not respect aspect ratio
Glide.with(context).load(url).apply(requestOptions).into(imageView)
View ClearGlideCache.kt
// This method must be called on the main thread.
Glide.get(context).clearMemory()
Thread(Runnable {
// This method must be called on a background thread.
Glide.get(context).clearDiskCache()
}).start()
View GlideSignature.kt
val requestOptions = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.signature(ObjectKey(signature))
Glide.with(context).load(url).apply(requestOptions).into(imageView)
View GlideDiskCache.kt
val requestOptions = RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL)
Glide.with(context).load(url).apply(requestOptions).into(imageView)