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/week xml07 #19

Open
wants to merge 5 commits into
base: develop-xml
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
11 changes: 7 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id 'kotlin-parcelize'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.0' // build.gradle projectμ—μ„œ 확인 κ°€λŠ₯
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
}

// plugin{}κ³Ό andriod{} 사이
Expand Down Expand Up @@ -43,6 +44,9 @@ android {
viewBinding true
buildConfig true // build ν•  λ•Œ buildConfig 파일이 λ§Œλ“€μ–΄μ§ -> 망치 이미지(build) λˆ„λ₯΄κΈ°!!
}
kapt {
correctErrorTypes true
}
}

dependencies {
Expand Down Expand Up @@ -75,14 +79,13 @@ dependencies {
implementation("com.squareup.okhttp3:okhttp")
implementation("com.squareup.okhttp3:logging-interceptor")

// dagger hilt
implementation "com.google.dagger:hilt-android:2.48.1"
kapt "com.google.dagger:hilt-compiler:2.48.1"

// viewmodel_ktx
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"

// coroutine
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'

// dagger hilt
implementation "com.google.dagger:hilt-android:2.51"
kapt "com.google.dagger:hilt-compiler:2.51"
Comment on lines +88 to +90

Choose a reason for hiding this comment

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

ν‚€μ•Ό.. λ―“μ°Œλ‹€..

}
2 changes: 2 additions & 0 deletions app/src/main/java/com/sopt/now/ApplicationClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.sopt.now
import android.app.Application
import com.sopt.now.ApplicationClass.SharedPreferences.editor
import com.sopt.now.ApplicationClass.SharedPreferences.sSharedPreferences
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class ApplicationClass: Application() {
object SharedPreferences {
lateinit var sSharedPreferences: android.content.SharedPreferences
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/com/sopt/now/config/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.sopt.now.config

import android.os.Bundle
import android.os.PersistableBundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding

abstract class BaseActivity<B : ViewBinding>(private val inflate: (LayoutInflater) -> B) :
AppCompatActivity() {
protected lateinit var binding: B


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = inflate(layoutInflater).apply {
setContentView(root)
}
}
}
31 changes: 31 additions & 0 deletions app/src/main/java/com/sopt/now/config/BaseFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.sopt.now.config

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding

abstract class BaseFragment<B : ViewBinding>(
private val bind: (View) -> B,
@LayoutRes layoutResId: Int
) : Fragment(layoutResId) {
private var _binding: B? = null
protected val binding get() = requireNotNull(_binding)

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = super.onCreateView(inflater, container, savedInstanceState)?.let { bind(it) }
return binding.root
}

override fun onDestroyView() {
_binding = null
super.onDestroyView()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sopt.now.utils
package com.sopt.now.config

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sopt.now.utils
package com.sopt.now.config

import com.sopt.now.ApplicationClass.SharedPreferences.sSharedPreferences
import com.sopt.now.utils.Constants.Companion.MEMBER_ID
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/com/sopt/now/data/AuthRepositoryImpl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.sopt.now.data

import com.sopt.now.datasource.AuthService
import com.sopt.now.model.login.RequestLoginDto
import com.sopt.now.model.signup.RequestSignUpDto
import com.sopt.now.repository.AuthRepository
import com.sopt.now.config.BaseResponse
import retrofit2.Response

class AuthRepositoryImpl(
private val authService: AuthService
): AuthRepository {
override suspend fun loginUser(data: RequestLoginDto): Response<BaseResponse<Unit>> {
return authService.login(data)
}

override suspend fun signupUser(data: RequestSignUpDto): Response<BaseResponse<Unit>> {
return authService.signUp(data)
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/com/sopt/now/data/MyPageRepositoryImpl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.sopt.now.data

import com.sopt.now.datasource.InfoService
import com.sopt.now.model.info.UserInfo
import com.sopt.now.repository.MyPageRepository
import com.sopt.now.config.BaseResponse
import retrofit2.Response

class MyPageRepositoryImpl(
private val infoService: InfoService
) : MyPageRepository {
override suspend fun getUserInfo(): Response<BaseResponse<UserInfo>> {
return infoService.getUserInfo()
}

}
2 changes: 1 addition & 1 deletion app/src/main/java/com/sopt/now/datasource/AuthService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.sopt.now.datasource

import com.sopt.now.model.login.RequestLoginDto
import com.sopt.now.model.signup.RequestSignUpDto
import com.sopt.now.utils.BaseResponse
import com.sopt.now.config.BaseResponse
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.POST
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/sopt/now/datasource/InfoService.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sopt.now.datasource

import com.sopt.now.model.info.UserInfo
import com.sopt.now.utils.BaseResponse
import com.sopt.now.config.BaseResponse
import retrofit2.Response
import retrofit2.http.GET

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@ package com.sopt.now.presentation.auth.login

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.sopt.now.ApplicationClass.SharedPreferences.sSharedPreferences
import com.sopt.now.config.BaseActivity
import com.sopt.now.databinding.ActivityLoginBinding
import com.sopt.now.model.login.RequestLoginDto
import com.sopt.now.presentation.auth.signup.SignupActivity
import com.sopt.now.presentation.main.MainActivity
import com.sopt.now.utils.Constants.Companion.MEMBER_ID
import com.sopt.now.utils.UiState
import com.sopt.now.utils.showToast
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

class LoginActivity : AppCompatActivity() {
private lateinit var binding: ActivityLoginBinding
private val loginViewModel by viewModels<LoginViewModel>()
@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding>(ActivityLoginBinding::inflate) {
private val loginViewModel: LoginViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityLoginBinding.inflate(layoutInflater).apply { setContentView(root) }

with(binding) {
btnSignup.setOnClickListener { navigateToSignup() }
btnLogin.setOnClickListener { login() }
Expand Down
Copy link
Member

Choose a reason for hiding this comment

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

ν˜„μž¬ λ·°λͺ¨λΈμ˜ μ„œλ²„ 톡신 λ‘œμ§λ“€μ€ uiλ‹¨μ˜ λ·°λͺ¨λΈμ΄ μ•„λ‹Œ dataλ‹¨μ˜ repostiory implμ—μ„œ μˆ˜ν–‰ν•˜λŠ”κ²Œ 더 쒋을 것 κ°™μ•„μš”!

Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
package com.sopt.now.presentation.auth.login

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopt.now.ApplicationClass.SharedPreferences.editor
import com.sopt.now.model.login.RequestLoginDto
import com.sopt.now.repository.AuthRepository
import com.sopt.now.utils.Constants.Companion.MEMBER_ID
import com.sopt.now.utils.NetworkUtil
import com.sopt.now.utils.ServicePool.loginService
import com.sopt.now.utils.UiState
import dagger.hilt.android.HiltAndroidApp
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

class LoginViewModel : ViewModel() {
@HiltViewModel
class LoginViewModel @Inject constructor(
private val authRepository: AuthRepository
) : ViewModel() {
private val _state = MutableStateFlow<UiState<Unit>>(UiState.LOADING)
val state get() = _state.asStateFlow()
Comment on lines 23 to 24

Choose a reason for hiding this comment

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

μ‚¬μ†Œν•˜μ§€λ§Œ λ³€μˆ˜μ˜ νƒ€μž…μ„ λͺ…μ‹œμ μœΌλ‘œ μ μ–΄μ£ΌλŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€!


fun postLogin(data: RequestLoginDto) {
viewModelScope.launch(Dispatchers.IO) {
_state.value = UiState.LOADING
runCatching {
loginService.login(data)
authRepository.loginUser(data)
}.onSuccess {
if (it.isSuccessful) {
editor.putString(MEMBER_ID, it.headers()["location"])
Expand All @@ -33,12 +36,14 @@ class LoginViewModel : ViewModel() {
_state.value = UiState.SUCCESS(null)
} else {
_state.value = UiState.FAILURE(
it.errorBody()?.let { e -> NetworkUtil.getErrorResponse(e)?.message }.toString()
it.errorBody()?.let { e -> NetworkUtil.getErrorResponse(e)?.message }
.toString()
)
}
}.onFailure {
it.printStackTrace()
}

}
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package com.sopt.now.presentation.auth.signup

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopt.now.model.signup.RequestSignUpDto
import com.sopt.now.repository.AuthRepository
import com.sopt.now.utils.NetworkUtil
import com.sopt.now.utils.ServicePool.authService
import com.sopt.now.utils.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

class SignUpViewModel : ViewModel() {
@HiltViewModel
class SignUpViewModel @Inject constructor(
private val authRepository: AuthRepository
) : ViewModel() {
private val _state = MutableStateFlow<UiState<Unit>>(UiState.LOADING)
val state = _state.asStateFlow()
val state get() = _state.asStateFlow()

fun signUp(data: RequestSignUpDto) {
viewModelScope.launch(Dispatchers.IO) {
_state.value = UiState.LOADING
runCatching {
authService.signUp(data)
authRepository.signupUser(data)
}.onSuccess {
if (it.isSuccessful) _state.value = UiState.SUCCESS(null)
else {
Comment on lines +16 to 30

Choose a reason for hiding this comment

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

μ•„ μ§„μ§œ λ©‹μžˆλ‹€..이 μ–Έλ‹ˆ

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.sopt.now.R
import com.sopt.now.config.BaseActivity
import com.sopt.now.databinding.ActivitySignupBinding
import com.sopt.now.model.signup.RequestSignUpDto
import com.sopt.now.utils.UiState
import com.sopt.now.utils.showToast
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

class SignupActivity : AppCompatActivity() {
private lateinit var binding: ActivitySignupBinding
private val signUpViewModel by viewModels<SignUpViewModel>()
@AndroidEntryPoint
class SignupActivity : BaseActivity<ActivitySignupBinding>(ActivitySignupBinding::inflate) {
private val signUpViewModel: SignUpViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivitySignupBinding.inflate(layoutInflater).apply { setContentView(root) }
binding.btnSignup.setOnClickListener { signUp() }
observeSignUp()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import com.sopt.now.databinding.ActivityMainBinding
import com.sopt.now.presentation.main.home.HomeFragment
import com.sopt.now.presentation.main.mypage.MyPageFragment
import com.sopt.now.presentation.main.search.SearchFragment
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.sopt.now.R
import com.sopt.now.config.BaseFragment
import com.sopt.now.databinding.FragmentHomeBinding

class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
private val binding get() = requireNotNull(_binding)
class HomeFragment : BaseFragment<FragmentHomeBinding>(FragmentHomeBinding::bind, R.layout.fragment_home) {
private val viewModel by viewModels<HomeViewModel>()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentHomeBinding.inflate(inflater, container, false)
return binding.root
}
private lateinit var profileAdapter: ProfileAdapter

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand All @@ -29,13 +21,9 @@ class HomeFragment : Fragment() {
}

private fun initRecyclerView() {
val friendAdapter = ProfileAdapter()
binding.rvFriend.adapter = friendAdapter
friendAdapter.setFriendList(viewModel.mockFriendList)
}
profileAdapter = ProfileAdapter(requireContext())
binding.rvFriend.adapter = profileAdapter
profileAdapter.submitList(viewModel.mockFriendList)

override fun onDestroyView() {
_binding = null
super.onDestroyView()
}
}
}
Loading