Skip to content

Instantly share code, notes, and snippets.

View GregKluska's full-sized avatar
😀

Greg Kluska GregKluska

😀
  • Manchester
View GitHub Profile
@GregKluska
GregKluska / GenericApiResponse.kt
Last active July 9, 2020 16:46
GenericApiResponse and LiveDataCallAdapter
import android.util.Log
import retrofit2.Response
/**
* Copied from Architecture components google sample:
* https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/api/ApiResponse.kt
*/
@Suppress("unused") // T is used in extending classes
sealed class GenericApiResponse<T> {
@GregKluska
GregKluska / AbsentLiveData.kt
Last active July 11, 2020 21:52
A LiveData class that has `null` value.
import androidx.lifecycle.LiveData
/**
* A LiveData class that has `null` value.
*/
class AbsentLiveData<T : Any?> private constructor(): LiveData<T>() {
init {
// use post instead of set since this can be created on any thread
postValue(null)
@GregKluska
GregKluska / ExampleFragment.kt
Created June 14, 2020 13:48
Check if the content implements an interface
import android.content.Context
import androidx.fragment.app.Fragment
import com.codingwithmitch.mviexample.ui.DataStateListener
import java.lang.ClassCastException
class ExampleFragment : Fragment() {
lateinit var dataStateHandler: DataStateListener
@GregKluska
GregKluska / Event.kt
Created June 14, 2020 14:47
Used as a wrapper for data that is exposed via a LiveData that represents an event.
/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
class Event<T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
@GregKluska
GregKluska / debugger pause beforeunload
Created June 16, 2020 13:55 — forked from carcinocron/debugger pause beforeunload
Chrome: pause before redirect
// Run this in the F12 javascript console in chrome
// if a redirect happens, the page will pause
// this helps because chrome's network tab's
// "preserve log" seems to technically preserve the log
// but you can't actually LOOK at it...
// also the "replay xhr" feature does not work after reload
// even if you "preserve log".
window.addEventListener("beforeunload", function() { debugger; }, false)
import androidx.lifecycle.LiveData
import retrofit2.Call
import retrofit2.CallAdapter
import retrofit2.Callback
import retrofit2.Response
import java.lang.reflect.Type
import java.util.concurrent.atomic.AtomicBoolean
class LiveDataCallAdapter<R>(private val responseType: Type) :
CallAdapter<R, LiveData<GenericApiResponse<R>>> {
@GregKluska
GregKluska / !MVI Architecture Generics
Last active July 11, 2020 22:04
Gist of generic classes from codingwithmitch.com
Gist of generic classes from codingwithmitch.com
List:
- StateResource.kt
- DataState.kt
- BaseViewModel.kt
fun isConnectedToTheInternet(): Boolean {
val cm = application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
try {
return cm.activeNetworkInfo.isConnected
} catch (e: Exception) {
Log.e(TAG, "isConnectedToTheInternet: ${e.message}", )
}
return false
}
import android.app.Activity
import android.content.Context
import androidx.annotation.IdRes
import androidx.annotation.NavigationRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
@GregKluska
GregKluska / ListAdapter.kt
Created October 13, 2020 10:51
ListAdapter.kt
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}#end
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
#parse("File Header.java")