Skip to content

Commit

Permalink
[feat/#60] 리스너 사용 방법 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
unam98 committed Jan 20, 2024
1 parent 16a9055 commit c4d3352
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,35 @@ class FrontCardView : CardView {
private val layoutParent = ConstraintLayout.LayoutParams.PARENT_ID
private var frontCard = FrontCard()

val tvName: TextView by lazy { findViewById(R.id.tvName) }
val tvCompany: TextView by lazy { findViewById(R.id.tvCompany) }
val tvJob: TextView by lazy { findViewById(R.id.tvJob) }
val tvLevel: TextView by lazy { findViewById(R.id.tvLevel) }
val tvArea: TextView by lazy { findViewById(R.id.tvArea) }
val tvMbti: TextView by lazy { findViewById(R.id.tvMbti) }
val ivCharacter: ImageView by lazy { findViewById(R.id.ivCharacter) }
val ivFloat: ImageView by lazy { findViewById(R.id.ivFloat) }

val ivEditName: ImageView by lazy { findViewById(R.id.ivEditName) }
val ivEditCompany: ImageView by lazy { findViewById(R.id.ivEditCompany) }
val ivEditJob: ImageView by lazy { findViewById(R.id.ivEditJob) }
val ivEditArea: ImageView by lazy { findViewById(R.id.ivEditArea) }

var isModify: Boolean = false
set(value) {
field = value
ivFloat.visibility = if (value) View.VISIBLE else View.INVISIBLE
}

var isModifyDetail: Boolean = false
set(value) {
field = value
ivEditName.visibility = if (value) View.VISIBLE else View.INVISIBLE
ivEditCompany.visibility = if (value) View.VISIBLE else View.INVISIBLE
ivEditJob.visibility = if (value) View.VISIBLE else View.INVISIBLE
ivEditArea.visibility = if (value) View.VISIBLE else View.INVISIBLE
}

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
init(context, attrs)
}
Expand All @@ -31,6 +60,10 @@ class FrontCardView : CardView {
init(context, null)
}

override fun setOnClickListener(listener: OnClickListener?) {
super.setOnClickListener(listener)
}

/**
* 컴포즈 환경에서 FrontCard 객체 생성
*/
Expand Down Expand Up @@ -79,9 +112,14 @@ class FrontCardView : CardView {
level = getString(com.teumteum.base.R.styleable.CardFrontView_level) ?: ""
area = getString(com.teumteum.base.R.styleable.CardFrontView_area) ?: ""
mbti = getString(com.teumteum.base.R.styleable.CardFrontView_mbti) ?: ""
characterResId = getResourceId(com.teumteum.base.R.styleable.CardFrontView_characterImage, 0)
isModify = getBoolean(com.teumteum.base.R.styleable.CardFrontView_isModify, false)
isModifyDetail = getBoolean(com.teumteum.base.R.styleable.CardFrontView_isModifyDetail, false)
characterResId =
getResourceId(com.teumteum.base.R.styleable.CardFrontView_characterImage, 0)
isModify =
getBoolean(com.teumteum.base.R.styleable.CardFrontView_isModify, false)
isModifyDetail = getBoolean(
com.teumteum.base.R.styleable.CardFrontView_isModifyDetail,
false
)
}
} finally {
recycle()
Expand All @@ -91,48 +129,32 @@ class FrontCardView : CardView {
}

private fun setUpViews() {
setTextView(tvName, frontCard.name)
setTextView(tvCompany, frontCard.company)
setTextView(tvJob, frontCard.job)
setTextView(tvLevel, frontCard.level)
setTextView(tvArea, frontCard.area)
setTextView(tvMbti, frontCard.mbti)

val ivFloatVisibility = if (frontCard.isModify == true) View.VISIBLE else View.INVISIBLE
val ivEditVisibility = if (frontCard.isModifyDetail == true) View.VISIBLE else View.INVISIBLE

// 이미지 뷰 프로퍼티 설정을 위한 공통 함수 호출
setImageViewProperties(ivFloat, ivFloatVisibility, frontCard.floatResId)
setImageViewProperties(ivEditName, ivEditVisibility, frontCard.editNameResId)
setImageViewProperties(ivEditCompany, ivEditVisibility, frontCard.editCompanyResId)
setImageViewProperties(ivEditJob, ivEditVisibility, frontCard.editJobResId)
setImageViewProperties(ivEditArea, ivEditVisibility, frontCard.editAreaResId)
}
tvName.text = frontCard.name
tvCompany.text = frontCard.company
tvJob.text = frontCard.job
tvLevel.text = frontCard.level
tvArea.text = frontCard.area
tvMbti.text = frontCard.mbti
ivCharacter.setImageResource(frontCard.characterResId)

private fun setImageViewProperties(viewId: Int, visibility: Int, resId: Int?) {
val imageView = findViewById<ImageView>(viewId)
imageView?.let {
it.visibility = visibility
resId?.let { imageResId -> it.setImageResource(imageResId) }
}
}

private fun setTextView(viewId: Int, text: String) {
val textView = findViewById<TextView>(viewId)
textView?.text = text
ivFloat.setImageResource(frontCard.floatResId)
ivEditName.setImageResource(frontCard.editNameResId)
ivEditCompany.setImageResource(frontCard.editCompanyResId)
ivEditJob.setImageResource(frontCard.editJobResId)
ivEditArea.setImageResource(frontCard.editAreaResId)
}

/**
* layout과 그 안에 포함시킬 뷰 추가
*/
private fun createLayoutAndViews(context: Context) = ConstraintLayout(context).apply {
id = clCardFront
id = R.id.cl
layoutParams =
ConstraintLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)

addTextView(
context,
id = tvName,
id = R.id.tvName,
text = context.getString(R.string.front_card_name),
textColor = com.teumteum.base.R.color.text_headline_primary,
textSizeSp = 30f,
Expand All @@ -145,31 +167,31 @@ class FrontCardView : CardView {
)
addTextView(
context,
id = tvCompany,
id = R.id.tvCompany,
text = context.getString(R.string.front_card_company),
textColor = com.teumteum.base.R.color.text_body_teritary,
textSizeSp = 16f,
fontFamily = com.teumteum.base.R.font.pretendard_semibold,
lineHeightDp = 22,
startToStartOf = tvName,
startToStartOf = R.id.tvName,
marginTop = 20,
topToBottomOf = tvName
topToBottomOf = R.id.tvName
)
addTextView(
context,
id = tvJob,
id = R.id.tvJob,
text = context.getString(R.string.front_card_job),
textColor = com.teumteum.base.R.color.text_headline_primary,
textSizeSp = 18f,
fontFamily = com.teumteum.base.R.font.pretendard_bold,
lineHeightDp = 24,
startToStartOf = tvName,
startToStartOf = R.id.tvName,
marginTop = 6,
topToBottomOf = tvCompany
topToBottomOf = R.id.tvCompany
)
addTextView(
context,
id = tvLevel,
id = R.id.tvLevel,
text = context.getString(R.string.front_card_level),
textColor = com.teumteum.base.R.color.text_body_secondary,
textSizeSp = 12f,
Expand All @@ -180,44 +202,44 @@ class FrontCardView : CardView {
paddingEnd = 8,
paddingTop = 2,
paddingBottom = 2,
startToStartOf = tvName,
topToBottomOf = tvJob,
startToStartOf = R.id.tvName,
topToBottomOf = R.id.tvJob,
background = R.drawable.shape_rect4_elevation_level02
)
addTextView(
context,
id = tvArea,
id = R.id.tvArea,
text = context.getString(R.string.front_card_area),
textColor = com.teumteum.base.R.color.text_body_teritary,
textSizeSp = 12f,
fontFamily = com.teumteum.base.R.font.pretendard_regular,
lineHeightDp = 18,
marginTop = 6,
startToStartOf = tvName,
bottomToTopOf = tvMbti
startToStartOf = R.id.tvName,
bottomToTopOf = R.id.tvMbti
)
addTextView(
context,
id = tvMbti,
id = R.id.tvMbti,
text = context.getString(R.string.front_card_mbti),
textColor = com.teumteum.base.R.color.text_headline_primary,
textSizeSp = 20f,
fontFamily = com.teumteum.base.R.font.pretendard_bold,
lineHeightDp = 28,
marginBottom = 32,
startToStartOf = tvName,
startToStartOf = R.id.tvName,
bottomToBottomOf = layoutParent
)
addImageView(
context,
id = ivCharacter,
id = R.id.ivCharacter,
drawableRes = R.drawable.ic_card_penguin,
bottomToBottomOf = layoutParent,
endToEndOf = layoutParent
)
addImageView(
context,
id = ivFloat,
id = R.id.ivFloat,
drawableRes = R.drawable.ic_card_float,
bottomToBottomOf = layoutParent,
endToEndOf = layoutParent,
Expand All @@ -226,38 +248,38 @@ class FrontCardView : CardView {
)
addImageView( //todo - 하나의 id값으로 일괄 관리 가능 여부 확인
context,
id = ivEditName,
id = R.id.ivEditName,
drawableRes = R.drawable.ic_card_edit,
startToEndOf = tvName,
topToTopOf = tvName,
bottomToBottomOf = tvName,
startToEndOf = R.id.tvName,
topToTopOf = R.id.tvName,
bottomToBottomOf = R.id.tvName,
marginStart = 4
)
addImageView(
context,
id = ivEditCompany,
id = R.id.ivEditCompany,
drawableRes = R.drawable.ic_card_edit,
startToEndOf = tvCompany,
topToTopOf = tvCompany,
bottomToBottomOf = tvCompany,
startToEndOf = R.id.tvCompany,
topToTopOf = R.id.tvCompany,
bottomToBottomOf = R.id.tvCompany,
marginStart = 4
)
addImageView(
context,
id = ivEditJob,
id = R.id.ivEditJob,
drawableRes = R.drawable.ic_card_edit,
startToEndOf = tvJob,
topToTopOf = tvJob,
bottomToBottomOf = tvJob,
startToEndOf = R.id.tvJob,
topToTopOf = R.id.tvJob,
bottomToBottomOf = R.id.tvJob,
marginStart = 4
)
addImageView(
context,
id = ivEditArea,
id = R.id.ivEditArea,
drawableRes = R.drawable.ic_card_edit,
startToEndOf = tvArea,
topToTopOf = tvArea,
bottomToBottomOf = tvArea,
startToEndOf = R.id.tvArea,
topToTopOf = R.id.tvArea,
bottomToBottomOf = R.id.tvArea,
marginStart = 4
)
}
Expand Down Expand Up @@ -373,27 +395,4 @@ class FrontCardView : CardView {
}
addView(imageView)
}

/**
* 추가한 View들에 대한 터치 리스너
*/
fun setClickListener(viewId: Int, listener: View.OnClickListener) {
findViewById<View>(viewId)?.setOnClickListener(listener)
}

companion object {
const val clCardFront = 0
const val tvName = 1
const val tvCompany = 2
const val tvJob = 3
const val tvLevel = 4
const val tvArea = 5
const val tvMbti = 6
const val ivCharacter = 7
const val ivFloat = 8
const val ivEditName = 9
const val ivEditCompany = 10
const val ivEditJob = 11
const val ivEditArea = 12
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ data class FrontCard(
var area: String = "선택 지역에 사는",
var mbti: String = "MBTI",
var characterResId: Int = R.drawable.ic_card_penguin,
var floatResId: Int? = R.drawable.ic_card_float,
var editNameResId: Int? = R.drawable.ic_card_edit,
var editCompanyResId: Int? = R.drawable.ic_card_edit,
var editJobResId: Int? = R.drawable.ic_card_edit,
var editAreaResId: Int? = R.drawable.ic_card_edit,
var isModify: Boolean? = false,
var isModifyDetail: Boolean? = false
var floatResId: Int = R.drawable.ic_card_float,
var editNameResId: Int = R.drawable.ic_card_edit,
var editCompanyResId: Int = R.drawable.ic_card_edit,
var editJobResId: Int = R.drawable.ic_card_edit,
var editAreaResId: Int = R.drawable.ic_card_edit,
)
16 changes: 16 additions & 0 deletions app/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="cl" type="id"/>
<item name="tvName" type="id"/>
<item name="tvCompany" type="id"/>
<item name="tvJob" type="id"/>
<item name="tvLevel" type="id"/>
<item name="tvArea" type="id"/>
<item name="tvMbti" type="id"/>
<item name="ivCharacter" type="id"/>
<item name="ivFloat" type="id"/>
<item name="ivEditName" type="id"/>
<item name="ivEditCompany" type="id"/>
<item name="ivEditJob" type="id"/>
<item name="ivEditArea" type="id"/>
</resources>

0 comments on commit c4d3352

Please sign in to comment.