Skip to content

Instantly share code, notes, and snippets.

@baggednismo
baggednismo / PostalConversions.kt
Last active January 25, 2023 14:33
This class defines all the United States, Canada and Mexico states/provinces and allows for conversion from the full name to abbreviation.
import java.text.Normalizer
enum class State(val stateName: String) {
AK("Alaska"),
AL("Alabama"),
AR("Arkansas"),
AZ("Arizona"),
CA("California"),
CO("Colorado"),
CT("Connecticut"),
@baggednismo
baggednismo / ViewLifecycleAware.kt
Created November 30, 2020 20:59 — forked from rishabhkohli/!!ViewLifecycleAware.kt
A Kotlin delegated property implementation which automatically clears itself at appropriate times in the View Lifecycle of a Fragment.
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
fun <T> Fragment.viewLifecycleAware(initialise: () -> T): ReadOnlyProperty<Fragment, T> =
object : ReadOnlyProperty<Fragment, T>, DefaultLifecycleObserver {
private var binding: T? = null
override fun onCreate(savedInstanceState: Bundle?) {
...
btnContinue.setOnClickListener {
if (pager.currentItem < pager.adapter?.itemCount!! - 1) {
handler.removeMessages(0)
pager.currentItem = ++pager.currentItem
}
}
// I expect the back button to be hidden when ViewPager2 is at position 0
...
pager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
// Clear the messages so that every time an item has changed
// we reset our handler.
handler.removeMessages(0)
if (position == 0) btnBack.visibility = View.GONE
companion object {
private const val timerDelay: Long = 10 * 1000 // 10 seconds in milliseconds
}
override fun onCreate(savedInstanceState: Bundle?) {
...
pager.adapter = ViewPagerAdapter(updates)
...
pager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
class LauncherActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val handler = Handler()
setContentView(R.layout.activity_launcher)
val updates = listOf(
UpdatesSeriesModel(
public class OkHttpFactory {
private static final int DEFAULT_CONNECTION_TIMEOUT = 30; // seconds
private OkHttpClient mOkHttpClient;
public void OkHttpFactory() {
mOkHttpClient = new OkHttpClient();
}
/**
* To get default OkHttpClient with out any interceptor or log level. To add OkHttpClient with its properties use {@link Builder} class
class ChildItemsViewHolder(inflater: LayoutInflater, parent: ViewGroup, itemListener: ItemListener) :
RecyclerView.ViewHolder(inflater.inflate(R.layout.card_child_item, parent, false)), View.OnClickListener {
private var itemListener: ItemListener? = null
private var cardTitle: TextView? = null
private var cardDesc: TextView? = null
private var remove: ConstraintLayout? = null
init {
cardTitle = itemView.findViewById(R.id.card_title)
class ChildItemsAdapter(private val data: ArrayList<ChildItemModel>,
private var itemListener: ItemListener
) : RecyclerView.Adapter<ChildItemsViewHolder>(), ItemListener {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChildItemsViewHolder {
val inflater = LayoutInflater.from(parent.context)
return ChildItemsViewHolder(inflater, parent, this)
}
override fun onBindViewHolder(holder: ChildItemsViewHolder, position: Int) {
class ParentItemsViewHolder(inflater: LayoutInflater, parent: ViewGroup, itemListener: ItemListener) :
RecyclerView.ViewHolder(inflater.inflate(R.layout.card_parent_item, parent, false)) {
private var itemListener: ItemListener? = null
private var cardTitle: TextView? = null
private var recyclerItem: RecyclerView? = null
init {
cardTitle = itemView.findViewById(R.id.app_title)
recyclerItem = itemView.findViewById(R.id.id_card_child_items)