This file contains hidden or 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
// Retrofit | |
implementation 'com.squareup.retrofit2:retrofit:2.6.1' | |
implementation 'com.squareup.retrofit2:converter-gson:2.6.1' | |
implementation 'com.squareup.okhttp3:logging-interceptor:4.1.1' | |
// Kotlin Coroutines | |
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3' | |
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3' |
This file contains hidden or 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 ApiService { | |
companion object { | |
val BASE_URL: String = "https://demo.dataverse.org/api/" | |
} | |
@GET("search") | |
suspend fun getResult( | |
@Query("q") query: String?): Response<SearchResultModel?> | |
} |
This file contains hidden or 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
data class SearchResultModel( | |
val `data`: Data?, | |
val status: String? | |
) { | |
data class Data( | |
val count_in_response: Int?, | |
val items: List<Item?>?, | |
val q: String?, | |
val spelling_alternatives: SpellingAlternatives?, | |
val start: Int?, |
This file contains hidden or 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 ApiProvider { | |
private val retrofit = Retrofit.Builder() | |
.baseUrl(ApiService.BASE_URL) | |
.client(getHttpClient()) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
private fun getHttpClient(): OkHttpClient { | |
val logging = HttpLoggingInterceptor() | |
logging.level = HttpLoggingInterceptor.Level.BODY |
This file contains hidden or 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 DataRepository { | |
var apiService: ApiService? = null | |
var searchResultsLiveData: MutableLiveData<GenericDataModel<SearchResultModel>?> = MutableLiveData() | |
init { | |
apiService = | |
ApiProvider.createService( | |
ApiService::class.java | |
) | |
} |
This file contains hidden or 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
data class GenericDataModel<T>(val isSuccess: Boolean?, val data: T?, val message: String?) |
This file contains hidden or 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
private fun startSearching(searchQuery: String?) { | |
apiJob = CoroutineScope(Dispatchers.IO).launch { | |
DataRepository.getSearchResults(searchQuery) | |
withContext(Dispatchers.Main) { | |
DataRepository.searchResultsLiveData.observe( | |
this@MainActivity, | |
Observer { genericDataModel: GenericDataModel<SearchResultModel>? -> | |
run { | |
if (genericDataModel?.isSuccess == true) { |
This file contains hidden or 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
searchEditText?.addTextChangedListener(object: TextWatcher { | |
override fun afterTextChanged(s: Editable?) { | |
resultTextView?.visibility = View.GONE | |
progressLoading?.visibility = View.VISIBLE | |
apiJob?.cancel() | |
startSearching(s.toString()) | |
} | |
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { |
This file contains hidden or 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
private fun startSearching(searchQuery: String?) { | |
CoroutineScope(Dispatchers.IO).launch { | |
DataRepository.getSearchResults(searchQuery) | |
withContext(Dispatchers.Main) { | |
} | |
} | |
} |
OlderNewer