Skip to content

Commit

Permalink
Merge pull request #7 from NOW-SOPT-ANDROID/feat/#5-week2_xml
Browse files Browse the repository at this point in the history
Feat/#5 week2 xml 필수과제 구현
  • Loading branch information
hyeeum authored May 1, 2024
2 parents e8e690f + 9d4ddfb commit 3598df2
Show file tree
Hide file tree
Showing 25 changed files with 668 additions and 66 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Now Sopt Android
feat/#5-week2_xml

- **[FEAT]** : 새로운 기능 구현
- **[MOD]** : 코드 수정 및 내부 파일 수정
Expand Down
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

//viewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
implementation "androidx.fragment:fragment-ktx:1.6.1"
implementation "androidx.activity:activity-ktx:1.8.0"
}
41 changes: 41 additions & 0 deletions app/src/main/java/com/sopt/now/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.sopt.now

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.sopt.now.databinding.FragmentHomeBinding

class HomeFragment : Fragment() {
private val binding:FragmentHomeBinding
get()= requireNotNull(_binding){"_binding이 null이 아닌 경우만 _binding 반환"}
private var _binding: FragmentHomeBinding ?= null
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
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val homeListAdapter = HomeListAdapter()
binding.rvFriends.run {
adapter = homeListAdapter
layoutManager = LinearLayoutManager(requireContext())
}
homeListAdapter.setHomeList(viewModel.homeListData)
}

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

}
15 changes: 15 additions & 0 deletions app/src/main/java/com/sopt/now/HomeList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.sopt.now

import androidx.annotation.DrawableRes

data class HomeList(
@DrawableRes val profileImage: Int,
val name: String,
val selfDescription: String,
val viewType: Int
) {
companion object {
const val VIEW_TYPE_USER = 0
const val VIEW_TYPE_FRIEND = 1
}
}
44 changes: 44 additions & 0 deletions app/src/main/java/com/sopt/now/HomeListAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.sopt.now

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.sopt.now.databinding.ItemFriendBinding
import com.sopt.now.databinding.ItemUserBinding

class HomeListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var homeListList: List<HomeList> = emptyList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val bindingUser = ItemUserBinding.inflate(inflater, parent, false)
val bindingFriend = ItemFriendBinding.inflate(inflater, parent, false)
return when(viewType){
HomeList.VIEW_TYPE_USER -> {
UserViewHolder(bindingUser)
}
else -> {
FriendViewHolder(bindingFriend)
}
}
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when(homeListList.getOrNull(position)?.viewType){
HomeList.VIEW_TYPE_USER -> {
(holder as UserViewHolder).onBind(homeListList[position])
}
else -> {
(holder as FriendViewHolder).onBind(homeListList[position])
}
}
}

override fun getItemCount() = homeListList.size
override fun getItemViewType(position: Int): Int {
return homeListList[position].viewType
}
fun setHomeList(homeListList: List<HomeList>) {
this.homeListList = homeListList.toList()
notifyDataSetChanged()
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/com/sopt/now/HomeListViewHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.sopt.now

import androidx.recyclerview.widget.RecyclerView
import com.sopt.now.databinding.ItemFriendBinding
import com.sopt.now.databinding.ItemUserBinding

class FriendViewHolder(private val binding:ItemFriendBinding) : RecyclerView.ViewHolder(binding.root) {
fun onBind(friendData: HomeList) {
binding.run {
ivProfile.setImageResource(friendData.profileImage)
tvName.text = friendData.name
tvSelfDescription.text = friendData.selfDescription
}
}
}

class UserViewHolder(private val binding:ItemUserBinding) : RecyclerView.ViewHolder(binding.root) {
fun onBind(friendData: HomeList) {
binding.run {
ivProfile.setImageResource(friendData.profileImage)
tvName.text = friendData.name
tvSelfDescription.text = friendData.selfDescription
}
}
}
86 changes: 86 additions & 0 deletions app/src/main/java/com/sopt/now/HomeViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.sopt.now

import androidx.lifecycle.ViewModel

class HomeViewModel : ViewModel() {
val homeListData = listOf(
HomeList(
profileImage = R.drawable.main,
name = "송혜음",
selfDescription = "멀티 뷰 리싸이클러뷰!",
HomeList.VIEW_TYPE_USER
),
HomeList(
profileImage = R.drawable.main,
name = "박동민",
selfDescription = "곽의진...얼굴 재치 실력 모든걸 다 가진 남자... 하지만 밀양박씨 36대손인 나 박동민은 가지지 못했지",
HomeList.VIEW_TYPE_FRIEND
),
HomeList(
profileImage = R.drawable.main,
name = "이석준",
selfDescription = "죄송합니다 저 도핑했습니다... 안드-로이더 \uD83D\uDC89",
HomeList.VIEW_TYPE_FRIEND
),
HomeList(
profileImage = R.drawable.main,
name = "박유진",
selfDescription = "(ง˙∇˙)ว 에라 모르겠다",
HomeList.VIEW_TYPE_FRIEND
),
HomeList(
profileImage = R.drawable.main,
name = "이의경",
selfDescription = "다들 빨리 끝내고 뒤풀이 가고 싶지? ㅎㅎ 아직 반도 안왔어 ^&^",
HomeList.VIEW_TYPE_FRIEND
),
HomeList(
profileImage = R.drawable.main,
name = "우상욱",
selfDescription = "나보다 안드 잘하는 사람 있으면 나와봐",
HomeList.VIEW_TYPE_FRIEND
),
HomeList(
profileImage = R.drawable.main,
name = "배지현",
selfDescription = "표정 풀자 ^^",
HomeList.VIEW_TYPE_FRIEND
),
HomeList(
profileImage = R.drawable.main,
name = "이의경",
selfDescription = "다들 빨리 끝내고 뒤풀이 가고 싶지? ㅎㅎ 아직 반도 안왔어 ^&^",
HomeList.VIEW_TYPE_FRIEND
),
HomeList(
profileImage = R.drawable.main,
name = "우상욱",
selfDescription = "나보다 안드 잘하는 사람 있으면 나와봐",
HomeList.VIEW_TYPE_FRIEND
),
HomeList(
profileImage = R.drawable.main,
name = "배지현",
selfDescription = "표정 풀자 ^^",
HomeList.VIEW_TYPE_FRIEND
),
HomeList(
profileImage = R.drawable.main,
name = "이의경",
selfDescription = "다들 빨리 끝내고 뒤풀이 가고 싶지? ㅎㅎ 아직 반도 안왔어 ^&^",
HomeList.VIEW_TYPE_FRIEND
),
HomeList(
profileImage = R.drawable.main,
name = "우상욱",
selfDescription = "나보다 안드 잘하는 사람 있으면 나와봐",
HomeList.VIEW_TYPE_FRIEND
),
HomeList(
profileImage = R.drawable.main,
name = "배지현",
selfDescription = "표정 풀자 ^^",
HomeList.VIEW_TYPE_FRIEND
)
)
}
48 changes: 28 additions & 20 deletions app/src/main/java/com/sopt/now/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,65 @@ class LoginActivity : AppCompatActivity() {
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)

//회원가입에서 사용자 정보 받아옴
getUser()
getUserInfo()

//회원가입 페이지로 넘어가기
moveToSignUp()

}
private fun getUser() { // 아쉬운 부분
private fun getUserInfo() {
var id = ""
var pw = ""
var nick = ""
resultLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK) {
id = result.data?.getStringExtra("id") ?: ""
pw = result.data?.getStringExtra("pw") ?: ""
nick = result.data?.getStringExtra("nick") ?: ""
result.data?.let { data ->
id = data.getStringExtra("id") ?: ""
pw = data.getStringExtra("pw") ?: ""
nick = data.getStringExtra("nick") ?: ""
}
}
}
binding.btnLogin.setOnClickListener {
sendData(id,pw,nick)
moveToMain(id,pw,nick)
}
}
private fun moveToSignUp(){
binding.btnLoginSignIn.setOnClickListener {
val intent = Intent(this, SignUpActivity::class.java)
//회원가입 데이터를 받아오기 위해 startActivity가 아닌 resultLauncher사용
resultLauncher.launch(intent)
}
}
private fun moveToMain(id:String,pw:String,nick:String){
if (isLoginAvailable(id, pw)) {
val intent = Intent(this, MainActivity::class.java).apply {
saveUserInfo(id, pw, nick)
}
startActivity(intent)
}
}
private fun saveUserInfo(id:String,pw:String,nick:String) {
val sharedPreferences = getSharedPreferences("userInfo", MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor
.putString("userId", id)
.putString("userPw", pw)
.putString("userNick", nick)
.apply()
}
private fun isLoginAvailable(id: String, pw: String) :Boolean {
var loginBool = false
val userId = binding.etvLoginId.text.toString()
val userPw = binding.etvLoginPw.text.toString()
val message = when{
userId != id || userPw != pw -> "아이디 혹은 비밀번호가 일치하지 않습니다."
userId.isBlank() || userPw.isBlank() -> getString(R.string.login_error_blank)
userId != id || userPw != pw -> getString(R.string.login_error_different)
else -> {
loginBool = true
"로그인에 성공했습니다."
getString(R.string.login_success)
}
}
Toast.makeText(this,message,Toast.LENGTH_SHORT).show()
return loginBool
}
private fun sendData(id:String,pw:String,nick:String){
if (isLoginAvailable(id, pw)) {
val intent = Intent(this, MainActivity::class.java)
//메인 액티비티로 데이터를 보냄
intent.putExtra("id", id).putExtra("pw", pw).putExtra("nick", nick)
startActivity(intent)
}
}
}
45 changes: 38 additions & 7 deletions app/src/main/java/com/sopt/now/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,52 @@ package com.sopt.now

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.sopt.now.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityMainBinding.inflate(layoutInflater)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

//로그인화면에서 받아온 id/pw 화면에 출력
getUserInfo()
val currentFragment = supportFragmentManager.findFragmentById(binding.fcvHome.id)
if (currentFragment == null) {
supportFragmentManager.beginTransaction()
.add(R.id.fcv_home, HomeFragment())
.commit()
}

clickBottomNavigation()
}
private fun getUserInfo() {
binding.tvMainNick.text = intent.getStringExtra("nick")
binding.tvMainId.text = intent.getStringExtra("id")
binding.tvMainPw.text = intent.getStringExtra("pw")

private fun clickBottomNavigation() {
binding.bnvHome.setOnItemSelectedListener {
when (it.itemId) {
R.id.menu_home -> {
replaceFragment(HomeFragment())
true
}

R.id.menu_search -> {
replaceFragment(SearchFragment())
true
}

R.id.menu_mypage -> {
replaceFragment(MyPageFragment())
true
}

else -> false
}
}
}

private fun replaceFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.fcv_home, fragment)
.commit()
}
}
Loading

0 comments on commit 3598df2

Please sign in to comment.