Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 홈화면 + 커뮤니티( 포스트 미구현) #2

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'kotlin-android'
}


android {
compileSdk 31

Expand Down Expand Up @@ -51,4 +52,8 @@ dependencies {
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.4'

//Glide
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.likefirst.meyouhouse">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.MeYouHouse">
<activity
android:name=".ui.main.MainActivity"
Expand All @@ -18,6 +23,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".ui.community.ArticlePostingActivity"/>
<activity android:name=".ui.community.ArticleDetailActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class XAccessTokenInterceptor: Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val builder: Request.Builder = chain.request().newBuilder()

val jwtToken: String? = getJwt()
// TODO
// null 대신에 getUser() sharedPreferencesManager에 정의해서 사용해야 합니다
val jwtToken: String? = null //or host or

jwtToken?.let{
builder.addHeader(X_ACCESS_TOKEN, jwtToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.likefirst.meyouhouse.data.dto.community

import com.google.gson.annotations.SerializedName

data class Article(
@SerializedName("postId") val postId : Int,
@SerializedName("content") val content : String,
@SerializedName("createdAt") val createdAt : String,
@SerializedName("commentCnt") val commentCnt : Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.likefirst.meyouhouse.data.dto.community

import com.google.gson.annotations.SerializedName

data class Articles(
@SerializedName("items") val items : MutableList<Article>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.likefirst.meyouhouse.data.dto.community

import com.google.gson.annotations.SerializedName

data class Comment(
@SerializedName("id") val id : Int,
@SerializedName("content") val content : String,
@SerializedName("createdAt") val createdAt : String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.likefirst.meyouhouse.data.dto.community

import com.google.gson.annotations.SerializedName

data class DetailArticle(
@SerializedName("postId") val postId : Int,
@SerializedName("content") val content : String,
@SerializedName("imgs") val imgs : MutableList<String>,
@SerializedName("comments") val comments : MutableList<Comment>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.likefirst.meyouhouse.data.dto.community

import com.google.gson.annotations.SerializedName

data class DetailImage(
@SerializedName("imgs") val url : MutableList<String>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.likefirst.meyouhouse.data.dto.community

import com.google.gson.annotations.SerializedName

data class PostCommentResult (
@SerializedName("content") val content : String,
@SerializedName("userId") val userId : Int,
@SerializedName("postId") val postId : Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.likefirst.meyouhouse.data.dto.community

import android.net.Uri

data class SelectedImage(
val name : String,
val uri : Uri
)
7 changes: 4 additions & 3 deletions app/src/main/java/com/likefirst/meyouhouse/ui/BaseFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import androidx.viewbinding.ViewBinding
import com.likefirst.meyouhouse.util.Inflate

abstract class BaseFragment<VB : ViewBinding>(
private val inflate: Inflate<VB>
private val inflate: Inflate<VB>,

) : Fragment() {
) : Fragment() {
private var _binding: VB? = null
protected val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
savedInstanceState: Bundle?,
): View? {
_binding = inflate.invoke(inflater, container, false)

Expand All @@ -36,6 +36,7 @@ abstract class BaseFragment<VB : ViewBinding>(
_binding = null
}


protected abstract fun initAfterBinding()

fun showToast(message: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import com.likefirst.meyouhouse.ui.BaseFragment

class CalendarFragment : BaseFragment<FragmentCalendarBinding>(FragmentCalendarBinding::inflate) {
override fun initAfterBinding() {
TODO("Not yet implemented")

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.likefirst.meyouhouse.ui.community

import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.likefirst.meyouhouse.data.dto.community.*
import com.likefirst.meyouhouse.databinding.ActivityArticleDetailBinding
import com.likefirst.meyouhouse.ui.BaseActivity
import com.likefirst.meyouhouse.util.RetrofitInterface
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.text.SimpleDateFormat
import java.util.*

class ArticleDetailActivity :
BaseActivity<ActivityArticleDetailBinding>(ActivityArticleDetailBinding::inflate) {

val comments = mutableListOf<Comment>()
val images = mutableListOf<String>()

lateinit var retrofit: Retrofit
lateinit var retrofitService : RetrofitInterface
private lateinit var commentAdapter: CommentRVAdapter
private lateinit var detailImageAdapter: DetailImageRVAdapter
private var postId = 0
private lateinit var date : String

override fun initAfterBinding() {
postId = intent.getIntExtra(CommunityFragment.POST_ID,0)
date = intent.getStringExtra(CommunityFragment.DATE).toString()

initCommentRV()
initDetailImageRV()
initRetrofit()
getDetailArticle()
initCommentSubmitButton()
}

private fun initDetailImageRV() {
detailImageAdapter = DetailImageRVAdapter(images)
binding.detailImageRecyclerView.layoutManager = LinearLayoutManager(this).apply {
orientation = LinearLayoutManager.HORIZONTAL
}
binding.detailImageRecyclerView.adapter = detailImageAdapter
}

private fun initBodyAndDateTextView(content : String, date : String) {
binding.detailBodyTextView.text = content
binding.detailDate.text = date
}

private fun initCommentRV() {
commentAdapter = CommentRVAdapter(comments)
binding.commentRecyclerView.layoutManager = LinearLayoutManager(this)
binding.commentRecyclerView.adapter = commentAdapter
}


private fun initRetrofit() {
retrofit = Retrofit.Builder().baseUrl(CommunityFragment.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()

retrofitService = retrofit.create(RetrofitInterface::class.java)
}

private fun getDetailArticle() {
retrofitService.getArticlesDetail(postId.toString())
.enqueue(object : Callback<DetailArticle>{
override fun onResponse(
call: Call<DetailArticle>,
response: Response<DetailArticle>,
) {
if(response.isSuccessful.not()) return
response.body().let {
if (it == null ) return
initBodyAndDateTextView(it.content,date)
detailImageAdapter.setData(it.imgs)
commentAdapter.setData(it.comments)
}
}
override fun onFailure(call: Call<DetailArticle>, t: Throwable) {
showToast("데이터 불러오기 실패")
}
})
}

private fun initCommentSubmitButton() {
binding.commentSubmitButton.setOnClickListener {
val content = binding.commentEditText.text.toString()
// TODO userID를 어떻게 처리해야하는지 질문
val userId = 1
val comment = PostCommentResult(content,userId,postId.toInt())

retrofitService.postComment(comment)
.enqueue(object : Callback<ResponseBody>{
override fun onResponse(
call: Call<ResponseBody>,
response: Response<ResponseBody>,
) {
if(response.isSuccessful.not()) return
Log.d(TAG,response.body().toString())
binding.commentEditText.text.clear()
getDetailArticle()
}
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
Log.d(TAG,t.message.toString())
showToast("네트워크 오류")
}
})
}
}

companion object {
const val TAG = "ArticleDetailAt"
}
}
Loading