class APIClient {
companion object {
val BASE_URL = "baseURL"
val client = OkHttpClient.Builder()
.connectTimeout(100, TimeUnit.SECONDS)
.readTimeout(100, TimeUnit.SECONDS)
.writeTimeout(100, TimeUnit.SECONDS)
.build()
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL).client(client).build()
val api = retrofit.create(RetrofitApi::class.java)
}
}
Interface for API
interface RetrofitApi {
@GET(BuildConfig.categories)
fun getCategories(): Observable<ModelClass>
@POST(BuildConfig.authurl)
fun Login(@Query("username") username: String,@Query("password") password: String): Observable<Loginmodel>
}
Call API From Activity
// Calling one API and will get whole data and parse manually
private val disposables = CompositeDisposable()
fun GetRMDCategories() {
APIClient.api.getCategories()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(IoScheduler())
.subscribe(
{ Log.e(TAG, "GetRMDCategories: ${it} \n\n")
val Response = it.string()
// val Response = loadJSONFromAsset() if (Response != null) {
// parseJSON(Response) displayData(Response)
}
},
{ Log.i(TAG, "Error while GetRMDCategories: ${it.message}")
} ).addTo(disposables)
}
in companion object
companion object {
fun Disposable.addTo(compositeDisposable: CompositeDisposable): Disposable =
apply { compositeDisposable.add(this) }}
Fill data inTo Adapter
private fun displayData(Response: String) {
ProgressBar.visibility = View.GONE CategoryListWithItems = JsonParser().parseCategoryJson(Response)
rv_parent.adapter =
CategoriesAdapter(CategoryListWithItems,
onAudioButtonClicked = { position1, audio, audioplaylist, videoDetails, playlist -> progressbar_bottom.visibility = View.VISIBLE
showCustomDialog(position1, audio, audioplaylist, videoDetails, playlist)
})
}
Adapter
class CategoriesAdapter(
private val videoItems: ArrayList<RMD_CategoryListWithItems>,
private val onAudioButtonClicked: (position: Int, audio: AudioModel, audioPlaylist: java.util.ArrayList<AudioModel>, videoDetails: RMD_CategoryItems, playlist: java.util.ArrayList<String>) -> Unit
) : RecyclerView.Adapter<RMD_CategoriesAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v =
LayoutInflater.from(parent.context).inflate(R.layout.item_video_category, parent, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return videoItems.size }
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
notifyDataSetChanged()
super.onAttachedToRecyclerView(recyclerView)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val category = videoItems.get(position)
holder.recyclerView.apply { layoutManager =
LinearLayoutManager(holder.recyclerView.context, RecyclerView.HORIZONTAL, false)
holder.textView.text = category.title_name adapter = CategoryItemsAdapter(
category.categoryItems,
category.category_playlist,
context,
category.audioPlayList,
onAudioClick = { position1, audio, audioplaylist, videoDetails,playlist-> onAudioButtonClicked.invoke(
position1,
audio,
audioplaylist,videoDetails,playlist
)
} )
} }
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val recyclerView: RecyclerView = itemView.rv_child as RecyclerView
val textView: TextView = itemView.textView
}
}
0 comments:
Post a Comment