This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Map it please. Returns a list containing the results of applying the | |
* given transform function to each element in the original collection. | |
* | |
* OP: https://twitter.com/jeranfox/status/1575133458631999488 | |
*/ | |
inline fun <TYPE, RESULT> Iterable<TYPE>.mip(transform: TYPE.() -> RESULT): List<RESULT> = map { it.transform() } | |
/** before: | |
* users.map { it.firstName } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T : Any> KClass<T>.safeOf(default: T, predicate: (T) -> Boolean): T = this.values().find { predicate(it) } ?: default | |
fun <T : Any> KClass<T>.values(): List<T> { | |
if (!this.isSealed) throw IllegalCallerException("Receiver MUST be a Sealed class") | |
return this.sealedSubclasses.map { sealedSubclass -> | |
when { | |
sealedSubclass.objectInstance != null -> sealedSubclass.objectInstance!! | |
else -> try { | |
sealedSubclass.primaryConstructor?.callBy(mapOf()) ?: throw Exception() | |
} catch (ignored: Exception) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Adds horizontal scrolling to tables in Obsidian Notes. Tested in Obsidian v0.15.9 | |
See also: https://forum.obsidian.md/t/css-horizontal-scrolling-tables/26581/4 | |
*/ | |
/* in preview / read mode */ | |
.markdown-preview-view table , | |
.markdown-source-view table { | |
display: block; /* This is needed for scrollbar */ | |
width: 100%; | |
max-width: -moz-max-content; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Changes the readable line length in Obsidian Notes. Tested in Obsidian v0.15.9 | |
See also: https://forum.obsidian.md/t/adjustable-readable-line-length/7564/6 | |
Note: For this the "readable line length" property in settings has to be enabled | |
(as expected). 700px width is the application's default, adjust all numbers below. | |
Pixel (or percentage) as a unit enables a width independent from the number of characters | |
(good when adjusting zoom level / font size). For fixed amount of characters use rem */ | |
:root { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ZipManager { | |
fun zip(files: List<String>, outputStream: OutputStream): Boolean = try { | |
ZipOutputStream(BufferedOutputStream(outputStream)).use { stream -> | |
for (file in files) { | |
BufferedInputStream(FileInputStream(File(file))).use { origin -> | |
stream.putNextEntry(ZipEntry(file.substring(file.lastIndexOf("/") + 1))) | |
origin.copyTo(stream) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) { | |
private val paint by lazy { Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL } } | |
@ColorInt | |
var color: Int = Color.TRANSPARENT | |
set(value) { | |
field = value | |
invalidate() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TriangleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) { | |
private val paint by lazy { Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL } } | |
private val path: Path = Path() | |
@ColorInt | |
var color: Int = Color.TRANSPARENT | |
set(value) { | |
field = value | |
invalidate() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object TimeAgo { | |
fun toRelative(millis: Long, spans: List<Span>): Map<Span, Long> { | |
var millisMutable = millis | |
return spans.sortedBy(Span::order).associateWith { span -> | |
val timeDelta: Long = millisMutable / span.millis | |
if (timeDelta.isGreaterThanZero()) millisMutable -= span.millis * timeDelta | |
timeDelta | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FixedColumnsHorizontalGridLayoutManager(context: Context?, spanCountVertical: Int, private var columns: Int, orientation: Int, reverseLayout: Boolean) : | |
GridLayoutManager(context, spanCountVertical, orientation, reverseLayout) { | |
private val safeWidth: Int | |
get() = width - paddingRight - paddingLeft | |
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams = spanLayoutSize(super.generateDefaultLayoutParams()) | |
override fun generateLayoutParams(context: Context, attrs: AttributeSet): RecyclerView.LayoutParams = spanLayoutSize(super.generateLayoutParams(context, attrs)) | |
override fun generateLayoutParams(layoutParams: ViewGroup.LayoutParams): RecyclerView.LayoutParams = spanLayoutSize(super.generateLayoutParams(layoutParams)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2022. Héctor de Isidro - hector6872 | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software |