Skip to content

Commit

Permalink
Merge pull request #22 from Nbti/feat-naze
Browse files Browse the repository at this point in the history
[FEAT] Retrofit Test
  • Loading branch information
NaZe0320 authored Jun 10, 2023
2 parents a017f1d + ea4c37e commit 7f923ac
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.IDear"
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/nbit/Idear/RetrofitBuilder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.nbit.Idear

import com.nbit.Idear.write.WriteInterface
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitBuilder {
private val retrofit by lazy {
Retrofit.Builder()
.baseUrl("http://54.180.95.50:9010")
.addConverterFactory(GsonConverterFactory.create())
.build()
}

val writeApi: WriteInterface by lazy {
retrofit.create(WriteInterface::class.java)
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/nbit/Idear/write/WriteActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.nbit.Idear.databinding.ActivityWriteBinding

class WriteActivity : AppCompatActivity() {
private lateinit var binding: ActivityWriteBinding
//private lateinit var writeViewModel : WriteViewModel
private lateinit var writeViewModel : WriteViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -18,7 +18,7 @@ class WriteActivity : AppCompatActivity() {
val repository = WriteRepository()

val viewModelFactory = WriteFactory(repository,this@WriteActivity)
//writeViewModel = ViewModelProvider(this@WriteActivity, viewModelFactory)[WriteViewModel::class.java]
writeViewModel = ViewModelProvider(this@WriteActivity, viewModelFactory)[WriteViewModel::class.java]

supportFragmentManager.beginTransaction()
.add(R.id.fl_write, WriteFirstFragment()) // fragment_container는 Fragment를 삽입할 레이아웃의 ID입니다.
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/nbit/Idear/write/WriteFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nbit.Idear.write

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class WriteFactory(private val repo: WriteRepository, private val context: Context): ViewModelProvider.Factory {
override fun <T:ViewModel> create(modelClass: Class<T>): T {
return WriteViewModel(repo, context) as T
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/nbit/Idear/write/WriteFirstFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class WriteFirstFragment : Fragment() {
private var selectItem: String = ""
private var next: Int = 0

//private val viewModel: WriteViewModel by activityViewModels()
private val viewModel: WriteViewModel by activityViewModels()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = FragmentWriteFirstBinding.inflate(inflater, container, false)
Expand Down Expand Up @@ -82,7 +82,7 @@ class WriteFirstFragment : Fragment() {
.commit()
}

// viewModel.test()
viewModel.test()



Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/nbit/Idear/write/WriteInterface.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.nbit.Idear.write

import retrofit2.Response
import retrofit2.http.GET

interface WriteInterface {

@GET("/mypage/profile")
suspend fun getTest(

) : Response<Any>
}
2 changes: 2 additions & 0 deletions app/src/main/java/com/nbit/Idear/write/WriteModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.nbit.Idear.write

11 changes: 11 additions & 0 deletions app/src/main/java/com/nbit/Idear/write/WriteRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nbit.Idear.write

import com.nbit.Idear.RetrofitBuilder
import retrofit2.Response

class WriteRepository {
suspend fun getTest() : Response<Any> {
return RetrofitBuilder.writeApi.getTest()
}

}
22 changes: 22 additions & 0 deletions app/src/main/java/com/nbit/Idear/write/WriteViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.nbit.Idear.write

import android.content.Context
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch

class WriteViewModel(private val repo: WriteRepository, val context: Context): ViewModel() {


fun test() {
try {
viewModelScope.launch {
val response = repo.getTest()
Log.d("TEST","${response.body()}")
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}

0 comments on commit 7f923ac

Please sign in to comment.