Skip to content

Commit

Permalink
[feat] : 글쓰기 post 요청 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
arinming committed Oct 7, 2023
1 parent 2dff77e commit eac9e60
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:roundIcon="@drawable/logo_main"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.Kusitms_hackathon_c"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.pcandriod.kusitms_hackathon_c.data.remote.request

import com.squareup.moshi.Json

data class WriteCustomerRequest(
@field:Json(name = "accessToken")
val accessToken: String,
@field:Json(name = "category")
val category: String,
@field:Json(name = "title")
val title: String,
@field:Json(name = "content")
val content: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.pcandriod.kusitms_hackathon_c.data.remote.service

import com.pcandriod.kusitms_hackathon_c.data.remote.request.SignInRequest
import com.pcandriod.kusitms_hackathon_c.data.remote.request.WriteCustomerRequest
import com.pcandriod.kusitms_hackathon_c.data.remote.response.ResponsePost
import com.pcandriod.kusitms_hackathon_c.data.remote.response.ResponseSignIn
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.Headers
import retrofit2.http.POST

interface WriteService {
@Headers("Content-Type: application/json;charset=UTF-8")
@POST("/api/articles")
fun postCustomer(
@Body writeCustomerRequest: WriteCustomerRequest,
): Call<ResponsePost>
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MainActivity : AppCompatActivity() {
val view = binding.root
setContentView(view)

HomeFragment().changeFragment()
HomeFragment(ArrayList()).changeFragment()
initBottomNav()
}

Expand All @@ -47,7 +47,7 @@ class MainActivity : AppCompatActivity() {
binding.btmNavMain.setOnItemSelectedListener {
when(it.itemId) {
R.id.nav_fragment_home -> {
HomeFragment().changeFragment()
HomeFragment(ArrayList()).changeFragment()
}
R.id.nav_fragment_map -> {
MapFragment().changeFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ import com.pcandriod.kusitms_hackathon_c.databinding.FragmentHomeBinding
import com.pcandriod.kusitms_hackathon_c.presentation.adapter.PostAdapter
import com.pcandriod.kusitms_hackathon_c.presentation.ui.main.MainActivity
import com.pcandriod.kusitms_hackathon_c.presentation.ui.main.write.WriteCustomerFragment
import com.pcandriod.kusitms_hackathon_c.presentation.ui.main.write.WriteOwnerFragment
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class HomeFragment : Fragment() {
class HomeFragment(var itemList : ArrayList<PostItem>) : Fragment() {
lateinit var binding: FragmentHomeBinding
lateinit var mainActivity: MainActivity
private var itemList = ArrayList<PostItem>()

private lateinit var mRecyclerView: RecyclerView

Expand Down Expand Up @@ -85,7 +83,7 @@ class HomeFragment : Fragment() {
binding.fabAddPost.setOnClickListener {
val fragmentManager = requireActivity().supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.replace(R.id.fv_main, WriteCustomerFragment())
transaction.replace(R.id.fv_main, WriteCustomerFragment(itemList))
transaction.addToBackStack(null)
transaction.commit()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,40 @@ import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.provider.MediaStore
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.pcandriod.kusitms_hackathon_c.R
import com.pcandriod.kusitms_hackathon_c.data.data.PostItem
import com.pcandriod.kusitms_hackathon_c.data.module.api.ApiModule
import com.pcandriod.kusitms_hackathon_c.data.remote.request.SignInRequest
import com.pcandriod.kusitms_hackathon_c.data.remote.request.WriteCustomerRequest
import com.pcandriod.kusitms_hackathon_c.data.remote.response.ResponsePost
import com.pcandriod.kusitms_hackathon_c.data.remote.response.ResponseSignIn
import com.pcandriod.kusitms_hackathon_c.data.remote.service.HomeService
import com.pcandriod.kusitms_hackathon_c.data.remote.service.WriteService
import com.pcandriod.kusitms_hackathon_c.databinding.FragmentWriteCustomerBinding
import com.pcandriod.kusitms_hackathon_c.databinding.FragmentWriteOwnerBinding
import com.pcandriod.kusitms_hackathon_c.presentation.adapter.PostAdapter
import com.pcandriod.kusitms_hackathon_c.presentation.ui.main.MainActivity
import com.pcandriod.kusitms_hackathon_c.presentation.ui.main.home.HomeFragment
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class WriteCustomerFragment : Fragment() {

class WriteCustomerFragment(var itemList: ArrayList<PostItem>) : Fragment() {

lateinit var fragmentWriteCustomerBinding: FragmentWriteCustomerBinding
lateinit var mainActivity: MainActivity
lateinit var albumLauncher: ActivityResultLauncher<Intent>

// 업로드할 이미지의 Uri
var uploadUri: Uri? = null
override fun onCreateView(
Expand All @@ -49,12 +64,13 @@ class WriteCustomerFragment : Fragment() {

btnComplete.setOnClickListener {
// 게시글 데이터 전송

postCustomer()
}

ibtnRegisterImage.setOnClickListener{
ibtnRegisterImage.setOnClickListener {
// 앨범에서 사진을 선택할 수 있는 Activity를 실행한다.
val newIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
val newIntent =
Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
// 실행할 액티비티의 마임타입 설정(이미지로 설정해준다)
newIntent.setType("image/*")
// 선택할 파일의 타입을 지정(안드로이드 OS가 이미지에 대한 사전 작업을 할 수 있도록)
Expand Down Expand Up @@ -89,14 +105,16 @@ class WriteCustomerFragment : Fragment() {
// 안드로이드 10 (Q) 이상이라면...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// 이미지를 생성할 수 있는 디코더를 생성한다.
val source = ImageDecoder.createSource(mainActivity.contentResolver, uploadUri!!)
val source =
ImageDecoder.createSource(mainActivity.contentResolver, uploadUri!!)
// Bitmap객체를 생성한다.
val bitmap = ImageDecoder.decodeBitmap(source)

previewImageView.setImageBitmap(bitmap)
} else {
// 컨텐츠 프로바이더를 통해 이미지 데이터 정보를 가져온다.
val cursor = mainActivity.contentResolver.query(uploadUri!!, null, null, null, null)
val cursor =
mainActivity.contentResolver.query(uploadUri!!, null, null, null, null)
if (cursor != null) {
cursor.moveToNext()

Expand All @@ -115,4 +133,49 @@ class WriteCustomerFragment : Fragment() {

return albumLauncher
}


private fun postCustomer() {
val api = ApiModule.getInstance().create(WriteService::class.java)
if (
fragmentWriteCustomerBinding.etvContent.text.toString() != ""
&&
fragmentWriteCustomerBinding.etvTitle.text.toString() != ""
) {
api.postCustomer(
WriteCustomerRequest(
"",
"",
fragmentWriteCustomerBinding.etvTitle.toString(),
fragmentWriteCustomerBinding.etvContent.toString()
)
).enqueue(object : Callback<ResponsePost> {
override fun onResponse(
call: Call<ResponsePost>,
response: Response<ResponsePost>
) {
Log.d("HomeFragment", "API 성공 ${response.body()}")
val postItem = PostItem(
fragmentWriteCustomerBinding.etvTitle.toString(),
fragmentWriteCustomerBinding.etvContent.toString()
)
itemList.add(postItem)
val fragmentManager = requireActivity().supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.replace(R.id.fv_main, HomeFragment(itemList))
transaction.addToBackStack(null)
transaction.commit()
}

override fun onFailure(call: Call<ResponsePost>, t: Throwable) {
Log.e("HomeFragment", "API 실패 ${t}")
}

})
} else {
Toast.makeText(context, "제목과 내용을 모두 입력하세요 !", Toast.LENGTH_SHORT).show()
}

}

}
2 changes: 1 addition & 1 deletion app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<background android:drawable="@drawable/logo_main" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
<monochrome android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>

0 comments on commit eac9e60

Please sign in to comment.