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
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 ?: "") | |
} |
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 MainActivityViewModel : ViewModel() { | |
private val _counterLiveData = MutableLiveData(0) | |
val counterLiveData: LiveData<Int> = _counterLiveData | |
fun incrementByOne() { | |
_counterLiveData.value = _counterLiveData.value?.plus(1) | |
} | |
} |
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 viewModel = MainActivityViewModel() | |
findViewById<Button>(R.id.btn_increment).setOnClickListener { | |
viewModel.incrementByOne() | |
} | |
viewModel.counterLiveData.observe(this) { counter -> | |
findViewById<TextView>(R.id.tv_counter).text = "$counter" | |
} |
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 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" | |
} |
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
public constructor( | |
owner: ViewModelStoreOwner | |
) : this(owner.viewModelStore, defaultFactory(owner)) |
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
interface ImageService { | |
suspend fun getImages(): ImageResponse | |
} |
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
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 { |
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 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) | |
} |
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
interface ImageService { | |
suspend fun getImages(): ImageResponse | |
companion object { | |
@ExperimentalSerializationApi fun create(): ImageService { | |
return ImageServiceImpl( | |
client = HttpClient(Android) { | |
install(Logging) | |
install(JsonFeature) { | |
serializer = GsonSerializer() |
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
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) { |