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
// 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)
}
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)
tableView.loadData(refresh: true)
// Add paginated delegates only
tableView.paginatedDelegate = self
tableView.paginatedDataSource = self
// Assign custom class to table view in storyboard
@IBOutlet weak var tableView: PaginatedTableView!
// 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)))
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)
// 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()
val requestOptions = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.signature(ObjectKey(signature))
Glide.with(context).load(url).apply(requestOptions).into(imageView)
val requestOptions = RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL)
Glide.with(context).load(url).apply(requestOptions).into(imageView)