Skip to content

Commit

Permalink
[MIN-44] feat: Home화면 API 구현 및 데이터바인딩 적용(#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
KDW03 committed Feb 11, 2023
1 parent 6c59fd7 commit 66b2fd1
Show file tree
Hide file tree
Showing 20 changed files with 266 additions and 29 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/com/najudoryeong/mineme/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupWithNavController
import com.najudoryeong.mineme.common_ui.MainActivityUtil
import com.najudoryeong.mineme.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint


// 하위 모듈이 MainViewModel 코드에 접근할 수 있게 MainViewModelUtil 상속 구현
@AndroidEntryPoint
class MainActivity : AppCompatActivity(), MainActivityUtil {

private lateinit var binding: ActivityMainBinding
Expand Down Expand Up @@ -69,15 +71,14 @@ class MainActivity : AppCompatActivity(), MainActivityUtil {
binding.topAppBar.visibility = View.VISIBLE
}




/** [MainActivityUtil] */
override fun setToolbarTitle(newTitle: String) {
binding.toolbar.title = newTitle
}

override fun navigateToHome(start: Fragment) {
start.findNavController().navigate(R.id.nav_home)
showAppBar()
}

override fun setVisibilityBottomAppbar(visibilityMode: Int) {
binding.bottomAppBar.visibility = visibilityMode
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ buildscript {
kakao_version = '2.12.1'
dataStorePreferences_version = '1.0.0'
retrofit2Version = '2.9.0'

}

dependencies {
Expand Down
6 changes: 6 additions & 0 deletions common-ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ dependencies {
implementation "com.google.dagger:hilt-android:$rootProject.hiltVersion"
kapt "com.google.dagger:hilt-android-compiler:$rootProject.hiltVersion"


// Glide
implementation 'com.github.bumptech.glide:glide:4.14.2'
kapt 'com.github.bumptech.glide:compiler:4.14.2'


implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'com.google.android.material:material:1.8.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.fragment.app.Fragment

interface MainActivityUtil{
fun setToolbarTitle(newTitle: String)
fun navigateToHome(start : Fragment)
fun setVisibilityBottomAppbar(visibilityMode : Int)
fun setVisibilityTopAppBar(visibilityMode: Int)
}
41 changes: 41 additions & 0 deletions common-ui/src/main/res/drawable/img_profile.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="80dp"
android:height="80dp"
android:viewportWidth="80"
android:viewportHeight="80">
<group>
<clip-path
android:pathData="M80,40C80,62.091 62.091,80 40,80C17.909,80 0,62.091 0,40C0,17.909 17.909,0 40,0C62.091,0 80,17.909 80,40Z"/>
<path
android:pathData="M0,0h80v80h-80z"
android:fillColor="#FFDB94"/>
<path
android:pathData="M24.753,53.765C26.608,55.62 28.719,56.712 29.792,57.124C43.659,62.458 47.325,59.825 49.521,58.247C49.631,58.168 49.736,58.093 49.839,58.021C56.197,53.57 60.882,44.844 62.028,37.296C62.582,33.642 62.134,30.122 60.311,26.897C58.754,24.144 55.945,21.038 52.844,20.03C49.009,18.783 44.579,20.667 41.838,23.334C41.096,24.056 40.357,24.897 39.698,25.817C37.105,23.234 32.642,22.297 29.228,22.476C23.798,22.76 20.119,27.205 18.311,31.967C16.319,37.215 18.157,41.68 20.203,46.648C20.255,46.776 20.308,46.905 20.361,47.034C22.002,51.025 23.505,52.522 24.613,53.626C24.661,53.673 24.707,53.719 24.753,53.765ZM39.698,25.817C38.343,27.71 37.327,29.937 37.327,32.145C37.327,33.708 38.192,38.04 40.664,36.187C43.239,34.255 42.239,29.756 40.923,27.416C40.59,26.825 40.176,26.293 39.698,25.817Z"
android:fillColor="#ffffff"
android:fillType="evenOdd"/>
<path
android:strokeWidth="1"
android:pathData="M39.698,25.817C37.105,23.234 32.642,22.297 29.228,22.476C23.798,22.76 20.119,27.205 18.311,31.967C16.319,37.215 18.157,41.68 20.203,46.648C20.255,46.776 20.308,46.905 20.361,47.034C22.002,51.025 23.505,52.522 24.613,53.626C24.661,53.673 24.707,53.719 24.753,53.765C26.608,55.62 28.719,56.712 29.792,57.124C43.659,62.458 47.325,59.825 49.521,58.247C49.631,58.168 49.736,58.093 49.839,58.021C56.197,53.57 60.882,44.844 62.028,37.296C62.582,33.642 62.134,30.122 60.311,26.897C58.754,24.144 55.945,21.038 52.844,20.03C49.009,18.783 44.579,20.667 41.838,23.334C41.096,24.056 40.357,24.897 39.698,25.817ZM39.698,25.817C38.343,27.71 37.327,29.937 37.327,32.145C37.327,33.708 38.192,38.04 40.664,36.187C43.239,34.255 42.239,29.756 40.923,27.416C40.59,26.825 40.176,26.293 39.698,25.817Z"
android:fillColor="#00000000"
android:strokeColor="#000000"
android:strokeLineCap="round"/>
<path
android:strokeWidth="1"
android:pathData="M32.166,40C31.703,40.463 31.339,40.893 31,41.458"
android:fillColor="#00000000"
android:strokeColor="#000000"
android:strokeLineCap="round"/>
<path
android:strokeWidth="1"
android:pathData="M48,40C48.967,40 49.654,41.332 50.041,42.041"
android:fillColor="#00000000"
android:strokeColor="#000000"
android:strokeLineCap="round"/>
<path
android:strokeWidth="1"
android:pathData="M36,46.146C36.389,47.028 37.61,47.627 38.243,47.944C39.957,48.8 41.395,49.199 43.046,48.106C43.833,47.584 44.227,47.028 45.038,46"
android:fillColor="#00000000"
android:strokeColor="#000000"
android:strokeLineCap="round"/>
</group>
</vector>
2 changes: 2 additions & 0 deletions common-ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
<string name="app_name">mineme</string>
<string name="next">다음</string>
<string name="confirm">확인</string>
<string name="newStory">New Story</string>
<string name="noStory">No Story</string>
</resources>
2 changes: 2 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
id 'androidx.navigation.safeargs.kotlin'

}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.common.data.dto

import com.example.common.data.entity.HomeData

data class HomeResponse(
val data: HomeData,
val success: Boolean
)
57 changes: 57 additions & 0 deletions common/src/main/java/com/example/common/data/entity/HomeData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.common.data.entity

data class HomeData(
val couple: Couple,
val newStory: NewStory,
val widgets: List<Widget>
)


data class Couple(
val me: Me,
val mine: Mine,
val name: String,
val startDate: String
)


data class NewStory(
val date: String,
val postId: Int,
val region: String,
val thumbnailImage: String
)


data class Mine(
val birthday: String,
val description: String,
val gender: String,
val instaId: String,
val nickname: String,
val profileImage: String
)


data class Me(
val birthday: String,
val description: String,
val gender: String,
val instaId: String,
val nickname: String,
val profileImage: String
)


data class Widget(
val Id: Int,
val color: String,
val heigth: Int,
val name: String,
val order: Int,
val type: String,
val widget: WidgetX,
val width: Int
)

class WidgetX
17 changes: 13 additions & 4 deletions common/src/main/java/com/example/common/data/source/UserService.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
package com.example.common.data.source

import com.example.common.data.dto.CodeRequest
import com.example.common.data.dto.HomeResponse
import com.example.common.data.dto.UserInfoRequest
import com.example.common.data.dto.NullResponse
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.POST

interface UserService {

@POST("api/v1/user/info")
suspend fun postUserInfo(
@Header("Authorization") token : String,
@Header("Authorization") token: String,
@Body userInfoRequest: UserInfoRequest
) : NullResponse
): NullResponse


@POST("api/v1/user/code")
suspend fun postTargetCode(
@Header("Authorization") token : String,
@Header("Authorization") token: String,
@Body userCodeRequest: CodeRequest
) : NullResponse
): NullResponse


@GET("api/test/main/info")
suspend fun getHomeInfo(
@Header("Authorization") token: String,
): HomeResponse


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.common.domain.usecase

import com.example.common.data.dto.HomeResponse
import com.example.common.data.source.UserService
import kotlinx.coroutines.launch
import javax.inject.Inject
import javax.inject.Singleton

//todo Singletone?
@Singleton
class UserUseCase @Inject constructor(
private val userService: UserService
) {
// suspend fun getHomeData(token: String): HomeResponse {
// return userService.getHomeInfo(token)
// }
}
2 changes: 2 additions & 0 deletions home/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />

</manifest>
28 changes: 28 additions & 0 deletions home/src/main/java/com/najudoryeong/mineme/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,38 @@ package com.najudoryeong.mineme.home
import android.os.Bundle
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.activity.viewModels
import androidx.fragment.app.viewModels
import com.najudoryeong.mineme.common_ui.BaseFragment
import com.najudoryeong.mineme.common_ui.MainActivityUtil
import com.najudoryeong.mineme.home.databinding.FragmentHomeBinding
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding>(Home) {

private val viewModel: HomeViewModel by viewModels()



override fun initView() {
/*
데이터 바인딩 객체의 라이플 사이클 오너를 프래그먼트의 라이프 사이클로 지정하는 것
이렇게 해야 프래그먼트의 라이프 사이클이 끝날 때 까지 데이터 바인딩을 유지 하며 관찰 가능
이 작업을 안하면 실시간 데이터 변경 적용 안될 수 있음
*/

binding.lifecycleOwner =viewLifecycleOwner
binding.vm = viewModel
//todo recycle

viewModel.settingHomeData()

val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
activity?.finish()
Expand All @@ -22,5 +44,11 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(Home) {
requireActivity().onBackPressedDispatcher.addCallback(
viewLifecycleOwner, callback
)




}


}
49 changes: 48 additions & 1 deletion home/src/main/java/com/najudoryeong/mineme/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
package com.najudoryeong.mineme.home

import android.content.Context
import android.util.Log
import androidx.databinding.Bindable
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.common.data.dto.HomeResponse
import com.example.common.data.entity.HomeData
import com.example.common.data.source.AuthService
import com.example.common.data.source.UserService
import com.example.common.domain.usecase.DataStoreUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject

class HomeViewModel : ViewModel() {
@HiltViewModel
class HomeViewModel @Inject constructor(
@ApplicationContext private val context: Context,
private val userService: UserService,
private val dataStoreUseCase: DataStoreUseCase
) : ViewModel() {

private val _isNewStory = MutableStateFlow(true)
val isNewStory : StateFlow<Boolean> = _isNewStory

private val _homeData = MutableStateFlow<HomeData?>(null)
val homeData: StateFlow<HomeData?> = _homeData


fun settingHomeData() {
viewModelScope.launch {
var a = dataStoreUseCase.bearerJsonWebToken.first()
if (a == null) a = "bearer test"
userService.getHomeInfo(a).let {
if (it.success){
Log.d("TESTAPI","API넣음")
if (it.data.newStory.postId == -1){
_isNewStory.value = true
}
_homeData.value = it.data

} else {
Log.d("TESTAPI","API실패")
}
}
}
}

}
Loading

0 comments on commit 66b2fd1

Please sign in to comment.