Skip to content

Instantly share code, notes, and snippets.

View CostaFot's full-sized avatar
🍦

Costa Fotiadis CostaFot

🍦
View GitHub Profile
@CostaFot
CostaFot / build.gradle
Last active February 25, 2019 19:39
Dependencies
// RX Java
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.2'
// Networking
implementation "com.squareup.retrofit2:retrofit:2.4.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
implementation "com.squareup.retrofit2:converter-gson:2.4.0"
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
@CostaFot
CostaFot / Repository.kt
Created February 17, 2019 23:22
Generic repository
open class Repository(
baseUrl: String,
isDebugEnabled: Boolean,
apiKey: String
) {
private val apiKeyHeader: String = "x-api-key"
val retrofit: Retrofit
init {
@CostaFot
CostaFot / CatsDataSource.kt
Created February 17, 2019 23:37
This is where the GET is located
class CatsDataSource(retrofit: Retrofit) {
private val api: CatsApi = retrofit.create(CatsApi::class.java)
fun getNumberOfRandomCats(limit: Int, category_ids: Int?) =
api.getNumberOfRandomCats(limit, category_ids)
interface CatsApi {
@GET("images/search")
@CostaFot
CostaFot / NetCat.kt
Created February 17, 2019 23:39
The cat model
/**
* The class representing the Json response. Use http://www.jsonschema2pojo.org/ to get this.
* Or you can add this plugin for AS here https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-
* It will create your data class from JSON to kotlin.
*/
data class NetCat(
@SerializedName("id") val id: String,
@SerializedName("url") val url: String,
@SerializedName("breeds") val breeds: List<Any>,
@CostaFot
CostaFot / CatsRepository.kt
Created February 17, 2019 23:45
~The main guy
/**
* This guy extends Repository class so the retrofit variable will be available to use as it's instantiated in the init!
*/
class CatsRepository(
baseUrl: String,
isDebugEnabled: Boolean,
apiKey: String
) : Repository(baseUrl, isDebugEnabled, apiKey) {
private val catsDataSource: CatsDataSource = CatsDataSource(retrofit)
@CostaFot
CostaFot / MainActivity.kt
Created February 17, 2019 23:55
Sample activity
class MainActivity : AppCompatActivity() {
// Read the docs with detailed instructions to get your API key and endpoint!
// https://docs.thecatapi.com/
// the server url endpoint
private val serverUrl = "https://api.thecatapi.com/v1/"
// this is where you declare your api key
private val apiKey = "yourApiKeyHere"
@CostaFot
CostaFot / build.gradle
Last active February 25, 2019 19:39
Glide dependencies
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
// need this plugin to make this work
apply plugin: 'kotlin-kapt'
android {
// your android stuff goes here
}
@CostaFot
CostaFot / GlideAppModule.kt
Created February 19, 2019 22:03
The Glide module (???)
@GlideModule
class GlideAppModule : AppGlideModule()
@CostaFot
CostaFot / activity_main.xml
Created February 19, 2019 22:17
very complicated layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
@CostaFot
CostaFot / AndroidManifest.xml
Last active February 19, 2019 23:15
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.yourpackagenamehere">
<dist:module dist:instant="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"