Skip to content

Commit

Permalink
Feat: 상품 수정 및 삭제 기능 구현 #321 (#373)
Browse files Browse the repository at this point in the history
* Feat: 상품 삭제 기능 구현 #321

* Feat: 상품 수정 기능 구현 #321
  • Loading branch information
hxeyexn authored Sep 23, 2023
1 parent 8e317ed commit af1f89e
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,18 @@ class ProductDetailPreviewFragment : Fragment() {
storageRef.child(productThumbnailImageFileName).putFile(product.thumbnail_image.toUri())
.addOnSuccessListener {
product.thumbnail_image = productThumbnailImageFileName
}

val productImageFileNameList = arrayListOf<String>()
product.image.take(product.image.size).forEach {
val productImageFileName = "productImage/$productId/${getMilliSec()}.jpg"
productImageFileNameList.add(productImageFileName)
storageRef.child(productImageFileName).putFile(it.toUri()).addOnSuccessListener {
uploadProductFloorPlans(productId)
val productImageFileNameList = arrayListOf<String>()
product.image.take(product.image.size).forEach {
val productImageFileName = "productImage/$productId/${getMilliSec()}.jpg"
productImageFileNameList.add(productImageFileName)
storageRef.child(productImageFileName).putFile(it.toUri())
.addOnSuccessListener {
uploadProductFloorPlans(productId)
}
}
product.image = productImageFileNameList
}
}
product.image = productImageFileNameList
}

private fun uploadProductFloorPlans(productId: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,36 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide
import com.google.android.material.snackbar.Snackbar
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import com.google.firebase.storage.ktx.storage
import likelion.project.agijagi.MainActivity.Companion.getMilliSec
import likelion.project.agijagi.R
import likelion.project.agijagi.databinding.FragmentProductDetailPreviewBinding
import likelion.project.agijagi.model.ProductModel
import likelion.project.agijagi.model.SellerModel
import likelion.project.agijagi.model.UserModel
import java.text.DecimalFormat

class ProductUpdateDetailPreviewFragment : Fragment() {

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

lateinit var product: ProductModel
val dec = DecimalFormat("#,###")

val db = Firebase.firestore
private val storageRef = Firebase.storage.reference

private lateinit var newThumbnailImage: String
var newImageList = mutableListOf<String>()

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand All @@ -27,10 +46,169 @@ class ProductUpdateDetailPreviewFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

getProductDataFromArguments()
initViews()
setToolbarItemAction()
setProductUpdateButton()
}

private fun getProductDataFromArguments() {
val bundle = arguments

if (bundle != null) {
product = bundle.getParcelable("productData")!!
}
}

private fun getProductId(): String {
return arguments?.getString("prodId").toString()
}

private fun initViews() {
binding.run {
if (product.thumbnail_image.startsWith("productImage/")) {
storageRef.child(product.thumbnail_image).downloadUrl.addOnSuccessListener {
Glide.with(this@ProductUpdateDetailPreviewFragment)
.load(it)
.placeholder(R.drawable.product_detail_default_image)
.into(imageviewProductDetailPreviewThumbnailImage)
newThumbnailImage = it.toString()
}
} else if (product.thumbnail_image == "") {
if (product.image[0].startsWith("productImage/")) {
storageRef.child(product.image[0]).downloadUrl.addOnSuccessListener {
Glide.with(this@ProductUpdateDetailPreviewFragment)
.load(it)
.placeholder(R.drawable.product_detail_default_image)
.into(imageviewProductDetailPreviewThumbnailImage)
newThumbnailImage = it.toString()
}
} else {
Glide.with(this@ProductUpdateDetailPreviewFragment)
.load(product.image[0])
.placeholder(R.drawable.product_detail_default_image)
.into(imageviewProductDetailPreviewThumbnailImage)
newThumbnailImage = product.image[0]
}
} else {
Glide.with(this@ProductUpdateDetailPreviewFragment)
.load(product.thumbnail_image)
.placeholder(R.drawable.product_detail_default_image)
.into(imageviewProductDetailPreviewThumbnailImage)
newThumbnailImage = product.thumbnail_image
}

val imageViews = listOf(
imageviewProductPreviewDetailImage1,
imageviewProductPreviewDetailImage2,
imageviewProductPreviewDetailImage3,
imageviewProductPreviewDetailImage4,
imageviewProductPreviewDetailImage5,
imageviewProductPreviewDetailImage6
)

product.image.take(imageViews.size).forEachIndexed { index, imageUrl ->
if (product.image[index].startsWith("productImage/")) {
storageRef.child(product.image[index]).downloadUrl.addOnSuccessListener {
Glide.with(this@ProductUpdateDetailPreviewFragment)
.load(it)
.placeholder(R.drawable.product_detail_default_image)
.into(imageViews[index])
newImageList.add(it.toString())
}
} else {
Glide.with(this@ProductUpdateDetailPreviewFragment)
.load(imageUrl)
.placeholder(R.drawable.product_detail_default_image)
.into(imageViews[index])
newImageList.add(imageUrl)
}
}

imageViews.subList(product.image.size, imageViews.size).forEach { imageView ->
imageView.visibility = View.GONE
}

textviewProductDetailPreviewBrand.text = SellerModel.businessName
textviewProductDetailPreviewName.text = product.name
"${dec.format(product.price.toLong())}".also {
textviewProductDetailPreviewPrice.text = it
}
textviewProductDetailPreviewInfoTitle.text = product.name
textviewProductDetailPreviewInfoDescription.text = product.detail
}
}

private fun uploadProductImages(productId: String) {
val productThumbnailImageFileName =
"productImage/$productId/thumbnail_${getMilliSec()}.jpg"
storageRef.child(productThumbnailImageFileName).putFile(newThumbnailImage.toUri())
.addOnSuccessListener {
product.thumbnail_image = productThumbnailImageFileName

val productImageFileNameList = arrayListOf<String>()
newImageList.take(newImageList.size).forEach {
val productImageFileName = "productImage/$productId/${getMilliSec()}.jpg"
productImageFileNameList.add(productImageFileName)
storageRef.child(productImageFileName).putFile(it.toUri())
.addOnSuccessListener {
uploadProductFloorPlans(productId)
}
}
product.image = productImageFileNameList
}
}

private fun uploadProductFloorPlans(productId: String) {
if (!product.isCustom) {
registerProductData(productId)
} else {
val productFloorPlanFileNameList = arrayListOf<String>()
product.floorPlan.take(product.floorPlan.size).forEach {
val productFloorPlanFileName = "productFloorPlan/$productId/${getMilliSec()}.jpg"
productFloorPlanFileNameList.add(productFloorPlanFileName)
storageRef.child(productFloorPlanFileName).putFile(it.toUri())
.addOnSuccessListener {
registerProductData(productId)
}
}
product.floorPlan = productFloorPlanFileNameList
}
}

private fun registerProductData(productId: String) {
product.state = "판매"
val customOptionInfo = hashMapOf(
"image_fee" to product.customOptionInfo["image_fee"],
"image_is_use" to product.customOptionInfo["image_is_use"],
"lettering_fee" to product.customOptionInfo["lettering_fee"],
"lettering_is_use" to product.customOptionInfo["lettering_is_use"]
)

val productMap = hashMapOf<String, Any>(
"brand" to SellerModel.businessName,
"category" to product.category,
"customOptionInfo" to customOptionInfo,
"detail" to product.detail,
"floor_plan" to product.floorPlan,
"image" to product.image,
"is_custom" to product.isCustom,
"is_delete" to product.isDelete,
"name" to product.name,
"state" to product.state,
"price" to product.price,
"sales_quantity" to product.salesQuantity,
"seller_id" to UserModel.roleId,
"shopping_quantity" to product.shoppingQuantity,
"thumbnail_image" to product.thumbnail_image,
"update_date" to getMilliSec()
)
db.collection("product").document(productId).set(productMap).addOnSuccessListener {
Snackbar.make(requireView(), "상품 수정이 완료되었습니다.", Snackbar.LENGTH_SHORT).show()
findNavController().navigate(R.id.action_productUpdateDetailPreviewFragment_to_productListFragment)
}
}

private fun setToolbarItemAction() {
binding.toolbarProductDetailPreview.setNavigationOnClickListener {
findNavController().popBackStack()
Expand All @@ -41,8 +219,7 @@ class ProductUpdateDetailPreviewFragment : Fragment() {
binding.buttonProductDetailPreviewProductRegistration.run {
text = "수정"
setOnClickListener {
Snackbar.make(it, "상품 수정이 완료되었습니다.", Snackbar.LENGTH_SHORT).show()
findNavController().navigate(R.id.action_productUpdateDetailPreviewFragment_to_productListFragment)
uploadProductImages(getProductId())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ class ProductUpdateFragment : Fragment() {
editinputProductUpdateProductname.hint = data.name
}
// 가격
if (data.name != "") {
editinputlayoutProductUpdateProductprice.hint = data.name
if (data.price != "") {
editinputlayoutProductUpdateProductprice.hint = data.price
}
// 카테고리 선택
if (data.category != "") {
Expand Down Expand Up @@ -662,7 +662,10 @@ class ProductUpdateFragment : Fragment() {


// 번들 생성, 전달
val bundle = bundleOf("productData" to dataOrigin)
val bundle = Bundle()
bundle.putParcelable("productData", dataOrigin)
bundle.putString("prodId", getProductId())

findNavController().navigate(
R.id.action_productUpdateFragment_to_productUpdateDetailPreviewFragment,
bundle
Expand Down Expand Up @@ -691,6 +694,10 @@ class ProductUpdateFragment : Fragment() {
return data!!
}

private fun getProductId(): String {
return arguments?.getString("prodId").toString()
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package likelion.project.agijagi.sellermypage

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -151,7 +150,9 @@ class SellerProductDetailFragment : Fragment() {
binding.run {
textviewSellerProductDetailBrand.text = brand
textviewSellerProductDetailName.text = name
textviewSellerProductDetailPrice.text = price
"${dec.format(price.toLong())}".also {
textviewSellerProductDetailPrice.text = it
}
textviewSellerProductDetailInfoTitle.text = title
textviewSellerProductDetailInfoDescription.text = detail
}
Expand Down Expand Up @@ -190,14 +191,23 @@ class SellerProductDetailFragment : Fragment() {
R.id.menu_product_detail_edit -> {
val bundle = Bundle()
bundle.putParcelable("productData", product)
bundle.putString("prodId", getProductId())
findNavController().navigate(
R.id.action_sellerProductDetailFragment_to_productUpdateFragment,
bundle
)
}

R.id.menu_product_detail_delete -> {

val isDeleteState = hashMapOf<String, Any>(
"is_delete" to true
)
db.collection("product")
.document(getProductId())
.update(isDeleteState)
.addOnSuccessListener {
findNavController().popBackStack()
}
}
}
false
Expand Down

0 comments on commit af1f89e

Please sign in to comment.