Skip to content

Commit

Permalink
#7 이것저것
Browse files Browse the repository at this point in the history
  • Loading branch information
2zerozu committed Oct 11, 2022
1 parent 31fc585 commit bafb8e5
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,47 +1,19 @@
package org.sopt.sample.presentation.home

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import dagger.hilt.android.AndroidEntryPoint
import org.sopt.sample.R
import org.sopt.sample.databinding.ActivityMainBinding
import org.sopt.sample.entity.User
import org.sopt.sample.presentation.home.gallery.GalleryFragment
import org.sopt.sample.presentation.home.home.HomeFragment
import org.sopt.sample.presentation.home.search.SearchFragment
import org.sopt.sample.presentation.login.SignInActivity
import org.sopt.sample.util.base.BaseActivity
import org.sopt.sample.util.extensions.showToast

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {
private val homeViewModel: MainViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.vm = homeViewModel
getUserInfo()
observeLogout()
}

private fun getUserInfo() {
val userInfo = intent.getSerializableExtra("userInfo") as User
homeViewModel.id.value = getString(R.string.home_expressed_name, userInfo.id)
homeViewModel.mbti.value = getString(R.string.home_expressed_mbti, userInfo.mbti)
}

private fun observeLogout() {
homeViewModel.logout.observe(this) { logout ->
if (logout) {
showToast(getString(R.string.success_logout))
val toLogin = Intent(this, SignInActivity::class.java)
startActivity(toLogin)
finish()
}
}
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initTransactionEvent()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import org.sopt.sample.databinding.ItemHomeBodyBinding
import org.sopt.sample.databinding.ItemHomeHeaderBinding
import org.sopt.sample.entity.RepoData

class HomeAdapter : ListAdapter<RepoData, RecyclerView.ViewHolder>(homeDiffUtil) {
class HomeAdapter() : ListAdapter<RepoData, RecyclerView.ViewHolder>(homeDiffUtil) {

private lateinit var itemHomeHeaderBinding: ItemHomeHeaderBinding
private lateinit var itemHomeBodyBinding: ItemHomeBodyBinding

Expand Down Expand Up @@ -50,23 +51,24 @@ class HomeAdapter : ListAdapter<RepoData, RecyclerView.ViewHolder>(homeDiffUtil)
if (holder is BodyViewHolder) {
holder.onBind(getItem(position))
} else if (holder is HeaderViewHolder) {
holder
holder.onBind()
}
}

class BodyViewHolder(
private val binding: ItemHomeBodyBinding
) : RecyclerView.ViewHolder(binding.root) {
fun onBind(data: RepoData) {
binding.ivItemHomeProfile.setImageResource(data.img)
binding.tvItemHomeRepo.text = data.repo
binding.tvItemHomeName.text = data.name
binding.data = data
}
}

class HeaderViewHolder(
private val binding: ItemHomeHeaderBinding
) : RecyclerView.ViewHolder(binding.root)
) : RecyclerView.ViewHolder(binding.root) {
fun onBind() {
}
}

companion object {
private val homeDiffUtil = object : DiffUtil.ItemCallback<RepoData>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
package org.sopt.sample.presentation.home.home

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.activityViewModels
import org.sopt.sample.R
import org.sopt.sample.databinding.FragmentHomeBinding
import org.sopt.sample.entity.RepoData
import org.sopt.sample.entity.User
import org.sopt.sample.presentation.login.SignInActivity
import org.sopt.sample.util.base.BaseFragment
import org.sopt.sample.util.extensions.showToast

class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home) {
private val homeViewModel: HomeViewModel by activityViewModels()
private lateinit var homeAdapter: HomeAdapter

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
getUserInfo()
initAdapter()
}

private fun getUserInfo() {
val userInfo = requireActivity().intent.getSerializableExtra("userInfo") as User
homeViewModel.id.value = getString(R.string.home_expressed_name, userInfo.id)
homeViewModel.mbti.value = getString(R.string.home_expressed_mbti, userInfo.mbti)
}

private fun onClickLogout() {
homeViewModel.logout.observe(viewLifecycleOwner) { logout ->
if (logout) {
requireActivity().showToast(getString(R.string.success_logout))
val toLogin = Intent(requireActivity(), SignInActivity::class.java)
startActivity(toLogin)
requireActivity().finish()
Log.d("asdf", "$logout")
}
}
}

private fun initAdapter() {
homeAdapter = HomeAdapter()
binding.rvHome.adapter = homeAdapter
Expand All @@ -24,13 +50,14 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home) {
private fun initRepoData() {
homeAdapter.submitList(
listOf(
RepoData(R.drawable.img_profile, "Android", "Yeongju Lee"),
RepoData(R.drawable.img_profile, "Server", "Daehwan Gye"),
RepoData(R.drawable.img_profile, "IOS", "Hajeong Kim"),
RepoData(R.drawable.img_profile, "Design", "Jieun Kim"),
RepoData(R.drawable.img_profile, "Plan", "Nunu Lee"),
RepoData(R.drawable.img_profile, "Android", "Yeongju Lee"),
RepoData(R.drawable.img_profile, "Server", "Daehwan Gye"),
RepoData(0, "", ""),
RepoData(R.drawable.img_jandi, "Android", "Yeongju Lee"),
RepoData(R.drawable.img_jandi, "Server", "Daehwan Gye"),
RepoData(R.drawable.img_jandi, "IOS", "Hajeong Kim"),
RepoData(R.drawable.img_jandi, "Design", "Jieun Kim"),
RepoData(R.drawable.img_jandi, "Plan", "Nunu Lee"),
RepoData(R.drawable.img_jandi, "Android", "Yeongju Lee"),
RepoData(R.drawable.img_jandi, "Server", "Daehwan Gye"),
RepoData(R.drawable.img_profile, "IOS", "Hajeong Kim"),
RepoData(R.drawable.img_profile, "Design", "Jieun Kim"),
RepoData(R.drawable.img_profile, "Plan", "Nunu Lee"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sopt.sample.presentation.home
package org.sopt.sample.presentation.home.home

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
Expand All @@ -11,7 +11,7 @@ import org.sopt.sample.util.SaveUserInfo
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(
class HomeViewModel @Inject constructor(
private val saveUserInfo: SaveUserInfo
) : ViewModel() {
val id = MutableLiveData<String>()
Expand Down
Binary file added app/src/main/res/drawable/img_jandi.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 0 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

<data>

<variable
name="vm"
type="org.sopt.sample.presentation.home.MainViewModel" />

</data>

<androidx.constraintlayout.widget.ConstraintLayout
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

<data>

<variable
name="vm"
type="org.sopt.sample.presentation.home.home.HomeViewModel" />

</data>

<FrameLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".presentation.home.home.HomeFragment">
android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_home"
Expand All @@ -19,5 +22,5 @@
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_home_body" />

</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
8 changes: 4 additions & 4 deletions app/src/main/res/layout/item_home_body.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<data>

<variable
name="vm"
name="data"
type="org.sopt.sample.entity.RepoData" />

</data>
Expand All @@ -20,7 +20,7 @@
android:id="@+id/iv_item_home_profile"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@{vm.img}"
android:src="@{data.img}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -34,7 +34,7 @@
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:maxLines="1"
android:text="@{vm.repo}"
android:text="@{data.repo}"
android:textColor="@color/black"
app:layout_constraintBottom_toTopOf="@id/tv_item_home_name"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -47,7 +47,7 @@
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:maxLines="1"
android:text="@{vm.name}"
android:text="@{data.name}"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
73 changes: 63 additions & 10 deletions app/src/main/res/layout/item_home_header.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
xmlns:tools="http://schemas.android.com/tools">

<TextView
android:id="@+id/tv_home_name"
<data>

<variable
name="vm"
type="org.sopt.sample.presentation.home.home.HomeViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/header_title"
app:layout_constraintTop_toTopOf="parent" />
android:background="@color/black"
app:layout_constraintTop_toTopOf="parent">

<Button
android:id="@+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:backgroundTint="@color/white"
android:text="@string/logout"
android:onClickListener="@{()->vm.logoutOnClick()}"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iv_home_profile"
android:layout_width="120dp"
android:layout_height="0dp"
android:layout_marginTop="50dp"
android:src="@drawable/img_profile"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_home_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@{vm.id}"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_home_profile"
tools:text="ID : 2zerozu" />

<TextView
android:id="@+id/tv_home_mbti"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="50dp"
android:text="@{vm.mbti}"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_home_name"
tools:text="MBTI : ISFP" />

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<string name="fail_login">다시 입력하세요.</string>
<string name="success_logout">로그아웃 되었습니다.</string>
<string name="auto_login">자동로그인</string>
<string name="logout">Logout</string>

<string name="home_expressed_name">이름 : %s</string>
<string name="home_expressed_mbti">MBTI : %s</string>
Expand Down

0 comments on commit bafb8e5

Please sign in to comment.