Skip to content

Instantly share code, notes, and snippets.

View parthdesai1208's full-sized avatar
🏢
Working from office

Parth Desai parthdesai1208

🏢
Working from office
View GitHub Profile
open class A{
init {
println("init A")
}
constructor(){
println("primary constructor of A")
}
constructor(x:Int) : this() {
@parthdesai1208
parthdesai1208 / Composite
Last active February 12, 2024 14:20
Design Patterns
interface Components{
void showPrice();
}
class Composite implements Components{
String name;
List<Components> componentsList = new ArrayList<>();
public Composite(String name){
super();
//one-to-many relation
@Entity(tableName = "TicketGroup")
data class TicketGroup(
@PrimaryKey(autoGenerate = false)
val groupId: Int,
val groupName: String,
)
@Entity(tableName = "Ticket")
data class Ticket(
@parthdesai1208
parthdesai1208 / validation using custom annotation
Created January 31, 2023 09:31
custom annotation for validation
class MainActivity : AppCompatActivity(), ConstraintValidatorContext {
lateinit var edt: EditText
lateinit var btn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
edt = findViewById(R.id.edt)
@parthdesai1208
parthdesai1208 / rememberSaveable
Last active January 10, 2023 16:52
restore state in compose
************************************************************************************************************************************
restore state of list of Int, String using rememberSaveable
************************************************************************************************************************************
import androidx.compose.runtime.Composable
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.toMutableStateList
@Preview(group = "phone", name = "light", device = Devices.PIXEL_4_XL, showSystemUi = true)
@Preview(
group = "phone",
name = "dark",
uiMode = UI_MODE_NIGHT_YES,
device = Devices.PIXEL_4_XL,
showSystemUi = true,
showBackground = true
)
annotation class Phone
@parthdesai1208
parthdesai1208 / Flow Demo
Last active November 29, 2022 14:01
Flow APIs
*********************************************************StateFlow Demo**************************************************************
Step-1) build.gradle
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.6"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6"
Step-2) data class
data class Resource<out T>(val status: Status, val data: T?, val message: String?) {
companion object {
@parthdesai1208
parthdesai1208 / Interceptors
Created November 16, 2022 07:34
Retrofit interceptors
Gzip interceptors = for data compression
public class GzipRequestInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
return chain.proceed(originalRequest);
}
Request compressedRequest = originalRequest.newBuilder()
@parthdesai1208
parthdesai1208 / to get foldable postures
Created November 9, 2022 14:27
Compose for every screen
//WindowStateUtils.kt
/**
* Information about the posture of the device
*/
sealed interface DevicePosture {
object NormalPosture : DevicePosture
data class BookPosture(
val hingePosition: Rect
) : DevicePosture
@parthdesai1208
parthdesai1208 / App.kt
Created October 14, 2022 16:22
shared preferences using kotlin delegation
val sharedApplicationContext: Context get() = sharedApplicationContextBackingProperty
?: throw IllegalStateException(
"Application context not initialized yet."
)
private var sharedApplicationContextBackingProperty: Context? = null
class App : Application() {
override fun onCreate() {
super.onCreate()