Untitled

mail@pastecode.io avatar
unknown
kotlin
2 years ago
1.9 kB
1
Indexable
Never
fun loadMovies(): List<ResultsItem>?{
        val list = ArrayList<ResultsItem>()
        val client = ApiConfig.getApiService().getMovies(apiKey)
        client.enqueue(object : Callback<MovieResponse>{
            override fun onResponse(call: Call<MovieResponse>, response: Response<MovieResponse>){
                if(response.isSuccessful){
//                    val resultsItem: List<ResultsItem>?
                    val resultsItem = response.body()?.results
                    Log.d(TAG, "resultItem: $resultsItem")
                    for (result in resultsItem!!){
                        val listResult = ResultsItem(result.overview,
                            result.originalLanguage,
                            result.originalTitle,
                            result.video,
                            result.title,
                            result.genreIds,
                            result.posterPath,
                            result.backdropPath,
                            result.releaseDate,
                            result.mediaType,
                            result.voteAverage,
                            result.popularity,
                            result.id,
                            result.adult,
                            result.voteCount)
                        list.add(listResult)
                        Log.d(TAG, "listresult: $listResult")
                    }
                    Log.d(TAG, "ArrayList1: $list")
                } else{
                    Log.e(TAG, "onFailure: ${response.message()}")
                }
            }

            override fun onFailure(call: Call<MovieResponse>, t: Throwable) {
                Log.e(TAG, "onFailure: ${t.message.toString()}")
            }

        })
        Log.d(TAG, "ArrayList2: $list")
        return list
    }