Skip to content

Instantly share code, notes, and snippets.

View hiteshchopra11's full-sized avatar

Hitesh Chopra hiteshchopra11

View GitHub Profile
fun Application.notesRoutes() {
val db = DatabaseConnection.database
routing {
get("/notes") {
val notes = db.from(NotesEntity).select().map {
val id = it[NotesEntity.id]
val note = it[NotesEntity.note]
Note(id ?: -1, note ?: "")
}
@hiteshchopra11
hiteshchopra11 / MainActivityViewModel.kt
Last active December 11, 2021 10:55
Class Extending the ViewModel class
class MainActivityViewModel : ViewModel() {
private val _counterLiveData = MutableLiveData(0)
val counterLiveData: LiveData<Int> = _counterLiveData
fun incrementByOne() {
_counterLiveData.value = _counterLiveData.value?.plus(1)
}
}
val viewModel = MainActivityViewModel()
findViewById<Button>(R.id.btn_increment).setOnClickListener {
viewModel.incrementByOne()
}
viewModel.counterLiveData.observe(this) { counter ->
findViewById<TextView>(R.id.tv_counter).text = "$counter"
}
val viewModel = ViewModelProvider(this)[MainActivityViewModel::class.java]
findViewById<Button>(R.id.btn_increment).setOnClickListener {
viewModel.incrementByOne()
}
viewModel.counterLiveData.observe(this) { counter ->
findViewById<TextView>(R.id.tv_counter).text = "$counter"
}
public constructor(
owner: ViewModelStoreOwner
) : this(owner.viewModelStore, defaultFactory(owner))
interface ImageService {
suspend fun getImages(): ImageResponse
}
interface ImageService {
@GET("/photos?page=1")
suspend fun getImages(): ImageResponse
}
object RetrofitBuilder {
private const val BASE_URL = "https://api.unsplash.com/"
private fun getRetrofit(): Retrofit {
class ImageServiceImpl(
private val client: HttpClient
) : ImageService {
override suspend fun getImages(): ImageResponse {
return client.get {
headers {
append(HttpHeaders.Authorization, "Client-ID ${BuildConfig.CLIENT_ID}")
}
url(HttpRoutes.FETCH_IMAGES_URL)
}
interface ImageService {
suspend fun getImages(): ImageResponse
companion object {
@ExperimentalSerializationApi fun create(): ImageService {
return ImageServiceImpl(
client = HttpClient(Android) {
install(Logging)
install(JsonFeature) {
serializer = GsonSerializer()
public class HttpClient(
public val engine: HttpClientEngine,
private val userConfig: HttpClientConfig<out HttpClientEngineConfig> = HttpClientConfig()
) : {
init {
with(userConfig) {
config.install(HttpRequestLifecycle)
config.install(BodyProgress)
if (useDefaultTransformers) {