This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun fetchImages(): Result { | |
return withContext(Dispatchers.IO) { | |
try { | |
val result = ImageService.create().getImages() | |
Result.SuccessWithData(result) | |
} catch (exception: Exception) { | |
Result.Error(exception) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
PreviewParameterDemoTheme { | |
Greeting(name = "viewers") | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Preview | |
@Composable | |
fun PreviewEmployeeInformation(){ | |
EmployeeInformationUI( | |
fakeEmployeeData() | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (employeeData.previousCompanies.isNotEmpty()) { | |
append("\nPreview Companies-: ") | |
for (company in employeeData.previousCompanies) { | |
appendWithStyle(text = "$company ") | |
} | |
} else { | |
append("\nNo Previous Companies") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Preview(showBackground = true) | |
@Composable | |
fun PreviewEmployeeInformation( | |
@PreviewParameter(SampleEmployeeDataProvider::class) employeeData: EmployeeData | |
) { | |
EmployeeInformationUI( | |
employeeData | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}") | |
} |