Created
July 28, 2022 12:18
-
-
Save sudhirkrsingh/a9871e960d5593f4bde96d4265d5bf7c to your computer and use it in GitHub Desktop.
APIWithRxJava
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
package com.mega.kotlinapp.postAPIKotlin.mvvvm.network | |
import okhttp3.Interceptor | |
import okhttp3.OkHttpClient | |
import okhttp3.logging.HttpLoggingInterceptor | |
import retrofit2.Retrofit | |
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory | |
import retrofit2.converter.gson.GsonConverterFactory | |
object GetAPI { | |
private const val BASE_URL: String ="https://rpcauat-api.reciproci.com/" | |
private var retrofit: Retrofit? = null | |
fun getCards(): Retrofit? { | |
if (retrofit == null) { | |
val httpLoggingInterceptor = HttpLoggingInterceptor() | |
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | |
val client: OkHttpClient = OkHttpClient.Builder() | |
.addInterceptor(Interceptor { chain: Interceptor.Chain -> | |
val request = chain.request() | |
.newBuilder() | |
.addHeader("COUNTRY", "United Arab Emirates") | |
.addHeader("CITY", "Dubai") | |
.addHeader("LANGUAGE", "EN") | |
.addHeader("IS_FILTERED", "FALSE") | |
.addHeader("Authorization","NJ7FkM0RYiRB4iKR5z4HyQ==") | |
.addHeader("AUTH_TOKEN", "NJ7FkM0RYiRB4iKR5z4HyQ==") | |
.addHeader("Content-Type", "application/json") | |
.addHeader("DEVICE_ID", "cw4M6_7CRsO2hRhA9NSyqC") | |
.addHeader("APP_VERSION", "0.1.71") | |
.build() | |
chain.proceed(request) | |
}) | |
.addInterceptor(httpLoggingInterceptor) | |
.build() | |
retrofit = Retrofit.Builder() | |
.baseUrl(BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.addCallAdapterFactory(RxJava3CallAdapterFactory.create()) | |
.client(client) | |
.build() | |
} | |
return retrofit | |
} | |
} |
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
package com.mega.kotlinapp.postAPIKotlin.mvvvm.network | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.model.GiftCardModel | |
import io.reactivex.rxjava3.core.Single | |
import okhttp3.Interceptor | |
import okhttp3.OkHttpClient | |
import okhttp3.logging.HttpLoggingInterceptor | |
import retrofit2.Retrofit | |
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory | |
import retrofit2.converter.gson.GsonConverterFactory | |
import retrofit2.http.GET | |
interface GetCards { | |
@GET("cans/tt/rest/api/sec/v1/giftCard/giftCardDetails.json") | |
fun getData(): Single<GiftCardModel> | |
companion object{ | |
private val BASE_URL: String ="https://rpcauat-api.reciproci.com/" | |
private var retrofit: Retrofit? = null | |
fun getCards(): Retrofit? { | |
if (retrofit == null) { | |
val httpLoggingInterceptor = HttpLoggingInterceptor() | |
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | |
val client: OkHttpClient = OkHttpClient.Builder() | |
.addInterceptor(Interceptor { chain: Interceptor.Chain -> | |
val request = chain.request() | |
.newBuilder() | |
.addHeader("COUNTRY", "United Arab Emirates") | |
.addHeader("CITY", "Dubai") | |
.addHeader("LANGUAGE", "EN") | |
.addHeader("IS_FILTERED", "FALSE") | |
.addHeader("Authorization","NJ7FkM0RYiRB4iKR5z4HyQ==") | |
.addHeader("AUTH_TOKEN", "NJ7FkM0RYiRB4iKR5z4HyQ==") | |
.addHeader("Content-Type", "application/json") | |
.addHeader("DEVICE_ID", "cw4M6_7CRsO2hRhA9NSyqC") | |
.addHeader("APP_VERSION", "0.1.71") | |
.build() | |
chain.proceed(request) | |
}) | |
.addInterceptor(httpLoggingInterceptor) | |
.build() | |
retrofit = Retrofit.Builder() | |
.baseUrl(BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.addCallAdapterFactory(RxJava3CallAdapterFactory.create()) | |
.client(client) | |
.build() | |
} | |
return retrofit | |
} | |
} | |
} |
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
package com.mega.kotlinapp.postAPIKotlin.mvvvm.view | |
import android.annotation.SuppressLint | |
import android.os.Bundle | |
import android.util.Log | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.Toast | |
import androidx.fragment.app.Fragment | |
import androidx.lifecycle.ViewModelProvider | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import com.mega.kotlinapp.databinding.FragmentGetCardsBinding | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.Database.AppDatabase | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.model.CardsForRoomData | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.network.GetAPI | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.network.GetCards | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.repo.Repository | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.viewModel.GiftCardViewModel | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.viewModel.ViewModelFactory | |
class GetCardsFragment : Fragment() { | |
lateinit var binding:FragmentGetCardsBinding | |
lateinit var viewModel: GiftCardViewModel | |
private val retrofitService:GetCards = GetAPI.getCards()!!.create(GetCards::class.java) | |
private val service:GetCards = GetCards.getCards()!!.create(GetCards::class.java) | |
lateinit var database: AppDatabase | |
@SuppressLint("FragmentLiveDataObserve") | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle?): View? { | |
// Inflate the layout for this fragment | |
binding= FragmentGetCardsBinding.inflate(inflater, container, false) | |
binding.recview.layoutManager=LinearLayoutManager(context) | |
database= context?.let { AppDatabase.getDatabase(it) }!! | |
viewModel = | |
ViewModelProvider(this,ViewModelFactory(Repository(service))).get( | |
GiftCardViewModel::class.java | |
) | |
viewModel.getAllCards() | |
viewModel.cardList.observe(this) { it -> | |
if (it != null) { | |
// | |
var size:Int | |
size = it.size | |
Toast.makeText(context, "checking: "+size, Toast.LENGTH_SHORT).show() | |
for (i in 0..size-1){ | |
var cardsForRoomData = CardsForRoomData(1,marketingContent01 = it[i].marketingContent01,marketingContent02 = it[i].marketingContent02,giftCardName = it[i].giftCardName) | |
database!!.daoForRom().insertCards(cardsForRoomData) | |
} | |
// it[0].marketingContent01 | |
// it[1].marketingContent02 | |
binding.recview.adapter = context?.let { it1 -> CardAdapter(it1, it) } | |
// Toast.makeText(context, "data" + it[0], Toast.LENGTH_SHORT).show() | |
Toast.makeText(context, ""+Thread.currentThread(), Toast.LENGTH_SHORT).show() | |
Log.v("jh","thread"+Thread.currentThread()) | |
} | |
else { | |
} | |
} | |
// viewModel.movieList.observe(this, io.reactivex.rxjava3.core.Observer { it -> | |
// //TODO | |
// }) | |
// viewModel.movieList.observe(viewLifecycleOwner, Observer { | |
// if (it != null) { | |
// adapter.list= | |
// } else { | |
// Toast.makeText(this, "Error in fetching data", Toast.LENGTH_SHORT).show() | |
// } | |
// }) | |
return binding.root | |
} | |
override fun onDestroy() { | |
//don't send events once the activity is destroyed | |
viewModel.disposable.dispose() | |
super.onDestroy() | |
} | |
} |
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
package com.mega.kotlinapp.postAPIKotlin.mvvvm.model | |
import android.os.Parcel | |
import android.os.Parcelable | |
import com.google.gson.annotations.SerializedName | |
data class GiftCardModel ( | |
@SerializedName("giftCardResponseList" ) var giftCardResponseList : ArrayList<GiftCardResponseList> = arrayListOf(), | |
@SerializedName("status" ) var status : String? = null | |
):Parcelable { | |
constructor(parcel: Parcel) : this( | |
TODO("giftCardResponseList"), | |
parcel.readString() | |
) { | |
} | |
override fun describeContents(): Int { | |
TODO("Not yet implemented") | |
} | |
override fun writeToParcel(p0: Parcel?, p1: Int) { | |
TODO("Not yet implemented") | |
} | |
companion object CREATOR : Parcelable.Creator<GiftCardModel> { | |
override fun createFromParcel(parcel: Parcel): GiftCardModel { | |
return GiftCardModel(parcel) | |
} | |
override fun newArray(size: Int): Array<GiftCardModel?> { | |
return arrayOfNulls(size) | |
} | |
} | |
} |
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
package com.mega.kotlinapp.postAPIKotlin.mvvvm.model | |
import android.os.Parcel | |
import android.os.Parcelable | |
import com.google.gson.annotations.SerializedName | |
data class GiftCardResponseList ( | |
@SerializedName("productType" ) var productType : String? = null, | |
@SerializedName("giftCardCode" ) var giftCardCode : String? = null, | |
@SerializedName("currencyCode" ) var currencyCode : String? = null, | |
@SerializedName("faceValue" ) var faceValue : Int? = null, | |
@SerializedName("activationMaxAmt" ) var activationMaxAmt : Int? = null, | |
@SerializedName("activationMinAmt" ) var activationMinAmt : Int? = null, | |
@SerializedName("allowedActivate" ) var allowedActivate : Int? = null, | |
@SerializedName("allowedReload" ) var allowedReload : Int? = null, | |
@SerializedName("allowedRedeem" ) var allowedRedeem : Int? = null, | |
@SerializedName("isFixValue" ) var isFixValue : Int? = null, | |
@SerializedName("giftCardName" ) var giftCardName : String? = null, | |
@SerializedName("termAndCondition" ) var termAndCondition : String? = null, | |
@SerializedName("giftCardImageUrl" ) var giftCardImageUrl : String? = null, | |
@SerializedName("description" ) var description : String? = null, | |
@SerializedName("marketingContent01" ) var marketingContent01 : String? = null, | |
@SerializedName("marketingContent02" ) var marketingContent02 : String? = null, | |
@SerializedName("giftCardStatus" ) var giftCardStatus : String? = null, | |
@SerializedName("barcodeNumber" ) var barcodeNumber : String? = null, | |
@SerializedName("giftCardPurchaseType" ) var giftCardPurchaseType : String? = null, | |
@SerializedName("purchaseContent" ) var purchaseContent : String? = null, | |
@SerializedName("denominationAmount01" ) var denominationAmount01 : Int? = null, | |
@SerializedName("denominationAmount02" ) var denominationAmount02 : Int? = null, | |
@SerializedName("mobileNumber" ) var mobileNumber : String? = null, | |
@SerializedName("activationAmount" ) var activationAmount : Int? = null, | |
@SerializedName("referenceNumber" ) var referenceNumber : String? = null, | |
@SerializedName("expiryDate" ) var expiryDate : String? = null, | |
@SerializedName("purchaseDate" ) var purchaseDate : String? = null, | |
@SerializedName("latestBalance" ) var latestBalance : String? = null, | |
@SerializedName("sharedWith" ) var sharedWith : String? = null, | |
@SerializedName("giftedBy" ) var giftedBy : String? = null, | |
@SerializedName("activationBarcodeNumber" ) var activationBarcodeNumber : String? = null, | |
@SerializedName("balanceBarcodeNumber" ) var balanceBarcodeNumber : String? = null, | |
@SerializedName("status" ) var status : String? = null | |
):Parcelable { | |
constructor(parcel: Parcel) : this( | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readValue(Int::class.java.classLoader) as? Int, | |
parcel.readValue(Int::class.java.classLoader) as? Int, | |
parcel.readValue(Int::class.java.classLoader) as? Int, | |
parcel.readValue(Int::class.java.classLoader) as? Int, | |
parcel.readValue(Int::class.java.classLoader) as? Int, | |
parcel.readValue(Int::class.java.classLoader) as? Int, | |
parcel.readValue(Int::class.java.classLoader) as? Int, | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readValue(Int::class.java.classLoader) as? Int, | |
parcel.readValue(Int::class.java.classLoader) as? Int, | |
parcel.readString(), | |
parcel.readValue(Int::class.java.classLoader) as? Int, | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString(), | |
parcel.readString() | |
) { | |
} | |
override fun writeToParcel(parcel: Parcel, flags: Int) { | |
parcel.writeString(productType) | |
parcel.writeString(giftCardCode) | |
parcel.writeString(currencyCode) | |
parcel.writeValue(faceValue) | |
parcel.writeValue(activationMaxAmt) | |
parcel.writeValue(activationMinAmt) | |
parcel.writeValue(allowedActivate) | |
parcel.writeValue(allowedReload) | |
parcel.writeValue(allowedRedeem) | |
parcel.writeValue(isFixValue) | |
parcel.writeString(giftCardName) | |
parcel.writeString(termAndCondition) | |
parcel.writeString(giftCardImageUrl) | |
parcel.writeString(description) | |
parcel.writeString(marketingContent01) | |
parcel.writeString(marketingContent02) | |
parcel.writeString(giftCardStatus) | |
parcel.writeString(barcodeNumber) | |
parcel.writeString(giftCardPurchaseType) | |
parcel.writeString(purchaseContent) | |
parcel.writeValue(denominationAmount01) | |
parcel.writeValue(denominationAmount02) | |
parcel.writeString(mobileNumber) | |
parcel.writeValue(activationAmount) | |
parcel.writeString(referenceNumber) | |
parcel.writeString(expiryDate) | |
parcel.writeString(purchaseDate) | |
parcel.writeString(latestBalance) | |
parcel.writeString(sharedWith) | |
parcel.writeString(giftedBy) | |
parcel.writeString(activationBarcodeNumber) | |
parcel.writeString(balanceBarcodeNumber) | |
parcel.writeString(status) | |
} | |
override fun describeContents(): Int { | |
return 0 | |
} | |
companion object CREATOR : Parcelable.Creator<GiftCardResponseList> { | |
override fun createFromParcel(parcel: Parcel): GiftCardResponseList { | |
return GiftCardResponseList(parcel) | |
} | |
override fun newArray(size: Int): Array<GiftCardResponseList?> { | |
return arrayOfNulls(size) | |
} | |
} | |
} |
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
package com.mega.kotlinapp.postAPIKotlin.mvvvm.viewModel | |
import android.util.Log | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.ViewModel | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.model.GiftCardModel | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.model.GiftCardResponseList | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.repo.Repository | |
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers | |
import io.reactivex.rxjava3.annotations.NonNull | |
import io.reactivex.rxjava3.core.Observer | |
import io.reactivex.rxjava3.core.SingleObserver | |
import io.reactivex.rxjava3.disposables.Disposable | |
import io.reactivex.rxjava3.schedulers.Schedulers | |
class GiftCardViewModel (var repository: Repository) : ViewModel(){ | |
val cardList = MutableLiveData<List<GiftCardResponseList>>() | |
val errorMessage = MutableLiveData<String>() | |
lateinit var disposable: Disposable | |
fun getAllCards() { | |
//observer subscribing to observable | |
val response = repository.getAllCards() | |
response.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(singleObservable()) | |
} | |
// this is for single type observable | |
fun singleObservable(): SingleObserver<GiftCardModel> { | |
return object : SingleObserver<GiftCardModel> { | |
override fun onSubscribe(d: Disposable) { | |
disposable=d | |
} | |
override fun onSuccess(data: GiftCardModel) { | |
cardList.postValue(data.giftCardResponseList) | |
Log.v("jfhsd","thread"+Thread.currentThread()) | |
} | |
override fun onError(e: Throwable) { | |
// Error or Exception thrown. | |
} | |
} | |
} | |
// this is for normal type observable | |
private fun getCardsListObserver(): Observer<GiftCardModel> { | |
return object : Observer<GiftCardModel> { | |
override fun onSubscribe(d: Disposable) { | |
disposable=d | |
Log.v("subs","done") | |
} | |
override fun onNext(t: @NonNull GiftCardModel) { // run on main thread | |
cardList.postValue(t.giftCardResponseList) | |
} | |
override fun onError(e: Throwable) {} | |
override fun onComplete() {} | |
} | |
} | |
} | |
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
package com.mega.kotlinapp.postAPIKotlin.mvvvm.repo | |
import com.mega.kotlinapp.postAPIKotlin.mvvvm.network.GetCards | |
class Repository(private val cards: GetCards) { | |
fun getAllCards() = cards.getData() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment