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
@GET("rockets") | |
fun getRocketsAsync(): Deferred<Response<List<Rocket>>> | |
@GET("launches/upcoming") | |
fun getNextFlightsAsync(): Deferred<Response<List<Flight>>> | |
@GET("history") | |
fun getPastEventsAsync(): Deferred<Response<List<Event>>> | |
companion object { |
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 "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion" | |
implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:$retrofitCoroutinesAdapterVersion" | |
} |
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
inline fun <reified T: Any> fetchData(crossinline call: (SpaceXService) -> Deferred<Response<List<T>>>): LiveData<List<T>> { | |
val result = MutableLiveData<List<T>>() | |
CoroutineScope(Dispatchers.IO).launch { | |
val request = call(service) | |
withContext(Dispatchers.Main) { | |
try { | |
val response = request.await() | |
if (response.isSuccessful) { | |
result.value = response.body() |
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 RocketViewModel(private val repository: RocketRepository) : BaseViewModel<Rocket>() { | |
private lateinit var rocketList: LiveData<List<Rocket>> | |
override fun getDataFromRetrofit(): LiveData<List<Rocket>> { | |
rocketList = repository.loadData() | |
return rocketList | |
} | |
} |
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 "org.koin:koin-android:$koinVersion" | |
implementation "org.koin:koin-androidx-viewmodel:$koinVersion" | |
} |
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 networkModule = module { | |
single { SpaceXService.create() } | |
} | |
val repositoryModule = module { | |
single { RocketRepository(get(), get()) } | |
single { FlightRepository(get(), get()) } | |
single { EventRepository(get(), get()) } | |
} |
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
startKoin { | |
androidLogger() | |
androidContext(this@MainActivity) | |
modules(listOf(repositoryModule, networkModule, viewModelModule)) | |
} |
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 GenericAdapter<T>(private val context: Context, private val items: List<T>) | |
: RecyclerView.Adapter<RecyclerView.ViewHolder>() { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | |
return when { | |
items.all { it is Rocket } -> RocketViewHolder(LayoutInflater.from(context).inflate(R.layout.item_rocket, parent, false)) | |
items.all { it is Flight } -> FlightViewHolder(LayoutInflater.from(context).inflate(R.layout.item_flight, parent, false)) | |
else -> EventViewHolder(LayoutInflater.from(context).inflate(R.layout.item_event, parent, false)) | |
} | |
} |
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 RocketViewHolder(private val view: View) : RecyclerView.ViewHolder(view), GenericAdapter.Binder<Rocket> { | |
override fun bind(data: Rocket) { | |
view.tv_rocket_name.text = data.name | |
// ... | |
} | |
} |
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
object ObjectBox { | |
lateinit var boxStore: BoxStore | |
private set | |
fun init(context: Context): BoxStore { | |
if (::boxStore.isInitialized && !boxStore.isClosed) { | |
return boxStore | |
} |
OlderNewer