Skip to content

Commit

Permalink
[MERGE] pull request #38 from SujungVillage/feature/noticeDetail
Browse files Browse the repository at this point in the history
[FEAT/#13, #24, #36] notice 관련 API 연결 시도
  • Loading branch information
Chaeyeon authored Aug 7, 2022
2 parents 8f3a7f7 + 8ae8116 commit 8c6a6bc
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ dependencies {

// viewpager
implementation 'com.google.android.material:material:1.4.0'

// retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0' // 레트로핏 설치 (필수)
implementation 'com.squareup.retrofit2:converter-gson:2.9.0' // 컨버터 (선택)
implementation 'com.google.code.gson:gson:2.9.1' //GSON (선택)
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="kr.co.sujungvillage_admin">

<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/kr/co/sujungvillage_admin/NoticeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ package kr.co.sujungvillage_admin
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import kr.co.sujungvillage_admin.adapter.NoticeAdapter
import kr.co.sujungvillage_admin.data.NoticeRequestResultDTO
import kr.co.sujungvillage_admin.databinding.ActivityNoticeBinding
import kr.co.sujungvillage_admin.retrofit.RetrofitBuilder
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class NoticeActivity : AppCompatActivity() {

Expand Down Expand Up @@ -36,5 +45,34 @@ class NoticeActivity : AppCompatActivity() {

override fun onNothingSelected(p0: AdapterView<*>?) { }
}

// 공지사항 리스트 조회 API 연결
RetrofitBuilder.noticeApi.noticeRequest("99990001").enqueue(object: Callback<List<NoticeRequestResultDTO>> {
override fun onResponse(call: Call<List<NoticeRequestResultDTO>>, response: Response<List<NoticeRequestResultDTO>>) {
Log.d("NOTICE_REQUEST", "size of notice list : " + response.body()?.size.toString())

// 공지사항이 존재하지 않는 경우
if (response.body()?.size == 0) {
binding.textNone.visibility = View.VISIBLE
return
}

val noticeList: MutableList<NoticeRequestResultDTO> = mutableListOf()
for (info in response.body()!!) {
var notice = NoticeRequestResultDTO(info.id, info.title, info.dormitory, info.date)
noticeList.add(notice)
}
var adapter = NoticeAdapter()
adapter.noticeList = noticeList
binding.recyclerNotice.adapter = adapter
binding.recyclerNotice.layoutManager = LinearLayoutManager(this@NoticeActivity)
}

override fun onFailure(call: Call<List<NoticeRequestResultDTO>>, t: Throwable) {
Toast.makeText(this@NoticeActivity, "공지사항 조회 오류가 발생하였습니다.", Toast.LENGTH_SHORT).show()
Log.d("NOTICE_REQUEST", "공지사항 리스트 조회 실패")
Log.d("NOTICE_REQUEST", t.message.toString())
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ package kr.co.sujungvillage_admin

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import kr.co.sujungvillage_admin.data.NoticeDetailResultDTO
import kr.co.sujungvillage_admin.databinding.ActivityNoticeDetailBinding
import kr.co.sujungvillage_admin.retrofit.RetrofitBuilder
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class NoticeDetailActivity : AppCompatActivity() {

Expand All @@ -12,7 +19,33 @@ class NoticeDetailActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(binding.root)

// NoticeActivity에서 공지사항 ID 전달 받기
val noticeId = intent.getLongExtra("noticeId", -1)

// 뒤로가기 버튼 연결
binding.btnBack.setOnClickListener { finish() }

// 공지사항 상세 조회 API 연결
RetrofitBuilder.noticeApi.noticeDetailRequest("20180001", noticeId).enqueue(object: Callback<NoticeDetailResultDTO> {
override fun onResponse(call: Call<NoticeDetailResultDTO>, response: Response<NoticeDetailResultDTO>) {
Log.d("NOTICE_DETAIL_REQUEST", "id : " + response.body()?.id)
Log.d("NOTICE_DETAIL_REQUEST", "writer : " + response.body()?.name)
Log.d("NOTICE_DETAIL_REQUEST", "title : " + response.body()?.title)
Log.d("NOTICE_DETAIL_REQUEST", "content : " + response.body()?.content)
Log.d("NOTICE_DETAIL_REQUEST", "dormitory : " + response.body()?.dormitory)
Log.d("NOTICE_DETAIL_REQUEST", "register date : " + response.body()?.regDate)
Log.d("NOTICE_DETAIL_REQUEST", "modify date : " + response.body()?.modDate)

binding.textDormitory.text = response.body()?.dormitory
binding.textTitle.text = response.body()?.title
binding.textContent.text = response.body()?.content
}

override fun onFailure(call: Call<NoticeDetailResultDTO>, t: Throwable) {
Toast.makeText(this@NoticeDetailActivity, "공지사항 상세 조회 오류가 발생하였습니다.", Toast.LENGTH_SHORT).show()
Log.d("NOTICE_DETAIL_REQUEST", "공지사항 상세 조회 실패")
finish()
}
})
}
}
56 changes: 56 additions & 0 deletions app/src/main/java/kr/co/sujungvillage_admin/NoticeWriteActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ package kr.co.sujungvillage_admin

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import kr.co.sujungvillage_admin.base.hideKeyboard
import kr.co.sujungvillage_admin.data.NoticeCreateDTO
import kr.co.sujungvillage_admin.data.NoticeCreateResultDTO
import kr.co.sujungvillage_admin.data.NoticeDetailResultDTO
import kr.co.sujungvillage_admin.data.NoticeRequestResultDTO
import kr.co.sujungvillage_admin.databinding.ActivityNoticeWriteBinding
import kr.co.sujungvillage_admin.retrofit.RetrofitBuilder
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class NoticeWriteActivity : AppCompatActivity() {

Expand All @@ -16,6 +26,9 @@ class NoticeWriteActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(binding.root)

// ★★★ 로그인 회원 번호 불러오기
val userNum = "99990001"

// 키보드 내리기
binding.layout.setOnClickListener { this.hideKeyboard() }
binding.linear.setOnClickListener { this.hideKeyboard() }
Expand All @@ -34,5 +47,48 @@ class NoticeWriteActivity : AppCompatActivity() {

override fun onNothingSelected(p0: AdapterView<*>?) { }
}

// 등록 버튼 연결 : 공지사항 API 연결 및 액티비티 종료
binding.btnUpload.setOnClickListener {
// 비어있는 항목이 있는 경우
if (binding.editTitle.text.isEmpty()) {
Toast.makeText(this, "제목을 입력해주세요.", Toast.LENGTH_SHORT).show()
return@setOnClickListener
} else if (binding.editContent.text.isEmpty()) {
Toast.makeText(this, "내용을 입력해주세요.", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
val title = binding.editTitle.text.toString()
val content = binding.editContent.text.toString()
val dormitory = when (binding.spinnerDormitory.selectedItem) {
0 -> "전체"
1 -> "성미료"
2 -> "성미관"
3 -> "풍림"
4 -> "엠시티"
5 -> "그레이스"
6 -> "이율"
7 -> "장수"
else -> "오류"
}
var noticeInfo = NoticeCreateDTO(title, content, dormitory)

RetrofitBuilder.noticeApi.noticeCreate(userNum, noticeInfo).enqueue(object: Callback<NoticeCreateResultDTO> {
override fun onResponse(call: Call<NoticeCreateResultDTO>, response: Response<NoticeCreateResultDTO>) {
Log.d("NOTICE_CREATE", "id : " + response.body()?.id)
Log.d("NOTICE_CREATE", "writer : " + response.body()?.writer)
Log.d("NOTICE_CREATE", "title : " + response.body()?.title)
Log.d("NOTICE_CREATE", "content : " + response.body()?.content)
Log.d("NOTICE_CREATE", "register date : " + response.body()?.regDate)
Log.d("NOTICE_CREATE", "modify date : " + response.body()?.modDate)
Log.d("NOTICE_CREATE", "dormitory : " + response.body()?.dormitory)
}

override fun onFailure(call: Call<NoticeCreateResultDTO>, t: Throwable) {
Log.d("NOTICE_CREATE", "공지사항 작성 요청 실패")
Log.d("NOTICE_CREATE", t.message.toString())
}
})
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package kr.co.sujungvillage_admin.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import kr.co.sujungvillage_admin.NoticeDetailActivity
import kr.co.sujungvillage_admin.data.NoticeRequestResultDTO
import kr.co.sujungvillage_admin.databinding.ListitemNoticePostBinding

class NoticeAdapter : RecyclerView.Adapter<NoticeHolder>() {
var noticeList = mutableListOf<NoticeRequestResultDTO>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoticeHolder {
val binding = ListitemNoticePostBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return NoticeHolder(binding)
}

override fun onBindViewHolder(holder: NoticeHolder, position: Int) {
val notice = noticeList.get(position)
holder.setNotice(notice)
}

override fun getItemCount(): Int {
return noticeList.size
}
}

class NoticeHolder(val binding: ListitemNoticePostBinding): RecyclerView.ViewHolder(binding.root) {
fun setNotice(notice: NoticeRequestResultDTO) {
binding.textId.text = "${notice.id}"
binding.textDormitory.text = "${notice.dormitory}"
binding.textTitle.text = "${notice.title}"
binding.textDate.text = "${notice.date.subSequence(0, 4)}.${notice.date.subSequence(5, 7)}.${notice.date.subSequence(8, 10)}"

// 공지사항 클릭 시 상세 액티비티 생성
binding.root.setOnClickListener {
val intent = Intent(binding.root.context, NoticeDetailActivity::class.java)
intent.putExtra("noticeId", notice.id)
startActivity(binding.root.context, intent, null)
}
}
}
30 changes: 30 additions & 0 deletions app/src/main/java/kr/co/sujungvillage_admin/api/NoticeService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package kr.co.sujungvillage_admin.api

import kr.co.sujungvillage_admin.data.NoticeCreateDTO
import kr.co.sujungvillage_admin.data.NoticeCreateResultDTO
import kr.co.sujungvillage_admin.data.NoticeDetailResultDTO
import kr.co.sujungvillage_admin.data.NoticeRequestResultDTO
import retrofit2.Call
import retrofit2.http.*

interface NoticeService {
// 공지사항 리스트 조회
@GET("/api/common/getAnnouncementList")
fun noticeRequest(
@Header("user_id") userId: String,
): Call<List<NoticeRequestResultDTO>>

// 공지사항 상세 조회
@GET("/api/common/getDetailedAnnouncement")
fun noticeDetailRequest(
@Header("user_id") userId: String,
@Query("notice_id") noticeId: Long,
): Call<NoticeDetailResultDTO>

// 공지사항 작성
@POST("/api/admin/writeAnnouncement")
fun noticeCreate(
@Header("user_id") userId: String,
@Body noticeInfo: NoticeCreateDTO,
): Call<NoticeCreateResultDTO>
}
58 changes: 58 additions & 0 deletions app/src/main/java/kr/co/sujungvillage_admin/data/Notice.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package kr.co.sujungvillage_admin.data

import com.google.gson.annotations.SerializedName
import java.io.Serializable

data class NoticeRequestResultDTO(
@SerializedName("id")
val id: Long,
@SerializedName("title")
val title: String,
@SerializedName("dormitoryName")
val dormitory: String,
@SerializedName("regDate")
val date: String,
): Serializable {}

data class NoticeDetailResultDTO(
@SerializedName("id")
val id: Long,
@SerializedName("writerName")
val name: String,
@SerializedName("title")
val title: String,
@SerializedName("content")
val content: String,
@SerializedName("dormitoryName")
val dormitory: String,
@SerializedName("regDate")
val regDate: String,
@SerializedName("modDate")
val modDate: String,
): Serializable {}

data class NoticeCreateDTO(
@SerializedName("title")
val title: String,
@SerializedName("content")
val content: String,
@SerializedName("dormitoryName")
val dormitory: String,
): Serializable {}

data class NoticeCreateResultDTO(
@SerializedName("id")
val id: Long,
@SerializedName("writerId")
val writer: String,
@SerializedName("title")
val title: String,
@SerializedName("content")
val content: String,
@SerializedName("regDate")
val regDate: String,
@SerializedName("modDate")
val modDate: String,
@SerializedName("dormitoryName")
val dormitory: String,
): Serializable {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kr.co.sujungvillage_admin.retrofit

import com.google.gson.GsonBuilder
import kr.co.sujungvillage_admin.BuildConfig.BASE_URL
import kr.co.sujungvillage_admin.api.NoticeService
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitBuilder {
// 사용할 API 인터페이스 선언
var noticeApi: NoticeService

val gson = GsonBuilder().setLenient().create()

init {
// API 서버 연결
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()

noticeApi = retrofit.create(NoticeService::class.java)
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/layout/activity_notice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@
android:orientation="vertical"
android:paddingBottom="50dp">

<TextView
android:id="@+id/text_none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:fontFamily="@font/medium"
android:text="존재하는 공지사항이 없습니다."
android:visibility="gone"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_notice"
android:layout_width="match_parent"
Expand Down
Loading

0 comments on commit 8c6a6bc

Please sign in to comment.