From aaae56e6acc8c6e5163d362656e2beee38cecb79 Mon Sep 17 00:00:00 2001 From: kimdowoo Date: Sun, 12 Feb 2023 20:00:37 +0900 Subject: [PATCH] =?UTF-8?q?[MIN-49]=20feat:=20story=20Api=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84(#23)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/najudoryeong/mineme/MainActivity.kt | 2 +- .../common_ui/RecyclerViewBindingAdapter.kt | 13 ++ .../common/data/source/StoryService.kt | 13 ++ .../com/example/common/di/NetworkModule.kt | 8 ++ .../najudoryeong/mineme/home/HomeViewModel.kt | 6 +- .../mineme/story/StoryFragment.kt | 13 +- .../mineme/story/StoryViewModel.kt | 36 +++++- story/src/main/res/drawable/img_nostory.xml | 121 ++++++++++++++++++ story/src/main/res/layout/fragment_story.xml | 32 ++++- 9 files changed, 231 insertions(+), 13 deletions(-) create mode 100644 common-ui/src/main/java/com/najudoryeong/mineme/common_ui/RecyclerViewBindingAdapter.kt create mode 100644 common/src/main/java/com/example/common/data/source/StoryService.kt create mode 100644 story/src/main/res/drawable/img_nostory.xml diff --git a/app/src/main/java/com/najudoryeong/mineme/MainActivity.kt b/app/src/main/java/com/najudoryeong/mineme/MainActivity.kt index 1eab185..7091cbd 100644 --- a/app/src/main/java/com/najudoryeong/mineme/MainActivity.kt +++ b/app/src/main/java/com/najudoryeong/mineme/MainActivity.kt @@ -47,7 +47,7 @@ class MainActivity : AppCompatActivity(), MainActivityUtil { // appbar 구성 요소 설정 // 바텀네비게이션과 연결하면 해당 프래그먼트에 네비게이션으로는 backButton x appBarConfiguration = AppBarConfiguration( - setOf(R.id.nav_home, R.id.nav_setting, R.id.nav_setting) + setOf(R.id.nav_home, R.id.nav_story, R.id.nav_setting) ) // navController 와 actionbar 연결 diff --git a/common-ui/src/main/java/com/najudoryeong/mineme/common_ui/RecyclerViewBindingAdapter.kt b/common-ui/src/main/java/com/najudoryeong/mineme/common_ui/RecyclerViewBindingAdapter.kt new file mode 100644 index 0000000..54d3d49 --- /dev/null +++ b/common-ui/src/main/java/com/najudoryeong/mineme/common_ui/RecyclerViewBindingAdapter.kt @@ -0,0 +1,13 @@ +package com.najudoryeong.mineme.common_ui + +import androidx.databinding.BindingAdapter +import androidx.recyclerview.widget.ListAdapter +import androidx.recyclerview.widget.RecyclerView + +@BindingAdapter("adapter", "submitList", requireAll = true) +fun bindRecyclerView(view: RecyclerView, adapter: RecyclerView.Adapter<*>, submitList: List?) { + view.adapter = adapter.apply { + stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY + (this as ListAdapter).submitList(submitList?.toMutableList()) + } +} \ No newline at end of file diff --git a/common/src/main/java/com/example/common/data/source/StoryService.kt b/common/src/main/java/com/example/common/data/source/StoryService.kt new file mode 100644 index 0000000..35464d1 --- /dev/null +++ b/common/src/main/java/com/example/common/data/source/StoryService.kt @@ -0,0 +1,13 @@ +package com.example.common.data.source + +import com.example.common.data.dto.StoryListResponse +import retrofit2.http.GET +import retrofit2.http.Header + +interface StoryService { + @GET("api/v1/story/list") + suspend fun getStoryList( + @Header("Authorization") token: String, + ) : StoryListResponse + +} \ No newline at end of file diff --git a/common/src/main/java/com/example/common/di/NetworkModule.kt b/common/src/main/java/com/example/common/di/NetworkModule.kt index c8a30f6..f906878 100644 --- a/common/src/main/java/com/example/common/di/NetworkModule.kt +++ b/common/src/main/java/com/example/common/di/NetworkModule.kt @@ -1,6 +1,7 @@ package com.example.common.di import com.example.common.data.source.AuthService +import com.example.common.data.source.StoryService import com.example.common.data.source.UserService import dagger.Module import dagger.Provides @@ -26,4 +27,11 @@ class NetworkModule { return retrofit.create(UserService::class.java) } + + @Singleton + @Provides + fun provideStoryService(retrofit: Retrofit): StoryService { + return retrofit.create(StoryService::class.java) + } + } \ No newline at end of file diff --git a/home/src/main/java/com/najudoryeong/mineme/home/HomeViewModel.kt b/home/src/main/java/com/najudoryeong/mineme/home/HomeViewModel.kt index 071922a..69a0baa 100644 --- a/home/src/main/java/com/najudoryeong/mineme/home/HomeViewModel.kt +++ b/home/src/main/java/com/najudoryeong/mineme/home/HomeViewModel.kt @@ -34,9 +34,9 @@ class HomeViewModel @Inject constructor( fun settingHomeData() { viewModelScope.launch { - var a = dataStoreUseCase.bearerJsonWebToken.first() - if (a == null) a = "bearer test" - userService.getHomeInfo(a).let { + var jwt = dataStoreUseCase.bearerJsonWebToken.first() + if (jwt == null) jwt = "bearer test" + userService.getHomeInfo(jwt).let { if (it.success){ Log.d("TESTAPI","API넣음") if (it.data.newStory.postId == -1){ diff --git a/story/src/main/java/com/najudoryeong/mineme/story/StoryFragment.kt b/story/src/main/java/com/najudoryeong/mineme/story/StoryFragment.kt index 5960063..f8350f3 100644 --- a/story/src/main/java/com/najudoryeong/mineme/story/StoryFragment.kt +++ b/story/src/main/java/com/najudoryeong/mineme/story/StoryFragment.kt @@ -1,11 +1,16 @@ package com.najudoryeong.mineme.story +import androidx.fragment.app.viewModels import com.najudoryeong.mineme.common_ui.BaseFragment import com.najudoryeong.mineme.story.databinding.FragmentStoryBinding import com.example.common.data.entity.Story +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint class StoryFragment : BaseFragment(Story) { + private val viewModel: StoryViewModel by viewModels() + private val myListAdapter: StoryAdapter by lazy { StoryAdapter() } @@ -22,9 +27,11 @@ class StoryFragment : BaseFragment(Story) { override fun initView() { binding.apply { //todo callback - recyclerView.adapter = myListAdapter + this.lifecycleOwner = viewLifecycleOwner + this.adapter = myListAdapter + this.viewModel = viewModel } - myListAdapter.submitList(dataSet) - + viewModel.raedStory() } + } \ No newline at end of file diff --git a/story/src/main/java/com/najudoryeong/mineme/story/StoryViewModel.kt b/story/src/main/java/com/najudoryeong/mineme/story/StoryViewModel.kt index d9253c8..a069c20 100644 --- a/story/src/main/java/com/najudoryeong/mineme/story/StoryViewModel.kt +++ b/story/src/main/java/com/najudoryeong/mineme/story/StoryViewModel.kt @@ -1,7 +1,41 @@ package com.najudoryeong.mineme.story +import android.content.Context import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.example.common.data.entity.StoryWithDate +import com.example.common.data.source.StoryService +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 + + +@HiltViewModel +class StoryViewModel @Inject constructor( + @ApplicationContext private val context: Context, + private val storyService: StoryService, + private val dataStoreUseCase: DataStoreUseCase +) : ViewModel() { + + private val _storyList: MutableStateFlow> = MutableStateFlow(mutableListOf()) + val storyList: StateFlow> = _storyList + + fun raedStory() { + //todo isuploading + viewModelScope.launch { + storyService.getStoryList(dataStoreUseCase.bearerJsonWebToken.first()!!).let { + if (it.success) { + _storyList.value = it.data + } + } + } + } -class StoryViewModel : ViewModel(){ } \ No newline at end of file diff --git a/story/src/main/res/drawable/img_nostory.xml b/story/src/main/res/drawable/img_nostory.xml new file mode 100644 index 0000000..45c9ed1 --- /dev/null +++ b/story/src/main/res/drawable/img_nostory.xml @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/story/src/main/res/layout/fragment_story.xml b/story/src/main/res/layout/fragment_story.xml index 095d4da..ef6feae 100644 --- a/story/src/main/res/layout/fragment_story.xml +++ b/story/src/main/res/layout/fragment_story.xml @@ -3,12 +3,35 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> + + + + + + + + + android:layout_height="match_parent" + android:padding="4dp"> + + + @@ -16,7 +39,6 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - - -