Last active
December 12, 2023 08:11
-
-
Save kashifo/1a92f5000bb8c8a6473b9a6ec533d6b5 to your computer and use it in GitHub Desktop.
Dish Like Base Reference
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
| @GET("/volumes") | |
| fun searchBooks(@Query("userId") userId: String): Call<List<GBook>> | |
| @Headers("api-key: " + Constants.API_KEY) | |
| @GET("/zelish/user/getuser/{userId}/v1") | |
| Observable<UserData> getUser(@Path("userId") String userId); | |
| @GET("/zelish/v1/section/home/") | |
| Observable<SectionResponse> getSections(@Query("userId") String userId, @Query("pageNo") int pageNo, @Query("size") int size, @Query("country") String country); | |
| @GET() | |
| Observable<HomeCategoryResponse> getHomeCategory(@Url String url); | |
| @PUT("/zelish/user/modifyuser/{userId}/v1") | |
| Observable<UserResponse> modifyUser(@Path("userId") String userId, @Body UserData data); | |
| @POST("/zelish/pantry/deletePantry/") | |
| Observable<SimpleResponse> deletePantry(@Body JsonObject object); | |
| @POST("/zelish/user/adduser/v1") | |
| Observable<UserResponse> doLogin(@Body UserData request); |
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
| package `in`.zelish.zelish.dish_like | |
| import `in`.zelish.zelish.LandingScreenAcitivity | |
| import `in`.zelish.zelish.R | |
| import `in`.zelish.zelish.db.AppDatabase | |
| import `in`.zelish.zelish.newer_home.adapter.CollectionAdapter | |
| import `in`.zelish.zelish.rest.RestClient | |
| import `in`.zelish.zelish.rest.SimpleResponse | |
| import `in`.zelish.zelish.rest.model.DishData | |
| import `in`.zelish.zelish.utils.* | |
| import android.content.Context | |
| import android.content.Intent | |
| import android.os.AsyncTask | |
| import android.os.Bundle | |
| import android.util.Log | |
| import android.view.View | |
| import androidx.appcompat.app.AppCompatActivity | |
| import androidx.recyclerview.widget.GridLayoutManager | |
| import androidx.recyclerview.widget.RecyclerView | |
| import com.google.gson.JsonObject | |
| import kotlinx.android.synthetic.main.activity_liked_dishes.* | |
| import org.jetbrains.anko.doAsync | |
| import org.jetbrains.anko.uiThread | |
| import retrofit2.Call | |
| import retrofit2.Callback | |
| import retrofit2.Response | |
| import rx.android.schedulers.AndroidSchedulers | |
| import rx.schedulers.Schedulers | |
| import java.lang.ref.WeakReference | |
| class DishLikeActivity : AppCompatActivity() { | |
| val TAG = javaClass.simpleName | |
| lateinit var rv_liked_dishes: RecyclerView | |
| var adapter: CollectionAdapter? = null | |
| var likedList: ArrayList<DishData>? = ArrayList(); | |
| var context: Context? = null | |
| var offset = 0; val limit = 10; | |
| var isLoading = false; | |
| var userId = "" | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_liked_dishes) | |
| context = this@DishLikeActivity | |
| userId = PreferenceHandler.getUserId(this) | |
| AnalyticsProvider.updateAnaylytcsBooleans("My_Fav_Recipes", true); | |
| btn_back.setOnClickListener({ | |
| onBackPressed() | |
| }) | |
| rv_liked_dishes = findViewById(R.id.rv_liked_dishes) | |
| val spaceValue = Helper.dpToPx(8) | |
| rv_liked_dishes.addItemDecoration(SpacesItemDecoration(spaceValue, spaceValue, spaceValue, Helper.dpToPx(10))) | |
| val manager = GridLayoutManager(this, 2) | |
| rv_liked_dishes.setLayoutManager(manager) | |
| adapter = CollectionAdapter(this) | |
| adapter?.setTwoGridView(true) | |
| adapter?.setFinish(true) | |
| adapter?.setFrom("Liked Dishes") | |
| rv_liked_dishes.adapter = adapter | |
| /*rv_liked_dishes.addOnScrollListener(object : RecyclerView.OnScrollListener() { | |
| override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { | |
| super.onScrolled(recyclerView, dx, dy) | |
| val layoutManager = rv_liked_dishes.getLayoutManager() as GridLayoutManager | |
| val pos = layoutManager.findLastCompletelyVisibleItemPosition() | |
| val numItems = adapter!!.itemCount | |
| if (pos >= numItems - 1) { | |
| Log.e("DishLikeActivity", "onScrolled offset=$offset, isLoading=$isLoading") | |
| if (!isLoading) { | |
| offset += likedList!!.size; | |
| DishDetailsActivityV2.fetchDB(this@DishLikeActivity, offset, 10).execute() | |
| isLoading = true; | |
| } | |
| } | |
| } | |
| })*/ | |
| if(intent.hasExtra("addToFav")){ | |
| val dishId = intent.getStringExtra("addToFav") | |
| updateToServer(dishId!!, userId, false) | |
| }else { | |
| getAllLikedRecipes() | |
| } | |
| }//onCreate | |
| override fun onResume() { | |
| super.onResume() | |
| /*DialogShower.showProgressDialog(this) | |
| likedList?.clear() | |
| adapter?.clearSuggetions() | |
| adapter?.notifyDataSetChanged() | |
| offset = 0; | |
| val limit = 10;*/ | |
| /*if(likedList!=null && !likedList!!.isEmpty()){ | |
| limit = likedList!!.size | |
| }*/ | |
| Log.e("DishLikeActivity", "onResume offset=$offset, isLoading=$isLoading") | |
| //DishDetailsActivityV2.fetchDB(this, offset, limit).execute() | |
| //isLoading=true; | |
| } | |
| fun updateToServer(dishId: String, userId: String, isLiked: Boolean) { | |
| DialogShower.showProgressDialog(this) | |
| val `object` = JsonObject() | |
| `object`.addProperty("dishId", dishId) | |
| `object`.addProperty("userId", userId) | |
| `object`.addProperty("liked", isLiked) | |
| RestClient().apiService.likeRecipe(`object`) | |
| .enqueue(object : Callback<SimpleResponse?> { | |
| override fun onResponse(call: Call<SimpleResponse?>, response: Response<SimpleResponse?>) { | |
| Log.e(TAG, "updateToServer response"+ response?.body()?.data) | |
| DialogShower.showToast(this@DishLikeActivity, response?.body()?.data) | |
| getAllLikedRecipes() | |
| } | |
| override fun onFailure(call: Call<SimpleResponse?>, t: Throwable) { | |
| t.printStackTrace() | |
| DialogShower.dismissProgressDialog() | |
| } | |
| }) | |
| } | |
| fun getAllLikedRecipes(){ | |
| DialogShower.showProgressDialog(this) | |
| RestClient().apiService.getAllLikedRecipes(userId) | |
| .subscribeOn(Schedulers.newThread()) | |
| .observeOn(AndroidSchedulers.mainThread()) | |
| .subscribe({ | |
| if(it!=null && it.data!=null) { | |
| Log.e(TAG, "getAllLikedRecipes response") | |
| val allDishes = it.data | |
| if (allDishes!=null && !allDishes.isEmpty()) { | |
| Log.e(TAG, "allDishes=${allDishes?.size}, ${allDishes.toString()}") | |
| doAsync { | |
| var dishDataList = java.util.ArrayList<DishData>() | |
| for (dlt in allDishes) { | |
| val dishData = DishData(dlt) | |
| dishData.isLiked = true | |
| dishDataList.add(dishData) | |
| } | |
| uiThread { | |
| Log.e( | |
| TAG, | |
| "getAllLikedRecipes dishDataList.size=" + dishDataList.size | |
| ) | |
| likedList?.addAll(dishDataList) | |
| showData() | |
| DialogShower.dismissProgressDialog() | |
| } | |
| } | |
| }else{ | |
| showEmpty() | |
| } | |
| }else{ | |
| showEmpty() | |
| } | |
| }) | |
| { | |
| Log.e(TAG, "getAllLikedRecipes() response ERROR") | |
| DialogShower.dismissProgressDialog() | |
| DialogShower.showToast(this, getString(R.string.user_already_exists)) | |
| } | |
| } | |
| companion object { | |
| class fetchDB internal constructor(context: DishLikeActivity, offset: Int, limit: Int) : AsyncTask<Int, String, ArrayList<DishData>>() { | |
| private var resp: String? = null | |
| private val activityReference: WeakReference<DishLikeActivity> = WeakReference(context) | |
| var offset = offset; | |
| var limit = limit; | |
| override fun doInBackground(vararg params: Int?): ArrayList<DishData>? { | |
| Log.e("DishLikeActivity", "doInBackground offset=$offset, limit=$limit") | |
| val allDishes: ArrayList<DishLikeTable> = AppDatabase.getDatabase(activityReference.get()).dishLikeDao().getDishes(offset, limit) as ArrayList<DishLikeTable> | |
| var dishDataList = java.util.ArrayList<DishData>() | |
| if(allDishes!=null && !allDishes.isEmpty()) { | |
| Log.e("DishLikeActivity", "allDishes=${allDishes?.size}, ${allDishes.toString()}") | |
| for (dlt in allDishes) { | |
| val dishData = DishData(dlt) | |
| dishDataList.add(dishData) | |
| } | |
| } | |
| return dishDataList | |
| } | |
| override fun onPostExecute(result: ArrayList<DishData>) { | |
| val activity = activityReference.get() | |
| if (activity == null || activity.isFinishing) return | |
| Log.e("DishLikeActivity", "onPostExecute result=${result?.size}") | |
| activity.likedList?.addAll(result) | |
| activity.showData() | |
| } | |
| } | |
| } | |
| fun showData() { | |
| DialogShower.dismissProgressDialog() | |
| if (likedList == null || likedList!!.isEmpty()) { | |
| showEmpty() | |
| return | |
| }else{ | |
| isLoading = false; | |
| adapter?.updateSuggetions(likedList!!) | |
| adapter?.notifyDataSetChanged() | |
| } | |
| } | |
| fun showEmpty() { | |
| DialogShower.dismissProgressDialog() | |
| tv_empty.visibility = View.VISIBLE | |
| } | |
| override fun onBackPressed() { | |
| if(intent.hasExtra("addToFav")){ | |
| val intent = Intent(this@DishLikeActivity, LandingScreenAcitivity::class.java) | |
| intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK) | |
| startActivity(intent) | |
| }else{ | |
| super.onBackPressed() | |
| } | |
| } | |
| } |
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
| package `in`.zelish.zelish.dish_like | |
| import `in`.zelish.zelish.R | |
| import `in`.zelish.zelish.interf.CookBookAddItemClick | |
| import `in`.zelish.zelish.rest.model.DishData | |
| import `in`.zelish.zelish.utils.PreferenceHandler | |
| import android.app.Activity | |
| import android.util.Log | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import android.widget.ImageView | |
| import android.widget.TextView | |
| import androidx.appcompat.widget.AppCompatImageView | |
| import androidx.cardview.widget.CardView | |
| import androidx.recyclerview.widget.RecyclerView | |
| import com.bumptech.glide.Glide | |
| import kotlinx.android.synthetic.main.cook_book_adapter_item.view.image | |
| import kotlinx.android.synthetic.main.cook_book_adapter_item.view.title | |
| import kotlinx.android.synthetic.main.cook_book_suggetion_item.view.* | |
| class DishLikeAdapter(val context: Activity? = null, val callback: AdapterListener?, val cookBookAddItemClick: CookBookAddItemClick?) : RecyclerView.Adapter<DishLikeAdapter.ViewHolder>() { | |
| val TAG = javaClass.simpleName | |
| var dishList: ArrayList<DishLikeTable> | |
| init { | |
| dishList = ArrayList() | |
| } | |
| interface AdapterListener { | |
| fun onFavClick(item: DishLikeTable) | |
| fun onEmpty() | |
| } | |
| fun addData(arrList: ArrayList<DishLikeTable>) { | |
| this.dishList = arrList | |
| Log.e(TAG, "addData-" + dishList.size) | |
| } | |
| override fun onCreateViewHolder(parent: ViewGroup, pos: Int): ViewHolder { | |
| return ViewHolder( | |
| LayoutInflater.from(parent.context).inflate( | |
| R.layout.cook_book_suggetion_item, | |
| parent, | |
| false | |
| ) | |
| ) | |
| } | |
| class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { | |
| var cardView: CardView = view.cardView | |
| var imageView: ImageView = view.image | |
| var ivAddMeal: ImageView = view.ivAddMeal | |
| var imgLike: AppCompatImageView = view.imgLike | |
| var title: TextView = view.title | |
| } | |
| override fun getItemCount(): Int { | |
| return dishList.size | |
| } | |
| override fun onBindViewHolder(holder: ViewHolder, pos: Int) { | |
| Glide.with(context!!).load(dishList.get(pos).dishImageUrl).override(256).centerCrop().into(holder.imageView) | |
| holder.title.text = dishList.get(pos).dishName | |
| holder.imgLike.setImageResource(R.drawable.heart_liked) | |
| /*if (!dishList.get(pos).foodTypesStr.isNullOrEmpty()) { | |
| val fts = dishList.get(pos).foodTypesStr!!; | |
| if (fts.contains("NON VEGETARIAN", true) || fts.contains("NONVEGETARIAN", true)) { | |
| holder.title.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_non_veg, 0) | |
| } else if (fts.contains("VEGETARIAN", true)) { | |
| holder.title.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_veg, 0) | |
| } else { | |
| holder.title.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_eggitarian, 0) | |
| } | |
| }*/ | |
| holder.imgLike.setOnClickListener { | |
| Log.e(TAG, "imgLike.setOnClickListener") | |
| DishLikeIO.removeDish(context, dishList.get(pos).dishId, PreferenceHandler.getUserId(context)) | |
| dishList.removeAt(pos) | |
| notifyDataSetChanged() | |
| if(dishList.isEmpty() && callback!=null) | |
| callback?.onEmpty() | |
| } | |
| holder.cardView.setOnClickListener { | |
| Log.e(TAG, "imageView.setOnClickListener") | |
| callback?.onFavClick(dishList.get(pos)) | |
| } | |
| holder.ivAddMeal.setOnClickListener({ | |
| cookBookAddItemClick?.onClick(dishList.get(pos).dishId, DishData(dishList.get(pos))) | |
| }) | |
| }//onBind | |
| } |
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
| package `in`.zelish.zelish.dish_like | |
| import `in`.zelish.zelish.rest.ApiService | |
| import `in`.zelish.zelish.rest.Resource | |
| import `in`.zelish.zelish.rest.RestClient | |
| import android.app.Application | |
| import android.content.Context | |
| import android.util.Log | |
| import androidx.lifecycle.MutableLiveData | |
| import rx.android.schedulers.AndroidSchedulers | |
| import rx.schedulers.Schedulers | |
| class DishLikeRepo private constructor(context: Context) { | |
| private val mApiService: ApiService | |
| private val dishData: MutableLiveData<Resource<*>> | |
| fun getLikedDishes(userId: String?): MutableLiveData<Resource<*>> { | |
| dishData.value = Resource.loading("") | |
| mApiService.getAllLikedRecipes(userId) | |
| .subscribeOn(Schedulers.newThread()) | |
| .observeOn(AndroidSchedulers.mainThread()) | |
| .subscribe( | |
| { | |
| response -> dishData.value = Resource.success(response) | |
| }, | |
| { throwable: Throwable? -> | |
| Log.d("ProfileRepo", "getUserDetails ERROR=$throwable") | |
| throwable?.printStackTrace() | |
| dishData.setValue(Resource.error("", throwable)) | |
| } | |
| ) | |
| return dishData | |
| } | |
| companion object { | |
| private var ourInstance: DishLikeRepo? = null | |
| fun getInstance(context: Application): DishLikeRepo? { | |
| if (ourInstance == null) { | |
| ourInstance = DishLikeRepo(context) | |
| } | |
| return ourInstance | |
| } | |
| } | |
| init { | |
| val mRestClient = RestClient() | |
| mApiService = mRestClient.apiService | |
| dishData = MutableLiveData() | |
| } | |
| } |
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
| package `in`.zelish.zelish.dish_like | |
| import `in`.zelish.zelish.rest.Resource | |
| import android.app.Application | |
| import androidx.lifecycle.AndroidViewModel | |
| import androidx.lifecycle.MutableLiveData | |
| class DishLikeViewModel(application: Application) : AndroidViewModel(application) { | |
| private val repo: DishLikeRepo? | |
| fun getLikedDishes(userId: String?): MutableLiveData<Resource<*>> { | |
| return repo!!.getLikedDishes(userId) | |
| } | |
| init { | |
| repo = DishLikeRepo.getInstance(application) | |
| } | |
| } |
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
| https://fabiomsr.github.io/from-java-to-kotlin/index.html | |
| https://quickref.me/kotlin.html | |
| https://proandroiddev.com/the-kotlin-guide-for-the-busy-java-developer-93dde84a77b7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment