Skip to content

Instantly share code, notes, and snippets.

View hiteshchopra11's full-sized avatar

Hitesh Chopra hiteshchopra11

View GitHub Profile
suspend fun fetchImages(): Result {
return withContext(Dispatchers.IO) {
try {
val result = ImageService.create().getImages()
Result.SuccessWithData(result)
} catch (exception: Exception) {
Result.Error(exception)
}
}
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
PreviewParameterDemoTheme {
Greeting(name = "viewers")
}
}
}
}
@Preview
@Composable
fun PreviewEmployeeInformation(){
EmployeeInformationUI(
fakeEmployeeData()
)
}
if (employeeData.previousCompanies.isNotEmpty()) {
append("\nPreview Companies-: ")
for (company in employeeData.previousCompanies) {
appendWithStyle(text = "$company ")
}
} else {
append("\nNo Previous Companies")
}
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",
@Preview(showBackground = true)
@Composable
fun PreviewEmployeeInformation(
@PreviewParameter(SampleEmployeeDataProvider::class) employeeData: EmployeeData
) {
EmployeeInformationUI(
employeeData
)
}
@hiteshchopra11
hiteshchopra11 / build.gradle
Last active April 23, 2022 19:44
Dependencies for SSE
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
@hiteshchopra11
hiteshchopra11 / MainActivity.kt
Created April 23, 2022 19:42
EventSourceListener
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 / MainActivity.kt
Created April 23, 2022 19:55
OkHttp Client and EventSourceFactory
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()
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()}")
}