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 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 | |
| } |
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
| Gist of generic classes from codingwithmitch.com | |
| List: | |
| - StateResource.kt | |
| - DataState.kt | |
| - BaseViewModel.kt |
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
| 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>>> { |
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
| // 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) |
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
| /** | |
| * 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. |
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
| 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 |
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
| 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) |
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
| 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> { |
NewerOlder