Skip to content

Instantly share code, notes, and snippets.

View darylsze's full-sized avatar

darylsze

  • Hong Kong
View GitHub Profile
@darylsze
darylsze / Demo.kt
Created January 11, 2023 15:59
Make lazy grid to be able to drag and drop, with section header that not moveable
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
@darylsze
darylsze / uninstall-netskope.sh
Created August 1, 2022 07:34 — forked from dongri/uninstall-netskope.sh
uninstall netskope
#!/bin/sh
sudo ps aux | grep Netskope | grep -v grep | awk '{ print "kill -9", $2 }' | sudo sh
echo '[✓] Kill Netskope Process'
sudo rm -rf /Applications/Remove\ Netskope\ Client.app
echo '[✓] Removed Remove Netskope Client.app'
sudo rm -rf /Library/Application\ Support/Netskope
echo '[✓] Removed Agent of Netskope Client.app'
@darylsze
darylsze / ContractCalculation.kt
Created November 22, 2021 01:40
Contract calculation by using visitor pattern
interface IReportVisitable {
fun accept(visitor: IReportVisitor): Int
}
data class FixedPriceContract(
val costPerYear: Int
) : IReportVisitable {
override fun accept(visitor: IReportVisitor): Int {
return visitor.visit(this)
}
@darylsze
darylsze / ContractCalculation.kt
Last active November 21, 2021 08:13
Contract calculator in Visitor pattern but simplied
interface IContracts
interface ICalculator {
fun calculate(vararg contracts: IContracts): Int
}
data class FixedPriceContract(
val costPerYear: Int
) : IContracts
@darylsze
darylsze / DialogBuilder.kt
Created October 1, 2021 04:27
DialogBuilder in Kotlin DSL (WIP)
class DialogBuilder(val activityContext: FragmentActivity) {
class TextBuilder(val context: Context) {
sealed class TextConfig {
data class TitleConfig(
val text: String,
val bold: Boolean = true,
@DimenRes val textSizeSdp: Int = R.dimen._16sdp
): TextConfig()
@darylsze
darylsze / CustomMessageEncoder.kt
Created August 5, 2021 08:54
Configurable message encoder for easier spannable mapping
import android.content.Context
import android.net.Uri
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import java.util.regex.Pattern
import java.util.regex.Pattern.LITERAL
/**
* variable definition (for easy understanding):
@darylsze
darylsze / MyToolbar.kt
Last active August 5, 2021 01:32
Converted legacy pain old Java code to Kotlin with builder pattern for rich functional Toolbar setup. Just demo some basic usage, removed a lots of legacy functions internally.
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
@darylsze
darylsze / MenuBuilder.kt
Last active August 5, 2021 01:24
Kotlin DSL to runtime build a configuration-easy menu by enum options. You can control a set of configuration for each menu item, including prerequisite condition, ordering, general callback or individual callback.
import com.package.MenuOption
class MenuBuilder {
val menuConfigs = mutableListOf<MenuOptionConfig>()
var commonCallback: ((MenuOption) -> Unit)? = null
fun addItem(menuOption: MenuOption, callback: ((MenuOption) -> Unit)? = null, eachBuildVisibilityPredicate: (() -> Boolean)? = null) {
menuConfigs.add(MenuOptionConfig(
option = menuOption,
callback = callback,
@darylsze
darylsze / RoundedBackgroundSpan.kt
Created August 5, 2021 01:12
RoundedBackgroundSpan
import android.content.Context
import android.graphics.*
import android.text.style.ReplacementSpan
import kotlin.math.roundToInt
class RoundedBackgroundSpan(
private val context: Context,
private val borderRadius: Float = 0f,
private val backgroundColorInt: Int? = null,
private val textColorInt: Int = Color.BLACK,
interface IResettable {
fun reset()
}
open class ResettableAspect: IResettable {
val delegateManager by lazy {
ResettableDelegateManager()
}
override fun reset() {
delegateManager.reset()