Skip to content

Commit

Permalink
Merge pull request #18 from Fill-ENERGY/Feature/#4-community
Browse files Browse the repository at this point in the history
Feature/#4 community
  • Loading branch information
seohyeon121 authored Jul 21, 2024
2 parents 9c6e13e + 4b62824 commit f829054
Show file tree
Hide file tree
Showing 30 changed files with 657 additions and 104 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,7 @@ dependencies {
//recyclerview
implementation("androidx.recyclerview:recyclerview:1.3.2")

//image upload
implementation("com.github.bumptech.glide:glide:4.16.0")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.energy.data.repository.community

import android.net.Uri

data class CommunityPost(
val userProfile: Int, //프로필 사진
val userName: String, //사용자 이름
val title: String, //제목
val content: String, //내용
val category: List<String>, //카테고리 리스트
val imageUrl: List<Uri>, //사진 리스트
val likes: String, //좋아요 수
val comments: String, //댓글 수
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.energy.data.repository.community

import android.net.Uri

data class WritingCommunityImage(
val imageUrl: Uri, // 선택한 이미지
var isRepresentative: Boolean = false, // 대표 이미지 여부를 나타내는 속성 추가
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
package com.example.energy.presentation.view.community

import android.net.Uri
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.energy.R
import com.example.energy.data.repository.community.CommunityPost
import com.example.energy.data.repository.community.WritingCommunityImage
import com.example.energy.databinding.FragmentCommunityWholeBinding
import com.example.energy.presentation.view.base.BaseFragment

class CommunityWholeFragment : BaseFragment<FragmentCommunityWholeBinding>({ FragmentCommunityWholeBinding.inflate(it)}) {

var postInfo = ArrayList<CommunityPost>() //선택한 이미지 데이터 리스트
val categoriesList = listOf("도와줘요", "요청 중") //임시 카테고리 리스트
val imageUrlsList: List<Uri> = emptyList() // 임시 이미지 리스트

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

// RecyclerView 연결 및 초기화
binding.wholeCommunityPostRv.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
val postCommunityAdapter = PostCommunityRVAdapter(postInfo)
binding.wholeCommunityPostRv.adapter = postCommunityAdapter

// 더미데이터
postInfo.apply{
add(CommunityPost(R.drawable.user_profile, "김규리", "연희동 급 SOS", "혹시 지금 연희동 쪽으로 도움 주러 오실 수 있는 분 계신가요? 멈춰서 움직일수가 없어요ㅠㅠ",
categoriesList, imageUrlsList, "1", "3"))
add(CommunityPost(R.drawable.user_profile, "김규리", "연희동 급 SOS", "혹시 지금 연희동 쪽으로 도움 주러 오실 수 있는 분 계신가요? 멈춰서 움직일수가 없어요ㅠㅠ",
categoriesList, imageUrlsList, "1", "3"))
add(CommunityPost(R.drawable.user_profile, "김규리", "연희동 급 SOS", "혹시 지금 연희동 쪽으로 도움 주러 오실 수 있는 분 계신가요? 멈춰서 움직일수가 없어요ㅠㅠ",
categoriesList, imageUrlsList, "1", "3"))
add(CommunityPost(R.drawable.user_profile, "김규리", "연희동 급 SOS", "혹시 지금 연희동 쪽으로 도움 주러 오실 수 있는 분 계신가요? 멈춰서 움직일수가 없어요ㅠㅠ",
categoriesList, imageUrlsList, "1", "3"))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package com.example.energy.presentation.view.community

import android.app.Activity
import android.app.Dialog
import android.content.Intent
import android.content.Intent.ACTION_GET_CONTENT
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.widget.ImageView
import android.widget.LinearLayout
import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.energy.R
import com.example.energy.data.repository.community.WritingCommunityImage
import com.example.energy.databinding.ActivityCommunityWritingBinding
import com.example.energy.databinding.DialogSelectCategoryBinding
import com.google.android.material.bottomsheet.BottomSheetDialog


class CommunityWritingActivity : AppCompatActivity(), WritingCommunityImageRVAdapter.MyItemClickListener {
class CommunityWritingActivity : AppCompatActivity(), GalleryAdapter.MyItemClickListener {
private lateinit var binding: ActivityCommunityWritingBinding
var list = ArrayList<WritingCommunityImage>() //선택한 이미지 데이터 리스트
val adapter = WritingCommunityImageRVAdapter(list) //Recycler View Adapter
var imageList = ArrayList<WritingCommunityImage>() //선택한 이미지 데이터 리스트
val adapter = GalleryAdapter(imageList) //Recycler View Adapter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -32,52 +36,73 @@ class CommunityWritingActivity : AppCompatActivity(), WritingCommunityImageRVAda
// 어댑터에 클릭 리스너 설정
adapter.setMyItemClickListener(this)

// 이미지 업로드
val selectImagesActivityResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data: Intent? = result.data
// 다중 이미지 선택
if (data?.clipData != null) {
val count = data.clipData?.itemCount ?: 0
for (i in 0 until count) {
val imageUri = data.clipData!!.getItemAt(i).uri
addImageToList(imageUri)
}
}
// 단일 이미지 선택
else if (data?.data != null) {
val imageUri: Uri? = data.data
if(imageUri !=null) {
addImageToList(imageUri)
}
}
}
adapter.notifyDataSetChanged()

}

// 뒤로가기 버튼
binding.communityWritingBackIcon.setOnClickListener {
finish() // 현재 액티비티 종료
}

// 이미지 업로드 버튼 클릭 시
// 카테고리 선택 버튼 클릭 리스너 설정
binding.communityWritingCategoryUnderBtn.setOnClickListener {
showCategoryDialog()
}

// 이미지 업로드 사진 클릭 시
binding.communityWritingImageSelect.setOnClickListener {
val intent = Intent(ACTION_GET_CONTENT)
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.type = "*/*"
selectImagesActivityResult.launch(intent)
val intent = Intent(Intent.ACTION_PICK) //갤러리 호출
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true) //멀티 선택 기능
activityResult.launch(intent)
}
}
private fun showCategoryDialog() { //카테고리 선택 화면
val dialog = Dialog(this)
val binding = DialogSelectCategoryBinding.inflate(layoutInflater)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) // 기존 다이어그램 배경 투명으로 적용(커스텀한 배경이 보이게 하기 위함)
dialog.window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
dialog.setContentView(binding.root)
dialog.setCancelable(true) // 바깥 영역 터치하면 닫힘

// 다이얼로그 표시
dialog.show()
}

// 이미지 가져오기
private var activityResult: ActivityResultLauncher<Intent> = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
){
//결과 코드 OK, 결과값 null 아니면 (이미지를 선택하면..)
if (it.resultCode == RESULT_OK){
//멀티 선택은 clip Data
if (it.data!!.clipData != null) { //멀티 이미지
val count = it.data!!.clipData!!.itemCount //선택한 이미지 갯수
for(index in 0 until count){
val imageUri = it.data!!.clipData!!.getItemAt(index).uri //이미지 담기
addImageToList(imageUri) //이미지 추가
}
} else{ //싱글 이미지
val imageUri = it.data!!.data
addImageToList(imageUri!!)
}
adapter.notifyDataSetChanged()
}
}

private fun addImageToList(imageUri: Uri) { //데이터 리스트에 업로드하는 이미지 저장
list.add(WritingCommunityImage(imageUri))
adapter.notifyItemInserted(list.size - 1)
val isRepresentative = imageList.isEmpty() // 첫 번째 이미지인 경우 대표 이미지로 설정
imageList.add(WritingCommunityImage(imageUri, isRepresentative))
adapter.notifyItemInserted(imageList.size - 1)
}

override fun onRemoveImage(position: Int) { //이미지 삭제
adapter.removeImage(position)
// 대표 이미지가 삭제된 경우 새로운 대표 이미지 설정
if (imageList.isNotEmpty() && !imageList.any { it.isRepresentative }) {
imageList[0].isRepresentative = true
adapter.notifyItemChanged(0)
}
}

}
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
package com.example.energy.presentation.view.community

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.energy.data.repository.community.WritingCommunityImage
import com.example.energy.databinding.ItemWritingCommunityImageBinding

class WritingCommunityImageRVAdapter (private val imageUrl: ArrayList<WritingCommunityImage>): RecyclerView.Adapter<WritingCommunityImageRVAdapter.ViewHolder>() {

class GalleryAdapter (private val imageUrl: ArrayList<WritingCommunityImage>): RecyclerView.Adapter<GalleryAdapter.ViewHolder>() {

interface MyItemClickListener{
fun onRemoveImage(position: Int)
}

// 외부에서 전달받은 Listener 객체를 Adapter에서 사용할 수 있도록 따로 저장할 변수 선언
// Listener 객체를 Adapter에서 사용할 수 있도록 따로 저장할 변수 선언
private lateinit var mItemClickListener: MyItemClickListener

fun setMyItemClickListener(itemClickListener: MyItemClickListener){
mItemClickListener = itemClickListener
}

override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): WritingCommunityImageRVAdapter.ViewHolder {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): GalleryAdapter.ViewHolder {
// itemview 객체 생성
val binding: ItemWritingCommunityImageBinding = ItemWritingCommunityImageBinding.inflate(LayoutInflater.from(viewGroup.context), viewGroup, false)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: WritingCommunityImageRVAdapter.ViewHolder, position: Int) {
holder.bind(imageUrl[position])
override fun onBindViewHolder(holder: GalleryAdapter.ViewHolder, position: Int) {
val currentImage = imageUrl[position]

Glide.with(holder.itemView.context)
.load(imageUrl[position].imageUrl) //이미지 위치
.into(holder.galleryView) //보여줄 위치

// 대표 이미지 표시
holder.binding.representativeLabel.visibility = if (currentImage.isRepresentative) View.VISIBLE else View.GONE

// X 아이콘 클릭 시 해당데이터 삭제
holder.binding.writingCommunityImageCancel.setOnClickListener {
Expand All @@ -46,9 +56,10 @@ class WritingCommunityImageRVAdapter (private val imageUrl: ArrayList<WritingCom
}

inner class ViewHolder(val binding: ItemWritingCommunityImageBinding): RecyclerView.ViewHolder(binding.root) {
fun bind(image: WritingCommunityImage) {
binding.itemWritingCommunityImage.setImageURI(image.imageUrl)
}
val galleryView : ImageView = binding.itemWritingCommunityImage
// fun bind(image: WritingCommunityImage) {
// binding.itemWritingCommunityImage.setImageURI(image.imageUrl)
// }
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.energy.presentation.view.community

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.energy.data.repository.community.CommunityPost
import com.example.energy.databinding.ItemCommunityPostBinding
import com.example.energy.databinding.ItemWritingCommunityImageBinding

class PostCommunityRVAdapter (private val postInfo: ArrayList<CommunityPost>): RecyclerView.Adapter<PostCommunityRVAdapter.ViewHolder>() {
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): PostCommunityRVAdapter.ViewHolder {
// itemview 객체 생성
val binding: ItemCommunityPostBinding = ItemCommunityPostBinding.inflate(
LayoutInflater.from(viewGroup.context), viewGroup, false)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(postInfo[position])
}


override fun getItemCount(): Int = postInfo.size

inner class ViewHolder(val binding: ItemCommunityPostBinding): RecyclerView.ViewHolder(binding.root) {
fun bind(postInfo: CommunityPost) {
binding.itemCommunityPostUserProfile.setImageResource(postInfo.userProfile)
binding.itemCommunityPostUserName.text = postInfo.userName
binding.itemCommunityPostTitle.text = postInfo.title
binding.itemCommunityPostContent.text = postInfo.content
binding.itemCommunityPostLikeNum.text = postInfo.likes
binding.itemCommunityPostCommentNum.text = postInfo.comments
}
}
}

This file was deleted.

5 changes: 5 additions & 0 deletions app/src/main/res/drawable/btn_sort_selected.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="@color/gray_scale8"/>
<size android:width="4dp" android:height="4dp"/>
</shape>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/btn_sort_unselected.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="@color/gray_scale6"/>
<size android:width="4dp" android:height="4dp"/>
</shape>
Binary file added app/src/main/res/drawable/check_box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/comment_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/dialog_style.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<corners
android:radius="10dp"
/>
</shape>
Binary file added app/src/main/res/drawable/like_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/representative_label_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/main_orange" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp" />
</shape>
Binary file added app/src/main/res/drawable/tag_curious.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/tag_daily.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/tag_help.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/tag_scooter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/tag_wheelchair.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/uncheck_box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/under_vector.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/user_profile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f829054

Please sign in to comment.