Skip to content
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
15 changes: 15 additions & 0 deletions backy/week4/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,19 @@ dependencies {
implementation("androidx.room:room-ktx:$roomVersion")
kapt("androidx.room:room-compiler:$roomVersion")

//Retrofit
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("com.squareup.retrofit2:adapter-rxjava2:2.9.0")

//okhttp
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")

//Glide
implementation("com.github.bumptech.glide:glide:4.11.0")
implementation("com.github.bumptech.glide:compiler:4.11.0")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0")
}
4 changes: 3 additions & 1 deletion backy/week4/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
Expand All @@ -14,7 +15,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Week2">
android:theme="@style/Theme.Week2"
android:usesCleartextTraffic="true">
<service
android:name=".MusicService"
android:enabled="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.rkdgudrn4094.week2

import com.google.gson.annotations.SerializedName

data class AuthResponse(
@SerializedName(value="status") val status: Boolean,
@SerializedName(value="code") val code: String,
@SerializedName(value="message") val message: String,
@SerializedName(value="data") val data: LoginData?
)

data class LoginData(
@SerializedName(value="name") val name: String,
@SerializedName(value="memberId") val memberId: Int,
@SerializedName(value="accessToken") var accessToken: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.rkdgudrn4094.week2

import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.POST

interface AuthRetrofitInterface {
@POST("signup")
fun signUp(@Body user: User): Call<AuthResponse>

@POST("login")
fun login(@Body user: User):Call<AuthResponse>

@GET("test")
fun test(@Header("Authorization") token: String): Call<TestResponse>
}
119 changes: 119 additions & 0 deletions backy/week4/app/src/main/java/com/rkdgudrn4094/week2/AuthService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.rkdgudrn4094.week2

import android.util.Log
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class AuthService {
private lateinit var signUpView: SignUpView
private lateinit var loginView: LoginView
private lateinit var testView: TestView

fun setSignUpView(signUpView: SignUpView){
this.signUpView = signUpView
}
fun setLoginView(loginView: LoginView){
this.loginView = loginView
}
fun setTestView(testView: TestView){
this.testView = testView
}

fun signUp(user: User){
val authService = getRetrofit().create(AuthRetrofitInterface::class.java)
authService.signUp(user).enqueue(object: Callback<AuthResponse>{
override fun onResponse(
call: Call<AuthResponse?>,
response: Response<AuthResponse?>
) {
Log.d("SIGNUP/SUCCESS", response.toString())

val resp: AuthResponse = response.body()!!

if(::signUpView.isInitialized){
when(resp.code){
"COMMON201"->signUpView.onSignUpSuccess()
else->signUpView.onSignUpFailure()
}
}

}

override fun onFailure(
call: Call<AuthResponse?>,
t: Throwable
) {
Log.d("SIGNUP/FAILURE", t.message.toString())
}


})

Log.d("SIGNUP", "HELLOOO")
}

fun login(user: User){
val authService = getRetrofit().create(AuthRetrofitInterface::class.java)
authService.login(user).enqueue(object: Callback<AuthResponse>{
override fun onResponse(
call: Call<AuthResponse?>,
response: Response<AuthResponse?>
) {
Log.d("LOGIN/SUCCESS", response.toString())

val resp: AuthResponse = response.body()!!
val code = resp.code
if(::loginView.isInitialized){
when(code){
"COMMON200_1"->loginView.onLoginSuccess(code, resp.data!!)
else->loginView.onLoginFailure()
}
}

}

override fun onFailure(
call: Call<AuthResponse?>,
t: Throwable
) {
Log.d("LOGIN/FAILURE", t.message.toString())
}
})

Log.d("LOGIN", "HELLOOO")
}

fun test(token: String){
val bearerToken = if (token.startsWith("Bearer ")) token else "Bearer $token"

val authService = getRetrofit().create(AuthRetrofitInterface::class.java)
authService.test(bearerToken).enqueue(object: Callback<TestResponse>{
override fun onResponse(
call: Call<TestResponse?>,
response: Response<TestResponse?>
) {
Log.d("TEST/SUCCESS", response.toString())

val resp: TestResponse = response.body()!!
val code = resp.code
if(::testView.isInitialized){
when(code){
"COMMON200_1"->testView.onTestSuccess(resp.message)
else->testView.onTestFailure()
}
}
}

override fun onFailure(
call: Call<TestResponse?>,
t: Throwable
) {
Log.d("TEST/FAILURE", t.message.toString())
}

})

Log.d("LOGIN", "HELLOOO")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.rkdgudrn4094.week2.databinding.ActivityLoginBinding

class LoginActivity: AppCompatActivity() {
class LoginActivity: AppCompatActivity(), LoginView {
lateinit var binding: ActivityLoginBinding

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -39,14 +39,19 @@ class LoginActivity: AppCompatActivity() {
val email: String = binding.loginIdEt.text.toString() + '@' + binding.loginDirectInputEt.text.toString()
val pwd: String = binding.loginPasswordEt.text.toString()

/*
val songDB = SongDatabase.getInstance(this)!!
val user = songDB.userDao().getUser(email, pwd)

user?.let {
Log.d("LOGIN_ACT/GET_USER", "userId: ${user.id}, $user")
saveJwt(user.id)
startMainActivity()
}
}*/
val authService = AuthService()
authService.setLoginView(this)

authService.login(User("", email, pwd))

Toast.makeText(this, "회원 정보가 존재하지 않습니다.", Toast.LENGTH_SHORT).show()
}
Expand All @@ -59,8 +64,30 @@ class LoginActivity: AppCompatActivity() {
editor.apply()
}

private fun saveJwt2(jwt: String){
val spf = getSharedPreferences("auth2", MODE_PRIVATE)
val editor = spf.edit()

editor.putString("jwt", jwt)
editor.apply()
}

private fun startMainActivity(){
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}

override fun onLoginSuccess(code: String, data: LoginData) {
Log.d("LOGIN","code:${code}")
when(code){
"COMMON200_1"->{
saveJwt2(data.accessToken)
startMainActivity()
}
}
}

override fun onLoginFailure() {
// 로그인 실패 처리
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.rkdgudrn4094.week2

interface LoginView {
fun onLoginSuccess(code: String, data: LoginData)
fun onLoginFailure()
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.gson.Gson
import com.rkdgudrn4094.week2.databinding.ActivityMainBinding
//import kotlin.jvm.java

class MainActivity : AppCompatActivity(), HomeFragmentDataListener {
class MainActivity : AppCompatActivity(), HomeFragmentDataListener, TestView {
private val getResultText = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
result ->
result ->
if (result.resultCode == Activity.RESULT_OK){
val title = result.data?.getStringExtra("title")
val singer = result.data?.getStringExtra("singer")
Expand All @@ -41,23 +38,7 @@ class MainActivity : AppCompatActivity(), HomeFragmentDataListener {
inputDummySongs()
inputDummyAlbums()

//val song = Song(binding.mainMiniplayerTitleTv.text.toString(), binding.mainMiniplayerSingerTv.text.toString(), 0, 60, false, "music_lilac")

binding.mainPlayerCl.setOnClickListener {

/*
Log.d("Activity", "MainActivity, song.second:${song.second}")
val intent = Intent(this, SongActivity::class.java)
intent.putExtra("title", song.title)
intent.putExtra("singer", song.singer)
intent.putExtra("second", song.second)
intent.putExtra("playTime", song.playTime)
intent.putExtra("isPlaying", song.isPlaying)
intent.putExtra("music", song.music)
getResultText.launch(intent)

*/

val editor = getSharedPreferences("song", MODE_PRIVATE).edit()
editor.putInt("songId", song.id)
editor.apply()
Expand All @@ -66,7 +47,13 @@ class MainActivity : AppCompatActivity(), HomeFragmentDataListener {
startActivity(intent)
}

binding.mainTokenTestTv.setOnClickListener {
testToken()
}

initBottomNavigation()

Log.d("MAIN/JWT_TO_SERVER", getJwt().toString())
}

private fun initBottomNavigation(){
Expand Down Expand Up @@ -161,6 +148,28 @@ class MainActivity : AppCompatActivity(), HomeFragmentDataListener {
Log.d("OnStart song ID", song.second.toString())
}

private fun getJwt(): String?{
val spf = this.getSharedPreferences("auth2", AppCompatActivity.MODE_PRIVATE)
return spf!!.getString("jwt", "")
}

override fun onTestSuccess(message: String) {
Toast.makeText(this, "msg: ${message}", Toast.LENGTH_LONG).show()
}

override fun onTestFailure() {
//테스트 실패 코드
}

private fun testToken(){
val authService = AuthService()
authService.setTestView(this)

val token: String=getJwt()!!
Log.d("MAIN", "getJwt: ${token}")

authService.test(token)
}


private fun inputDummySongs(){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.rkdgudrn4094.week2

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

const val BASE_URL = "http://43.200.73.115:8080/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

작성하신 BASE_URL을 보니 IP 주소를 상수에 그대로 적으셨는데, 프로젝트를 할 때는 보통 저렇게 하지 않습니다. Git 같은 곳에 올릴 때 서버가 노출되면 보안상 좋지 않기 때문에 보통은 local.properties라는 파일에 숨겨두고, build.gradle을 통해 불러오는 방식을 씁니다! 나중에 한번 찾아보시면 좋을 것 같아요~


fun getRetrofit(): Retrofit{
val retrofit = Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).build()

return retrofit
}
/*
class NetworkModule {
}*/
Loading