From eac9e60953b27a35ea928635dc52fa8750f75b6a Mon Sep 17 00:00:00 2001 From: arinming Date: Sun, 8 Oct 2023 03:52:44 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20:=20=EA=B8=80=EC=93=B0=EA=B8=B0=20post?= =?UTF-8?q?=20=EC=9A=94=EC=B2=AD=20(#23)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 2 +- .../remote/request/WriteCustomerRequest.kt | 14 ++++ .../data/remote/service/WriteService.kt | 18 +++++ .../presentation/ui/main/MainActivity.kt | 4 +- .../presentation/ui/main/home/HomeFragment.kt | 6 +- .../ui/main/write/WriteCustomerFragment.kt | 77 +++++++++++++++++-- .../res/mipmap-anydpi-v26/ic_launcher.xml | 2 +- 7 files changed, 108 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/request/WriteCustomerRequest.kt create mode 100644 app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/service/WriteService.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 59301b8..edffcee 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -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" diff --git a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/request/WriteCustomerRequest.kt b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/request/WriteCustomerRequest.kt new file mode 100644 index 0000000..8416d13 --- /dev/null +++ b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/request/WriteCustomerRequest.kt @@ -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, +) \ No newline at end of file diff --git a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/service/WriteService.kt b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/service/WriteService.kt new file mode 100644 index 0000000..ae051ec --- /dev/null +++ b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/data/remote/service/WriteService.kt @@ -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 +} \ No newline at end of file diff --git a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/MainActivity.kt b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/MainActivity.kt index c2ced8f..50b27c7 100644 --- a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/MainActivity.kt +++ b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/MainActivity.kt @@ -38,7 +38,7 @@ class MainActivity : AppCompatActivity() { val view = binding.root setContentView(view) - HomeFragment().changeFragment() + HomeFragment(ArrayList()).changeFragment() initBottomNav() } @@ -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() diff --git a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/home/HomeFragment.kt b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/home/HomeFragment.kt index 23d4ccf..af959ee 100644 --- a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/home/HomeFragment.kt +++ b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/home/HomeFragment.kt @@ -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) : Fragment() { lateinit var binding: FragmentHomeBinding lateinit var mainActivity: MainActivity - private var itemList = ArrayList() private lateinit var mRecyclerView: RecyclerView @@ -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() } diff --git a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/write/WriteCustomerFragment.kt b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/write/WriteCustomerFragment.kt index fd532cc..260c65f 100644 --- a/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/write/WriteCustomerFragment.kt +++ b/app/src/main/java/com/pcandriod/kusitms_hackathon_c/presentation/ui/main/write/WriteCustomerFragment.kt @@ -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) : Fragment() { lateinit var fragmentWriteCustomerBinding: FragmentWriteCustomerBinding lateinit var mainActivity: MainActivity lateinit var albumLauncher: ActivityResultLauncher + // 업로드할 이미지의 Uri var uploadUri: Uri? = null override fun onCreateView( @@ -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가 이미지에 대한 사전 작업을 할 수 있도록) @@ -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() @@ -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 { + override fun onResponse( + call: Call, + response: Response + ) { + 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, t: Throwable) { + Log.e("HomeFragment", "API 실패 ${t}") + } + + }) + } else { + Toast.makeText(context, "제목과 내용을 모두 입력하세요 !", Toast.LENGTH_SHORT).show() + } + + } + } \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml index 6f3b755..f8cfbe8 100644 --- a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file