Skip to content

Instantly share code, notes, and snippets.

View alifhasnain's full-sized avatar
🪲
debugging

Alif Hasnain alifhasnain

🪲
debugging
  • IOTA Infotech Limited
  • Dhaka,Bangladesh
View GitHub Profile

What are Sealed classes?

Sealed classes and interfaces represent restricted class hierarchies that provide more control over inheritance. All direct subclasses of a sealed class are known at compile time. No other subclasses may appear after a module with the sealed class is compiled.

So, what gives it more control on handling UI state?

As we can see that all subclasses of a sealed class is known at compile time it gives us the ability to use it inside when block and the compiler is able to warn us if any branch is not handled, in exactly the same way that it does for enumerations.

@alifhasnain
alifhasnain / ThumbUtils.kt
Last active February 22, 2023 18:22
A util class to generate thumbnail for image and videos.
import android.content.ContentResolver
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
@alifhasnain
alifhasnain / GridSpacingItemDecoration.kt
Created October 21, 2021 04:05
Equal Spacing for items in GridLayoutManager
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
class GridSpacingItemDecoration(
private val spanCount: Int,
@alifhasnain
alifhasnain / JSON Parsing.md
Last active February 22, 2023 18:23
JSON parsing with various library and various method.

GSON

TypeAdapter

We can create custom type adapters for dirty JSON files. Suppose we have a JSON text like below:

{
  "id": 2,
  "name": "Alif Hasnain",
  "age": {
    "age": 24,
    "mature": true
@alifhasnain
alifhasnain / LocationObserver.java
Created February 8, 2021 20:18
Lifecycle aware location observer.
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.os.Looper;
import androidx.core.app.ActivityCompat;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
@alifhasnain
alifhasnain / RxJava Notes.md
Last active June 21, 2021 06:56
RxJava important notes.
@alifhasnain
alifhasnain / NetworkUtils.kt
Created November 21, 2020 20:03
This is a class for observing network state. It was taken Foodium App.
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
/**
@alifhasnain
alifhasnain / GitNotes.md
Last active March 21, 2023 17:07
Git Notes

Add new remote

git remote add <origin_name> <url_of_the_remote_repo>

Update existing remote

git remote set-url <origin> <url_of_the_remote_repo>

To see remotes names

git remote

@alifhasnain
alifhasnain / UnitTestingNotes.md
Last active November 17, 2020 21:24
Unit Testing Notes

Instantiate a mock object (Without Annotation)

Mockito.mock(ClassName.class);

Instantiate a mock object (With Annotation)

@RunWith(MockitoJUnitRunner.class)
public class MyTestClassTest {
@alifhasnain
alifhasnain / Event.kt
Created September 29, 2020 21:02
A Kotlin class which can help as being a wrapper around any other class to trigger the LiveData once.
class Event<out R>(private val data: R) {
var hasEventBeenHandled = false
private set
val content: R?
get() = if (!hasEventBeenHandled) {
hasEventBeenHandled = true
data
} else {