🍥구현 기능
화면 상단에 회원 프로필 이미지, 이름, 아이디, 자기소개, 키워드 표시
자기소개 1줄 이상인 경우, 더보기 클릭을 통해 보기
회원정보 수정 클릭 시, 회원정보 화면으로 이동
- 회원이 최근에 작성한 게시글 5개 목록 표시
- 회원이 최근에 좋아요한 게시글 5개 목록 표시
- 게시글 클릭 시, 해당 게시글의 디테일 화면으로 이동
🍥구현하기
MyPage.kt
class MyPage : AppCompatActivity() {
private val back: ImageButton by lazy { findViewById(R.id.btn_back) }
private val setting: Button by lazy { findViewById(R.id.btn_edit) }
private val profilePhoto: ImageView by lazy { findViewById(R.id.img_profile) }
private val name: TextView by lazy { findViewById(R.id.tv_name) }
private val id: TextView by lazy { findViewById(R.id.tv_id) }
private val info: TextView by lazy { findViewById(R.id.tv_myinfo) }
private val more: TextView by lazy { findViewById(R.id.view_more) }
private val myPostScrollView: HorizontalScrollView by lazy { findViewById(R.id.scrollview_my_post) }
private val noWrittenPostTextView: TextView by lazy { findViewById(R.id.tv_no_my_post) }
private val likedPostScrollView: HorizontalScrollView by lazy { findViewById(R.id.scrollview_my_like) }
private val noLikedPostTextView: TextView by lazy { findViewById(R.id.tv_no_liked_post) }
private val character1: TextView by lazy { findViewById(R.id.tv_character1) }
private val character2: TextView by lazy { findViewById(R.id.tv_character2) }
private val character3: TextView by lazy { findViewById(R.id.tv_character3) }
private val userManager = UserManager.newInstance()
private val postManager = PostManager.newInstance()
private val userID by lazy {
intent.getStringExtra(EXTRA_ID) ?: "hong_gildong"
}
private val myPostItemList by lazy {
listOf<ViewGroup>(
findViewById(R.id.mypage_mypost1),
findViewById(R.id.mypage_mypost2),
findViewById(R.id.mypage_mypost3),
findViewById(R.id.mypage_mypost4),
findViewById(R.id.mypage_mypost5)
)
}
private val likedPostItemList by lazy {
listOf<ViewGroup>(
findViewById(R.id.mypage_likepost1),
findViewById(R.id.mypage_likepost2),
findViewById(R.id.mypage_likepost3),
findViewById(R.id.mypage_likepost4),
findViewById(R.id.mypage_likepost5)
)
}
private val myPostItemIDMap by lazy {
mutableMapOf<Int, Int>(
R.id.mypage_mypost1 to -1,
R.id.mypage_mypost2 to -1,
R.id.mypage_mypost3 to -1,
R.id.mypage_mypost4 to -1,
R.id.mypage_mypost5 to -1
)
}
private val likedPostItemIDMap by lazy {
mutableMapOf<Int, Int>(
R.id.mypage_likepost1 to -1,
R.id.mypage_likepost2 to -1,
R.id.mypage_likepost3 to -1,
R.id.mypage_likepost4 to -1,
R.id.mypage_likepost5 to -1
)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my_page)
setViewMore(info, more)
initProfile()
initMyPostScrollView()
initLikedPostScrollView()
initPostItem()
goToMain()
goToUserInfo()
}
override fun onResume() {
super.onResume()
initProfile()
initLikedPostScrollView()
}
private fun setViewMore(contentTextView: TextView, viewMoreTextView: TextView) {
}
private fun initProfile() {
}
private fun initMyPostScrollView() {
val loginUser = userManager.findUserByID(userID)!!
val myPostIDList = loginUser.writtenPostIDList
if (myPostIDList.isEmpty()) {
noWrittenPostTextView.isVisible = true
myPostScrollView.isVisible = false
return
}
noWrittenPostTextView.isVisible = false
myPostScrollView.isVisible = true
myPostItemList.forEach { it.isVisible = false }
for (index in 0 until min(5, myPostIDList.size)) {
val postID = myPostIDList[myPostIDList.size - 1 - index]
val postItem = myPostItemList[index]
myPostItemIDMap[postItem.id] = postID
setPostItemUI(postItem, postID)
}
}
private fun initLikedPostScrollView() {
val loginUser = userManager.findUserByID(userID)!!
val likedPostIDList = loginUser.likedPostIDList
Log.d(TAG, "userID: ${userID}, likedPostList: ")
for (postID in likedPostIDList) {
Log.d(TAG, "$postID")
}
if (likedPostIDList.isEmpty()) {
noLikedPostTextView.isVisible = true
likedPostScrollView.isVisible = false
return
}
noLikedPostTextView.isVisible = false
likedPostScrollView.isVisible = true
likedPostItemList.forEach { it.isVisible = false }
for (index in 0 until min(5, likedPostIDList.size)) {
val postID = likedPostIDList[likedPostIDList.size - 1 - index]
val postItem = likedPostItemList[index]
likedPostItemIDMap[postItem.id] = postID
setPostItemUI(postItem, postID)
}
}
private fun setPostItemUI(postItem: ViewGroup, postID: Int) {
postItem.isVisible = true
val imageView = postItem.findViewById<ImageView>(R.id.pt_image)
val titleText = postItem.findViewById<TextView>(R.id.tv_post_title)
val nameText = postItem.findViewById<TextView>(R.id.tv_post_name)
val post = postManager.findPostByID(postID)!!
imageView.run {
if (post.image != null) {
try {
Log.d(TAG, "postID: ${post.postID}, post image is not null")
setImageURI(post.image)
scaleType = ImageView.ScaleType.CENTER_CROP
setPadding(0)
} catch (e: Exception) {
Log.d(TAG, "게시물 이미지 접근 오류 발생!")
setImageResource(R.drawable.hikers_icon_small_grey)
scaleType = ImageView.ScaleType.CENTER_INSIDE
setPadding(20)
}
} else {
Log.d(TAG, "postID: ${post.postID}, post image is null")
setImageResource(R.drawable.hikers_icon_small_grey)
scaleType = ImageView.ScaleType.CENTER_INSIDE
setPadding(20)
}
}
titleText.text = post.title
nameText.text = userManager.findUserByID(post.writerId)!!.name
}
private fun initPostItem() {
for (postItem in myPostItemList) {
postItem.setOnClickListener {
val postID = myPostItemIDMap[postItem.id]!!
Log.d(TAG, "post item clicked) post id: ${postID}")
if (postID == -1) return@setOnClickListener
goToDetail(postID)
}
}
for (postItem in likedPostItemList) {
postItem.setOnClickListener {
val postID = likedPostItemIDMap[postItem.id]!!
Log.d(TAG, "post item clicked) post id: ${postID}")
if (postID == -1) return@setOnClickListener
goToDetail(postID)
}
}
}
private fun goToDetail(postID: Int) {
val detailIntent = Intent(this, DetailPageActivity::class.java).apply {
putExtra("userID", userID)
putExtra("postID", postID)
putExtra("activityName", "MyPage")
}
startActivity(detailIntent)
}
private fun goToMain() {
back.setOnClickListener {
finish()
}
}
private fun goToUserInfo() {
}
}