Skip to content

Instantly share code, notes, and snippets.

@chiragthummar
chiragthummar / BouncyClickable.kt
Created June 25, 2023 07:25
Increase user experience with bouncy click on any component of jetpack compose.
@OptIn(ExperimentalFoundationApi::class)
fun Modifier.bounceClickable(
dampingRatio: Float = 0.85f,
enabled: Boolean = true,
onClick: () -> Unit = {},
onDoubleClick: (() -> Unit)? = null,
onLongClick: (() -> Unit)? = null,
shape: Shape = RectangleShape,
useHapticFeedback: Boolean = true,
) = composed {
@chriseidhof
chriseidhof / ViewToPDF.swift
Created January 6, 2023 15:46
Image Rendering
import SwiftUI
extension View {
@MainActor
func pdf(size: ProposedViewSize) -> Data {
let renderer = ImageRenderer(content: self)
renderer.proposedSize = size
var pdfData = NSMutableData()
renderer.render { size, render in
var mediaBox = CGRect(origin: .zero, size: size)
import UIKit
import AVFoundation
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let tableView = UITableView(frame: .zero, style: .plain)
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(PlayerTableViewCell.self, forCellReuseIdentifier: "PlayerCell")
@c5inco
c5inco / HeartRate.kt
Last active September 1, 2022 09:40
Jetpack Compose implementation of inspiration: https://twitter.com/amos_gyamfi/status/1494053914945236999
package des.c5inco.material3
import android.graphics.Matrix
import android.graphics.Path
import androidx.compose.animation.core.*
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
@ianhanniballake
ianhanniballake / PickImageContracts.kt
Last active September 10, 2022 18:03
Gist showing how to write backward compatible ActivityResultContracts for supporting Android 13's new Photo Picker: https://developer.android.com/about/versions/13/features/photopicker
/**
* Use this [ActivityResultContract] to seamlessly switch between
* the new [MediaStore.ACTION_PICK_IMAGES] and [Intent.ACTION_GET_CONTENT]
* based on the availability of the Photo Picker.
*
* Use [PickMultipleImages] if you'd like the user to be able to select multiple
* photos/videos.
*
* Input: the mimeType you'd like to receive. This should generally be
* either `image/\*` or `video/\*` for requesting only images or only videos
@saket
saket / Present.swift
Last active July 30, 2023 16:25
SwiftUI with Reaktive presenters written with Kotlin Multiplatform
import SwiftUI
import Combine
import Common // Code shared through Kotlin Multiplaform.
import CombineExt // https://github.com/CombineCommunity/CombineExt
/// A convenience pass-through View to hide away the verbosity
/// of subscribing to a presenter's stream. Usage:
///
/// struct FooView: View {
/// let presenter: FooPresenter
fun Modifier.shape(width: Dp, strokeBrush: Brush, fillColor: Color, shape: Shape): Modifier = composed(
factory = {
this.then(
when {
shape === RectangleShape -> rectangleModifier(width, strokeBrush, fillColor)
else -> shapeModifier(width, strokeBrush, fillColor, shape)
}
)
},
inspectorInfo = debugInspectorInfo {
@shekibobo
shekibobo / ParcelTesting.kt
Created March 8, 2021 22:36
Testing a Parcelable class implemented with @parcelize
inline fun <reified T> T.forceParcel(): T? where T : Parcelable {
val bytes = Parcel.obtain().use {
writeParcelable(this@forceParcel, 0)
marshall()
}
return Parcel.obtain().use {
unmarshall(bytes, 0, bytes.size)
setDataPosition(0)
readParcelable(T::class.java.classLoader)
}
@epool
epool / BuildXcFramework.kt
Last active December 5, 2023 13:11
Builds a XcFramework from a shared module in KMM.
//region XcFramework tasks
val xcFrameworkPath = "xcframework/${project.name}.xcframework"
tasks.create<Delete>("deleteXcFramework") { delete = setOf(xcFrameworkPath) }
val buildXcFramework by tasks.registering {
dependsOn("deleteXcFramework")
group = "build"
val mode = "Release"
val frameworks = arrayOf("iosArm64", "iosX64")
import SwiftUI
// Note: There are some issues with using these modifiers inside of ButtonStyles on macOS. Please see https://twitter.com/noahsark769/status/1288256379640139776?s=20 for more info.
struct ConditionalContent<TrueContent: View, FalseContent: View>: View {
let value: Bool
let trueContent: () -> TrueContent
let falseContent: () -> FalseContent
@ViewBuilder var body: some View {