Skip to content

Instantly share code, notes, and snippets.

View GenerateBitmap.kt
fun generateImage(width: Int, height: Int): Bitmap {
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val paint = Paint()
val path = generatePath()
canvas.drawPath(path, paint)
return bitmap
}
View SharedViewModelExample.kt
class CounterViewModelFactory(val name: String) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return CounterViewModel(name) as T
}
}
//inside activity
val viewModel: CounterViewModel by lazy {
ViewModelProvider(
MainActivity@ this,
View SomeFragment.kt
class SomeFragment: Fragment(){
@Inject factory: AssistedViewModel.Factory
override fun onActivityCreated(savedInstanceState: Bundle?){
super.onActivityCreated(savedInstanceState)
val viewModel = factory.create(301)
...
}
View AssistedViewModel.kt
class AssistedViewModel @AssistedInject constructor(
private val dependency1: Dependency1,
private val dependency2: Dependency2,
...
@Assisted private val id: Int){
@AssistedInject.Factory
interface Factory{ fun create(id: Int) : AssistedViewModel }
}
View MyViewModel.kt
class MyViewModel @Inject constructor(private val dependency1: Dependency1,
private val dependency2: Dependency2,
...)
{
private var id: Int? = null
fun setupId(id: Int){
this.id = id
}
...
View MyViewModel.kt
class MyViewModel @Inject constructor(private val dependency1: Dependency1,
private val dependency2: Dependency2,
...
private val id: Int)//hold on Dagger! I will provide this on my own
{ ... }
View .gitignore
# Built application files
*.apk
*.ap_
*.aab
# Files for the ART/Dalvik VM
*.dex
# Java class files
View ExampleTest.kt
ExampleTest{
@BeforeEach
fun setUp() {
viewModel = SomeViewModel(...)
filtersObserver = viewModel.filtersLiveData.observeEvents()
}
@Test fun `test 1`(){
val expected = arrayListOf<Any>()
View AlertDialog.kt
fun Fragment.createAlertDialog() =
AlertDialog.Builder(ContextThemeWrapper(context, R.style.custom_dialog_style))
data class DialogAction(
var text: String,
var onClick: (() -> Unit)? = null
)
View print_out_artifact_urls.sh
#!/usr/bin/env bash
#access token can be generated here(https://app.bitrise.io/me/profile#/security) and stored as secret
access_token=$BITRISE_API_ACCESS_TOKEN
app_slug="your-app-id" # the id of your app
build_slug=$BITRISE_BUILD_SLUG
bitrise_api_url="https://api.bitrise.io/v0.1"
the_url="$bitrise_api_url/apps/$app_slug/builds/$build_slug/artifacts"
artifacts_array=$(curl -s -H "Authorization: $access_token" $the_url)