Skip to content

Instantly share code, notes, and snippets.

View alokomkar's full-sized avatar
🔁
Loop

Alok Gudikote alokomkar

🔁
Loop
View GitHub Profile
@alokomkar
alokomkar / DefaultURL
Created January 9, 2014 06:07
Custom Web Browser creation using Java FX and Swing.
package FXBrowser;
public interface DefaultURL {
//The default URL.
public static final String DEFAULT_URL = "http://365programperday.blogspot.in/";
}
@alokomkar
alokomkar / gist:6302b91be1e3c1b1badcab2552e2b5bc
Created October 13, 2019 07:47
RxObservable based Broadcast receiver
fun AppCompatActivity.subscribeToBroadcastsOnLifecycle(action: String, fn: (Intent) -> Unit) {
observeBroadcasts(action).subscribeOnLifecycle(lifecycle, fn)
}
fun <T> Observable<T>.subscribeOnLifecycle(lifecycle: Lifecycle, fn: (T) -> Unit) {
val lifecycleObserver: LifecycleObserver = object : LifecycleObserver {
private var subscription: Disposable? = null
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
@alokomkar
alokomkar / Observer
Last active October 30, 2019 12:57
LifecycleOwner based Observer
fun <L : LiveData<T>, T : Any> LifecycleOwner.observe(liveData: L, body: (T?) -> Unit) =
liveData.observe(this, Observer(body))
//Activity view model
@MainThread
inline fun <reified VM : BaseViewModel> Fragment.activityViewModels(body: VM.() -> Unit): VM =
ViewModelProviders.of(this.requireActivity()).get<VM>().apply(body)
@alokomkar
alokomkar / HomeActivity.kt
Last active October 19, 2022 02:04
HomeActivity
import android.app.Activity
import android.os.Bundle
class HomeActivity: Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
}
override fun onStart() {
@alokomkar
alokomkar / basic_manifest.xml
Last active October 18, 2022 09:23
activity_added_to_manifest.xml
<manifest ... >
<application ... >
<activity android:name=".HomeActivity" />
...
</application ... >
...
</manifest >
@alokomkar
alokomkar / HomeFragment.kt
Created October 18, 2022 07:51
Fragment Lifecycle
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class HomeFragment: Fragment() {
override fun onAttach(context: Context) {
@alokomkar
alokomkar / activity_home.xml
Created October 19, 2022 02:05
activity_home
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.alokomkar.activity.HomeFragment" />
@alokomkar
alokomkar / coroutinesBasics.kt
Created October 21, 2022 01:45
Coroutine_basics
fun main() = runBlocking { // this: CoroutineScope
launch { // launch a new coroutine and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
}
println("Hello") // main coroutine continues while a previous one is delayed
}
@alokomkar
alokomkar / BasicSuspendingFunction.kt
Created October 21, 2022 02:14
Suspending functions
fun main() = runBlocking { // this: CoroutineScope
launch { doWorld() }
println("Hello")
}
// this is your first suspending function
suspend fun doWorld() {
delay(1000L)
println("World!")
}
@alokomkar
alokomkar / CoroutineScopeBuilder.kt
Created October 21, 2022 02:28
Coroutine scope builder
// Sequentially executes doWorld followed by "Done"
fun main() = runBlocking {
doWorld()
println("Done")
}
// Concurrently executes both sections
suspend fun doWorld() = coroutineScope { // this: CoroutineScope
launch { // Coroutine 1