Skip to content

Commit

Permalink
Merge 00b8bb3 into b46c3be
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammedesadcomert authored Sep 11, 2022
2 parents b46c3be + 00b8bb3 commit 1d2f3ba
Show file tree
Hide file tree
Showing 18 changed files with 354 additions and 67 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ android {

defaultConfig {
applicationId "com.muhammedesadcomert.shopping"
minSdk 23
minSdk 24
targetSdk 32
versionCode 1
versionName "1.2.0"
versionName "1.3.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ object Constant {
const val ALIAS_KEY = "AtS1aPFxlIdVLth6ee2SEETlRxk="
const val DEFAULT_CATEGORY = "61b1f109a82ec0dd1c56f5ed" // Economic Menu
const val DEFAULT_SORT = "Price"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.muhammedesadcomert.shopping.data.remote

import com.muhammedesadcomert.shopping.ui.home.model.CategoryApiModel
import com.muhammedesadcomert.shopping.ui.home.model.ProductApiModel
import com.muhammedesadcomert.shopping.ui.home.model.ProductsApiModel
import com.muhammedesadcomert.shopping.ui.home.productdetail.model.ProductApiModel
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

interface ApiService {
Expand All @@ -13,6 +15,11 @@ interface ApiService {
@GET("products/advanced-filtered")
suspend fun getProducts(
@Query("categoryId") categoryId: String,
@Query("sort") sort: String,
@Query("sort") sort: String
): Response<ProductsApiModel>

@GET("products/{productId}")
suspend fun getSingleProductDetail(
@Path("productId") productId: String
): Response<ProductApiModel>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import com.muhammedesadcomert.shopping.common.base.BaseRepository
import com.muhammedesadcomert.shopping.common.util.Resource
import com.muhammedesadcomert.shopping.data.remote.ApiService
import com.muhammedesadcomert.shopping.ui.home.model.CategoryApiModel
import com.muhammedesadcomert.shopping.ui.home.model.ProductApiModel
import com.muhammedesadcomert.shopping.ui.home.model.ProductsApiModel
import com.muhammedesadcomert.shopping.ui.home.productdetail.model.ProductApiModel
import javax.inject.Inject

class ProductRepository @Inject constructor(private val apiService: ApiService) : BaseRepository() {
suspend fun getCategories(): Resource<CategoryApiModel> =
safeApiCall { apiService.getCategories() }

suspend fun getProducts(categoryId: String, sort: String): Resource<ProductApiModel> =
suspend fun getProducts(categoryId: String, sort: String): Resource<ProductsApiModel> =
safeApiCall { apiService.getProducts(categoryId, sort) }
}

suspend fun getSingleProductDetail(productId: String): Resource<ProductApiModel> =
safeApiCall { apiService.getSingleProductDetail(productId) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ class CategoryAdapter(private val onItemClicked: (Category) -> Unit) :
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class HomeFragment : Fragment() {
private lateinit var productAdapter: ProductAdapter

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentHomeBinding.inflate(inflater, container, false)
return binding.root
Expand Down Expand Up @@ -78,4 +79,4 @@ class HomeFragment : Fragment() {
super.onDestroyView()
_binding = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.muhammedesadcomert.shopping.common.util.Resource
import com.muhammedesadcomert.shopping.data.repository.ProductRepository
import com.muhammedesadcomert.shopping.ui.home.model.Category
import com.muhammedesadcomert.shopping.ui.home.model.Product
import com.muhammedesadcomert.shopping.ui.home.productdetail.model.Data
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -24,6 +25,9 @@ class HomeViewModel @Inject constructor(private val productRepository: ProductRe
private var _products: MutableLiveData<List<Product>> = MutableLiveData()
val products: LiveData<List<Product>> get() = _products

private var _productDetail: MutableLiveData<Data> = MutableLiveData()
val productDetail: LiveData<Data> get() = _productDetail

init {
getCategories()
getProducts()
Expand Down Expand Up @@ -60,4 +64,20 @@ class HomeViewModel @Inject constructor(private val productRepository: ProductRe
}
}
}
}

fun getSingleProductDetail(productId: String) {
viewModelScope.launch {
when (val resource = productRepository.getSingleProductDetail(productId)) {
is Resource.Success -> {
resource.data?.product?.let {
_productDetail.postValue(it)
}
}
is Resource.Failure -> {
Log.e("Failure", resource.errorMessage)
}
else -> {}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ class ProductAdapter(private val onItemClicked: (Product) -> Unit) :
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ data class CategoryApiModel(
@SerializedName("success")
val success: Boolean,
@SerializedName("data")
val categories: List<Category> = arrayListOf(),
val categories: List<Category> = arrayListOf()
)

data class Category(
Expand All @@ -27,5 +27,5 @@ data class Category(
@SerializedName("isActive")
val isActive: Boolean? = null,
@SerializedName("subCategories")
val subCategories: ArrayList<String> = arrayListOf(),
)
val subCategories: ArrayList<String> = arrayListOf()
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.muhammedesadcomert.shopping.ui.home.model

import com.google.gson.annotations.SerializedName

data class ProductApiModel(
data class ProductsApiModel(
@SerializedName("success")
val success: Boolean? = null,
@SerializedName("data")
val products: List<Product> = arrayListOf(),
@SerializedName("meta")
val meta: Meta? = null,
val meta: Meta? = null
)

data class Product(
Expand Down Expand Up @@ -69,7 +69,7 @@ data class Product(
@SerializedName("useFixPrice")
val useFixPrice: Boolean? = null,
@SerializedName("variantData")
val variantData: List<String> = arrayListOf(),
val variantData: List<String> = arrayListOf()
)

data class ProductCategory(
Expand All @@ -90,7 +90,7 @@ data class ProductCategory(
@SerializedName("isActive")
val isActive: Boolean? = null,
@SerializedName("subCategories")
val subCategories: List<String> = arrayListOf(),
val subCategories: List<String> = arrayListOf()
)

data class Brand(
Expand All @@ -105,33 +105,33 @@ data class Brand(
@SerializedName("name")
val name: String? = null,
@SerializedName("updateDate")
val updateDate: String? = null,
val updateDate: String? = null
)

data class FeaturedImage(
@SerializedName("t")
val t: String? = null,
@SerializedName("n")
val n: String? = null,
val n: String? = null
)

data class Images(
@SerializedName("t")
val t: String? = null,
@SerializedName("n")
val n: String? = null,
val n: String? = null
)

data class Icon(
@SerializedName("t")
val t: String? = null,
@SerializedName("n")
val n: String? = null,
val n: String? = null
)

data class Meta(
@SerializedName("queryCount")
val queryCount: Int? = null,
@SerializedName("itemsCount")
val itemsCount: Int? = null,
)
val itemsCount: Int? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.muhammedesadcomert.shopping.ui.home.productdetail

import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.bumptech.glide.Glide
import com.muhammedesadcomert.shopping.R
import com.muhammedesadcomert.shopping.databinding.FragmentProductDetailBinding
import com.muhammedesadcomert.shopping.ui.home.HomeViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ProductDetailFragment : Fragment() {

private var _binding: FragmentProductDetailBinding? = null
private val binding get() = _binding!!

private val viewModel: HomeViewModel by viewModels()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentProductDetailBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupDetails()
}

private fun setupDetails() {
viewModel.getSingleProductDetail(requireArguments().get("productId") as String)
viewModel.productDetail.observe(viewLifecycleOwner) { product ->
with(binding) {
textViewProductTitle.text = product.title
textViewProductPrice.text =
(product.price.toString()).plus(getString(R.string.price_postfix))

if (product.campaignPrice != null && product.campaignPrice != product.price) {
textViewProductPrice.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
textViewProductPrice.setTextColor(Color.GRAY)
textViewProductPrice.textSize = 16f
textViewProductCampaignPrice.text =
(product.campaignPrice.toString()).plus(getString(R.string.price_postfix))
textViewProductCampaignPrice.visibility = View.VISIBLE
}

if (product.stock == 0) {
imageViewSoldOut.visibility = View.VISIBLE
}

textViewProductDescription.text =
Html.fromHtml(product.description, Html.FROM_HTML_MODE_LEGACY)
Glide.with(requireContext()).load(product.featuredImage!!.n)
.placeholder(R.drawable.blank_screen)
.into(imageViewProductImage)
}
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Loading

0 comments on commit 1d2f3ba

Please sign in to comment.