Skip to content

Instantly share code, notes, and snippets.

View ricardopereira's full-sized avatar
🏠
Working from home

Ricardo Pereira ricardopereira

🏠
Working from home
View GitHub Profile
@ricardopereira
ricardopereira / SubscriptionTests.swift
Created September 13, 2021 17:17
Using the SKTestSession in XCTest.
import XCTest
import StoreKitTest
@available(iOS 14.0, *)
class SubscriptionTests: XCTestCase {
private var session: SKTestSession!
private var subscriptionsController: SubscriptionsController!
override func setUpWithError() throws {
@ricardopereira
ricardopereira / avd.sh
Created November 16, 2023 11:31 — forked from hidroh/avd.sh
Handy bash script to prompt for an Android virtual device (AVD) selection and launch it. Assuming that Android SDK has been set up and is in user PATH.
pushd ${ANDROID_HOME}/emulator
./emulator -list-avds | cat -n
printf "Select AVD: "
read index
avd=$(./emulator -list-avds | sed "${index}q;d")
echo "Selected $avd"
./emulator -netdelay none -netspeed full -avd $avd
popd
@ricardopereira
ricardopereira / GradientLayer + Mask + TableView.swift
Last active July 19, 2023 16:59
Apply vertical mask alpha gradient to UITableView
let gradient = CAGradientLayer()
gradient.frame = tableView.superview?.bounds ?? CGRectNull
gradient.colors = [UIColor.clearColor().CGColor, UIColor.clearColor().CGColor, UIColor.blackColor().CGColor, UIColor.blackColor().CGColor, UIColor.clearColor().CGColor, UIColor.clearColor().CGColor]
gradient.locations = [0.0, 0.15, 0.25, 0.75, 0.85, 1.0]
tableView.superview?.layer.mask = gradient
tableView.backgroundColor = UIColor.clearColor()
simulatorsIdentifiers=$(instruments -s devices |
grep -o "iPhone .* (.*) \[.*\]" | #only iPhone
grep -o "\[.*\]" | #only UUID
sed "s/^\[\(.*\)\]$/\1/" | #remove square brackets
sed 's/^/"/;$!s/$/"/;$s/$/"/' | #add quotes
sed '$!s/$/,/' #add comma to separate each element
)
arrayOfSimulatorsIdentifiers=($(echo "$simulatorsIdentifiers" | tr ',' '\n'))
@ricardopereira
ricardopereira / grid-trainer.swift
Created August 1, 2022 13:32 — forked from swiftui-lab/grid-trainer.swift
A grid trainer for Grid views
// Author: SwiftUI-Lab (swiftui-lab.com)
// Description: this learning tool is designed to showcase the different
// Grid and GridRow view options, added in SwiftUI 2022. It is part of the
// blog article: https://swiftui-lab.com/eager-grids
//
import SwiftUI
import UniformTypeIdentifiers
// The root view of the application
struct ContentView: View {
@ricardopereira
ricardopereira / DownloadProgressLiveData.kt
Created February 18, 2022 16:18 — forked from FhdAlotaibi/DownloadProgressLiveData.kt
Observe Download manager progress using LiveData and Coroutine
data class DownloadItem(val bytesDownloadedSoFar: Long = -1, val totalSizeBytes: Long = -1, val status: Int)
class DownloadProgressLiveData(private val application: Application, private val requestId: Long) : LiveData<DownloadItem>(), CoroutineScope {
private val downloadManager by lazy {
application.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
}
private val job = Job()
@ricardopereira
ricardopereira / LeakCheckTestCase.swift
Created August 10, 2021 08:28 — forked from aclima93/LeakCheckTestCase.swift
Automated detection of memory leaks in Unit Tests
class LeakCheckTestCase: XCTestCase {
private var excludedProperties = [String: Any]()
private var weakReferences = NSMapTable<NSString, AnyObject>.weakToWeakObjects()
// MARK: - SetUp
override func setUpWithError() throws {
try super.setUpWithError()
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// by calling [NSURLProtocol registerClass:[MyURLProtocol class]]; in -application:didFinishLoadingWithOptions:, your protocol will have priority over any of the built-in protocols.
URLProtocol.registerClass(ActivityURLProtocol.self)
//URLProtocol.unregisterClass(ActivityURLProtocol.self)
print(URLSession.shared.configuration.protocolClasses)
}
extension PHPhotoLibrary {
typealias PhotoAsset = PHAsset
typealias PhotoAlbum = PHAssetCollection
static func saveImage(image: UIImage, albumName: String, completion: (PHAsset?)->()) {
if let album = self.findAlbum(albumName) {
saveImage(image, album: album, completion: completion)
return
}
@ricardopereira
ricardopereira / BaseViewBindingFragment.java
Created November 12, 2020 17:47 — forked from killvetrov/BaseViewBindingFragment.java
Android View Binding: base class to reduce boilerplate in Java
public abstract class BaseViewBindingFragment extends Fragment {
private Field bindingField;
private Method inflate;
{
try {
for (Field declaredField : this.getClass().getDeclaredFields()) {
if (ViewBinding.class.isAssignableFrom(declaredField.getType())) {
bindingField = declaredField;