Skip to content

Instantly share code, notes, and snippets.

Avatar

Hitesh Chopra hiteshchopra11

View GitHub Profile
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 Apr 23, 2022
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()
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 Apr 23, 2022
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()
)
}
View Greeting.kt
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
PreviewParameterDemoTheme {
Greeting(name = "viewers")
}
}
}
}