Skip to content

Commit

Permalink
Merge pull request #7 from NOW-SOPT-ANDROID/feat/week-xml2
Browse files Browse the repository at this point in the history
[Week2] XML 필수 과제
  • Loading branch information
arinming authored May 2, 2024
2 parents e373996 + 98b6573 commit 6ce662f
Show file tree
Hide file tree
Showing 39 changed files with 714 additions and 40 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,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'

implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'

implementation 'androidx.fragment:fragment-ktx:1.6.2'
implementation 'androidx.activity:activity-ktx:1.8.2'
}
7 changes: 3 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@
android:theme="@style/Theme.NOWSOPTAndroid"
tools:targetApi="31">
<activity
android:name=".LoginActivity"
android:name=".ui.login.LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:name=".ui.main.MainActivity"
android:exported="false" />
<activity
android:name=".SignUpActivity"
android:name=".ui.signUp.SignUpActivity"
android:exported="false" />
</application>

Expand Down
31 changes: 0 additions & 31 deletions app/src/main/java/com/sopt/now/MainActivity.kt

This file was deleted.

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

import androidx.annotation.DrawableRes

sealed class ItemData {

data class MyProfile(
@DrawableRes val profileImage: Int?,
val name: String,
val description: String?,
) : ItemData()


data class Friend(
@DrawableRes val profileImage: Int?,
val name: String,
val description: String?,
) : ItemData()


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

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.sopt.now.data.ItemData
import com.sopt.now.databinding.ItemFriendBinding
import com.sopt.now.databinding.ItemMyProfileBinding

class ItemAdapter(private val items: MutableList<ItemData>) : RecyclerView.Adapter<ViewHolder>() {

companion object {
private const val VIEW_TYPE_MY_PROFILE = 0
private const val VIEW_TYPE_FRIEND = 1
}

interface ItemClick {
fun onItemClick(view: View, position: Int)
}

private var itemClick: ItemClick? = null


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return when (viewType) {
VIEW_TYPE_MY_PROFILE -> {
val binding = ItemMyProfileBinding.inflate(inflater, parent, false)
MyProfileViewHolder(binding)
}

else -> {
val binding = ItemFriendBinding.inflate(inflater, parent, false)
FriendViewHolder(binding)
}
}
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
when (val item = items[position]) {
is ItemData.MyProfile -> {
(holder as MyProfileViewHolder).profileImage.setImageResource(item.profileImage!!)
holder.name.text = item.name
holder.description.text = item.description
}

is ItemData.Friend -> {
(holder as FriendViewHolder).profileImage.setImageResource(item.profileImage!!)
holder.name.text = item.name
holder.description.text = item.description
holder.itemView.setOnClickListener {
itemClick?.onItemClick(it, position)
}
}
}
}


override fun getItemCount(): Int {
return items.size
}

override fun getItemViewType(position: Int): Int {
return when (items[position]) {
is ItemData.MyProfile -> VIEW_TYPE_MY_PROFILE
is ItemData.Friend -> VIEW_TYPE_FRIEND
}
}

private class MyProfileViewHolder(binding: ItemMyProfileBinding) :
ViewHolder(binding.root) {

val profileImage = binding.ivProfile
val name = binding.tvName
val description = binding.tvDescription
}

private class FriendViewHolder(binding: ItemFriendBinding) :
ViewHolder(binding.root) {

val profileImage = binding.ivProfile
val name = binding.tvName
val description = binding.tvDescription

}
}

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

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.sopt.now.data.ItemData
import com.sopt.now.databinding.FragmentHomeBinding
import com.sopt.now.ui.adapter.ItemAdapter
import com.sopt.now.ui.home.viewModel.HomeViewModel

class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
private val binding
get() = requireNotNull(_binding) {
"바인딩 객체를 생성하지 않음"
}

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 items = viewModel.friendList
val friendAdapter = ItemAdapter(items)
binding.rvFriends.adapter = friendAdapter
setFriendList(items)
}

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

private fun setFriendList(friendList: List<ItemData>) {
friendList.toList()
}

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

import androidx.lifecycle.ViewModel
import com.sopt.now.R
import com.sopt.now.data.ItemData

class HomeViewModel : ViewModel() {
val friendList = mutableListOf(

ItemData.MyProfile(
profileImage = R.drawable.img_arin,
name = "김아린",
description = "업보 청산 중..",
),
ItemData.Friend(
profileImage = R.drawable.img_profile0,
name = "이의경",
description = "김아린 과제 벼락치기 하지 마라",
),
ItemData.Friend(
profileImage = R.drawable.img_profile1,
name = "최준서",
description = "오운완 ㅋㅋ",
),
ItemData.Friend(
profileImage = R.drawable.img_profile2,
name = "이연진",
description = "아리니 넘 기엽당..",
),
ItemData.Friend(
profileImage = R.drawable.img_profile3,
name = "손민재",
description = "점심 뭐 먹지?",
),
ItemData.Friend(
profileImage = R.drawable.img_profile4,
name = "홍해인",
description = "난 눈물의 여왕이야",
),
ItemData.Friend(
profileImage = R.drawable.img_profile5,
name = "백현우",
description = "눈물의여왕시작하지말걸공부가안된다",
),
ItemData.Friend(
profileImage = R.drawable.img_profile6,
name = "이서경",
description = "저는 환연 과몰입러예요",
),
ItemData.Friend(
profileImage = R.drawable.img_profile7,
name = "이주원",
description = "너가 자기야 미안해 했잖아?",
),
ItemData.Friend(
profileImage = R.drawable.img_profile8,
name = "김광태",
description = "내일 뭐 해?",
),
ItemData.Friend(
profileImage = R.drawable.img_profile9,
name = "정현규",
description = "내봬누",
),
ItemData.Friend(
profileImage = R.drawable.img_profile10,
name = "성해은",
description = "벌써 스물 아홉이야",
),
ItemData.Friend(
profileImage = R.drawable.img_profile11,
name = "정규민",
description = "오마카세 사줄게",
),
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sopt.now
package com.sopt.now.ui.login

import android.app.Activity
import android.content.Intent
Expand All @@ -10,6 +10,8 @@ import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AppCompatActivity
import com.sopt.now.databinding.ActivityLoginBinding
import com.sopt.now.ui.main.MainActivity
import com.sopt.now.ui.signUp.SignUpActivity

class LoginActivity : AppCompatActivity() {
private lateinit var binding: ActivityLoginBinding
Expand Down
Loading

0 comments on commit 6ce662f

Please sign in to comment.