Skip to content

Instantly share code, notes, and snippets.

View hiteshchopra11's full-sized avatar

Hitesh Chopra hiteshchopra11

View GitHub Profile
View GenericSnackBarHostState.kt
package com.example.samplecompose
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.Box
import androidx.compose.material.SnackbarResult
import androidx.compose.runtime.Composable
@hiteshchopra11
hiteshchopra11 / MainActvity.kt
Created April 23, 2022 20:28
Complete Code
View MainActvity.kt
class MainActivity : AppCompatActivity() {
companion object {
private const val TAG = "MainActivity"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
View MainActivity.kt
lifecycleScope.launchWhenCreated {
withContext(Dispatchers.IO) {
client.newCall(request).enqueue(responseCallback = object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.e(MainActivity.TAG, "API Call Failure ${e.localizedMessage}")
}
override fun onResponse(call: Call, response: Response) {
Log.d(MainActivity.TAG, "APi Call Success ${response.body.toString()}")
}
@hiteshchopra11
hiteshchopra11 / MainActivity.kt
Created April 23, 2022 19:55
OkHttp Client and EventSourceFactory
View MainActivity.kt
val client = OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.MINUTES)
.writeTimeout(10, TimeUnit.MINUTES)
.build()
val request = Request.Builder()
.url("https://test-sse-backend.herokuapp.com/events")
.header("Accept", "application/json; q=0.5")
.addHeader("Accept", "text/event-stream")
.build()
@hiteshchopra11
hiteshchopra11 / MainActivity.kt
Created April 23, 2022 19:42
EventSourceListener
View MainActivity.kt
val eventSourceListener = object : EventSourceListener() {
override fun onOpen(eventSource: EventSource, response: Response) {
super.onOpen(eventSource, response)
Log.d(MainActivity.TAG, "Connection Opened")
}
override fun onClosed(eventSource: EventSource) {
super.onClosed(eventSource)
Log.d(MainActivity.TAG, "Connection Closed")
}
@hiteshchopra11
hiteshchopra11 / build.gradle
Last active April 23, 2022 19:44
Dependencies for SSE
View build.gradle
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
// Okhttp
implementation "com.squareup.okhttp3:okhttp:4.9.3"
// Server Sent Events
View EmployeeInformationScreen.kt
@Preview(showBackground = true)
@Composable
fun PreviewEmployeeInformation(
@PreviewParameter(SampleEmployeeDataProvider::class) employeeData: EmployeeData
) {
EmployeeInformationUI(
employeeData
)
}
View ampleEmployeeDataProvider.kt
class SampleEmployeeDataProvider : PreviewParameterProvider<EmployeeData> {
override val values: Sequence<EmployeeData> = sequenceOf(
EmployeeData(
name = "John Rick",
id = 34,
address = "767, abc colony, xyz city",
dob = "01-01-1999",
bankDetails = BankDetails(
accountNumber = 1234,
ifsc = "IFSC",
View EmployeeInformationScreen.kt
if (employeeData.previousCompanies.isNotEmpty()) {
append("\nPreview Companies-: ")
for (company in employeeData.previousCompanies) {
appendWithStyle(text = "$company ")
}
} else {
append("\nNo Previous Companies")
}
View EmployeeInformation.kt
@Preview
@Composable
fun PreviewEmployeeInformation(){
EmployeeInformationUI(
fakeEmployeeData()
)
}