Skip to content

Commit 5febf23

Browse files
authored
Merge pull request #79 from ttoklip/feat/#1_home_news_fragment
Feat/#1 home news fragment
2 parents b5dcf15 + 54a1b68 commit 5febf23

File tree

13 files changed

+215
-108
lines changed

13 files changed

+215
-108
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
![in1.png](https://raw.githubusercontent.com/ttoklip/BackEnd/main/introduce/in1.png)
2+
3+
![in2.png](https://raw.githubusercontent.com/ttoklip/BackEnd/main/introduce/in2.png)
4+
5+
![in3.png](https://raw.githubusercontent.com/ttoklip/BackEnd/main/introduce/in3.png)
6+
7+
![in4.png](https://raw.githubusercontent.com/ttoklip/BackEnd/main/introduce/in4.png)
8+
9+
10+
## 아키텍쳐
11+
<img width="755" alt="hangunhee39_ttoklip_architecure" src="https://github.com/ttoklip/Android/assets/77563098/c3640f5d-ea24-46bd-807b-023127e81ea0">
12+
13+
14+
## 맴버
15+
| [한건희](https://github.com/hangunhee39) | [김규진](https://github.com/kyujin0911) | [박승연](https://github.com/40food) | [심영수](https://github.com/posite) |
16+
|:------------------------------------------------------------------:|:------------------------------------------------------------------:|:------------------------------------------------------------------:|:---------------------------------------------------------------:|
17+
| 24.01. ~ | 24.01. ~ | 24.01. ~ | 24.01. ~ 24.02. |

app/src/main/java/com/umc/ttoklip/data/api/HomeApi.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ package com.umc.ttoklip.data.api
22

33
import com.umc.ttoklip.data.model.ResponseBody
44
import com.umc.ttoklip.data.model.home.HomeResponse
5+
import com.umc.ttoklip.data.model.home.NotificationResponse
56
import retrofit2.Response
67
import retrofit2.http.GET
8+
import retrofit2.http.Query
79

810
interface HomeApi {
911

1012
@GET("/api/v1/home")
1113
suspend fun getHomeMainApi(): Response<ResponseBody<HomeResponse>>
1214

15+
@GET("/api/v1/notification/my-notification")
16+
suspend fun getNotifications(
17+
@Query("notificationCategory") category: String
18+
): Response<ResponseBody<NotificationResponse>>
19+
1320
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.umc.ttoklip.data.model.home
2+
3+
data class NotificationItem(
4+
val noticeSuccess: Boolean,
5+
val notificationId: Int,
6+
val targetClassId: Int,
7+
val targetClassName: String,
8+
val text: String,
9+
val title: String
10+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.umc.ttoklip.data.model.home
2+
3+
data class NotificationResponse (
4+
val responses : List<NotificationItem>
5+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.umc.ttoklip.data.repository.home
22

33
import com.umc.ttoklip.data.model.home.HomeResponse
4+
import com.umc.ttoklip.data.model.home.NotificationResponse
45
import com.umc.ttoklip.module.NetworkResult
56

67
interface HomeRepository {
78

89
suspend fun getHomeMain(): NetworkResult<HomeResponse>
10+
11+
suspend fun getNotifications(category: String) : NetworkResult<NotificationResponse>
912
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package com.umc.ttoklip.data.repository.home
22

33
import com.umc.ttoklip.data.api.HomeApi
4-
import com.umc.ttoklip.data.api.SearchApi
54
import com.umc.ttoklip.data.model.ResponseBody
65
import com.umc.ttoklip.data.model.home.HomeResponse
7-
import com.umc.ttoklip.data.model.news.MainNewsResponse
8-
import com.umc.ttoklip.data.model.search.NewsSearchResponse
9-
import com.umc.ttoklip.data.model.search.SearchModel
10-
import com.umc.ttoklip.data.model.search.TipSearchResponse
11-
import com.umc.ttoklip.data.model.search.TownSearchResponse
6+
import com.umc.ttoklip.data.model.home.NotificationResponse
127
import com.umc.ttoklip.module.NetworkResult
138
import com.umc.ttoklip.module.handleApi
149
import javax.inject.Inject
@@ -21,4 +16,9 @@ class HomeRepositoryImpl @Inject constructor(
2116
override suspend fun getHomeMain(): NetworkResult<HomeResponse> {
2217
return handleApi({api.getHomeMainApi()}) {response: ResponseBody<HomeResponse> -> response.result}
2318
}
19+
20+
override suspend fun getNotifications(category: String): NetworkResult<NotificationResponse> {
21+
return handleApi({api.getNotifications(category = category)}) {response: ResponseBody<NotificationResponse> -> response.result}
22+
23+
}
2424
}

app/src/main/java/com/umc/ttoklip/presentation/alarm/AlarmActivity.kt

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,43 @@ package com.umc.ttoklip.presentation.alarm
22

33
import android.content.Context
44
import android.content.Intent
5-
import androidx.activity.viewModels
65
import com.umc.ttoklip.R
6+
import android.util.Log
7+
import androidx.activity.viewModels
8+
import androidx.lifecycle.Lifecycle
9+
import androidx.lifecycle.lifecycleScope
10+
import androidx.lifecycle.repeatOnLifecycle
711
import com.umc.ttoklip.databinding.ActivityAlarmBinding
812
import com.umc.ttoklip.presentation.base.BaseActivity
9-
import com.umc.ttoklip.presentation.news.adapter.Dummy
13+
import com.umc.ttoklip.presentation.hometown.together.read.ReadTogetherActivity
14+
import com.umc.ttoklip.presentation.honeytip.read.ReadHoneyTipActivity
15+
import com.umc.ttoklip.presentation.honeytip.read.ReadQuestionActivity
1016
import com.umc.ttoklip.presentation.search.adapter.AlarmRVA
11-
import com.umc.ttoklip.presentation.search.adapter.SearchRVA
17+
import dagger.hilt.android.AndroidEntryPoint
18+
import kotlinx.coroutines.launch
1219

20+
@AndroidEntryPoint
1321
class AlarmActivity : BaseActivity<ActivityAlarmBinding>(R.layout.activity_alarm) {
1422

15-
private val viewModel: AlarmViewModel by viewModels<AlarmViewModelImpl>()
23+
private val viewModel: AlarmViewModel by viewModels<AlarmViewModel>()
1624
private val alarmRVA by lazy {
17-
AlarmRVA({})
25+
AlarmRVA {
26+
when (it.title) {
27+
"꿀팁 공유해요" -> {
28+
startActivity(ReadHoneyTipActivity.newIntent(this, it.targetClassId))
29+
}
30+
31+
"우리동네 덧글", "우리동네 답글" -> {
32+
startActivity(ReadTogetherActivity.newIntent(this, it.targetClassId.toLong()))
33+
}
34+
35+
"질문해요" -> {
36+
startActivity(ReadQuestionActivity.newIntent(this, it.targetClassId))
37+
}
38+
39+
else -> Unit
40+
}
41+
}
1842
}
1943

2044
override fun initView() {
@@ -23,22 +47,23 @@ class AlarmActivity : BaseActivity<ActivityAlarmBinding>(R.layout.activity_alarm
2347
finish()
2448
}
2549
binding.alarmRV.adapter = alarmRVA
26-
alarmRVA.submitList(
27-
listOf(
28-
Dummy("1"),
29-
Dummy("2")
30-
)
31-
)
3250
}
3351

3452
override fun initObserver() {
35-
53+
lifecycleScope.launch {
54+
repeatOnLifecycle(Lifecycle.State.STARTED) {
55+
viewModel.notiList.collect {
56+
alarmRVA.submitList(it)
57+
Log.d("확인", "${it.map { it.notificationId }}")
58+
}
59+
}
60+
}
3661
}
3762

38-
companion object{
63+
companion object {
3964
const val ALARM = "alarm"
4065

41-
fun newIntent(context : Context) =
66+
fun newIntent(context: Context) =
4267
Intent(context, AlarmActivity::class.java)
4368

4469
}
Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,115 @@
11
package com.umc.ttoklip.presentation.alarm
22

3-
interface AlarmViewModel {
3+
import android.util.Log
4+
import androidx.lifecycle.ViewModel
5+
import androidx.lifecycle.viewModelScope
6+
import com.umc.ttoklip.TtoklipApplication
7+
import com.umc.ttoklip.data.model.home.NotificationItem
8+
import com.umc.ttoklip.data.model.home.NotificationResponse
9+
import com.umc.ttoklip.data.model.news.News
10+
import com.umc.ttoklip.data.repository.home.HomeRepository
11+
import com.umc.ttoklip.module.onException
12+
import com.umc.ttoklip.module.onFail
13+
import com.umc.ttoklip.module.onSuccess
14+
import dagger.hilt.android.lifecycle.HiltViewModel
15+
import kotlinx.coroutines.flow.MutableStateFlow
16+
import kotlinx.coroutines.flow.StateFlow
17+
import kotlinx.coroutines.launch
18+
import javax.inject.Inject
19+
20+
@HiltViewModel
21+
class AlarmViewModel @Inject constructor(
22+
private val repository: HomeRepository
23+
) : ViewModel() {
24+
25+
private val _notiList = MutableStateFlow<List<NotificationItem>>(listOf())
26+
val notiList: StateFlow<List<NotificationItem>>
27+
get() = _notiList
28+
29+
init {
30+
viewModelScope.launch {
31+
getNotificationsHoney()
32+
getNotificationsQuestion()
33+
getNotificationsTown()
34+
notiList.value.sortedBy { it.notificationId }
35+
}
36+
}
37+
38+
private fun getNotificationsHoney() {
39+
viewModelScope.launch {
40+
try {
41+
repository.getNotifications("HONEY_TIP_COMMENT")
42+
.onSuccess {
43+
_notiList.emit(notiList.value + it.responses)
44+
repository.getNotifications("HONEY_TIP_CHILD_COMMENT")
45+
.onSuccess {
46+
_notiList.emit(notiList.value + it.responses)
47+
}.onFail {
48+
49+
}.onException {
50+
throw it
51+
}
52+
}.onFail {
53+
54+
}.onException {
55+
throw it
56+
}
57+
} catch (e: Exception) {
58+
e.printStackTrace()
59+
Log.d("예외", "$e")
60+
}
61+
}
62+
}
63+
64+
private fun getNotificationsQuestion() {
65+
viewModelScope.launch {
66+
try {
67+
repository.getNotifications("QUESTION_COMMENT")
68+
.onSuccess {
69+
_notiList.emit(notiList.value + it.responses)
70+
repository.getNotifications("QUESTION_CHILD_COMMENT")
71+
.onSuccess {
72+
_notiList.emit(notiList.value + it.responses)
73+
}.onFail {
74+
75+
}.onException {
76+
throw it
77+
}
78+
}.onFail {
79+
80+
}.onException {
81+
throw it
82+
}
83+
} catch (e: Exception) {
84+
e.printStackTrace()
85+
Log.d("예외", "$e")
86+
}
87+
}
88+
}
89+
90+
private fun getNotificationsTown() {
91+
viewModelScope.launch {
92+
try {
93+
repository.getNotifications("OUR_TOWN_COMMENT")
94+
.onSuccess {
95+
_notiList.emit(notiList.value + it.responses)
96+
repository.getNotifications("OUR_TOWN_CHILD_COMMENT")
97+
.onSuccess {
98+
_notiList.emit(notiList.value + it.responses)
99+
}.onFail {
100+
101+
}.onException {
102+
throw it
103+
}
104+
}.onFail {
105+
106+
}.onException {
107+
throw it
108+
}
109+
} catch (e: Exception) {
110+
e.printStackTrace()
111+
Log.d("예외", "$e")
112+
}
113+
}
114+
}
4115
}

app/src/main/java/com/umc/ttoklip/presentation/alarm/AlarmViewModelImpl.kt

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/src/main/java/com/umc/ttoklip/presentation/alarm/adapter/AlarmRVA.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import android.view.ViewGroup
55
import androidx.recyclerview.widget.DiffUtil
66
import androidx.recyclerview.widget.ListAdapter
77
import androidx.recyclerview.widget.RecyclerView
8+
import com.umc.ttoklip.data.model.home.NotificationItem
89
import com.umc.ttoklip.databinding.ItemAlarmBinding
910
import com.umc.ttoklip.presentation.news.adapter.Dummy
1011

11-
class AlarmRVA(val onClick: () -> Unit) : ListAdapter<Dummy, AlarmRVA.ItemViewHolder>(differ) {
12+
class AlarmRVA(val onClick: (NotificationItem) -> Unit) : ListAdapter<NotificationItem, AlarmRVA.ItemViewHolder>(differ) {
1213

1314
inner class ItemViewHolder(
1415
private val binding: ItemAlarmBinding
1516
) : RecyclerView.ViewHolder(binding.root) {
1617

17-
fun bind(data: Dummy) {
18-
18+
fun bind(data: NotificationItem) {
19+
binding.item = data
20+
binding.root.setOnClickListener {
21+
onClick(data)
22+
}
1923
}
2024
}
2125

@@ -37,12 +41,12 @@ class AlarmRVA(val onClick: () -> Unit) : ListAdapter<Dummy, AlarmRVA.ItemViewHo
3741
}
3842

3943
companion object {
40-
val differ = object : DiffUtil.ItemCallback<Dummy>() {
41-
override fun areItemsTheSame(oldItem: Dummy, newItem: Dummy): Boolean {
42-
return oldItem.name == newItem.name
44+
val differ = object : DiffUtil.ItemCallback<NotificationItem>() {
45+
override fun areItemsTheSame(oldItem: NotificationItem, newItem: NotificationItem): Boolean {
46+
return oldItem.notificationId == newItem.notificationId
4347
}
4448

45-
override fun areContentsTheSame(oldItem: Dummy, newItem: Dummy): Boolean {
49+
override fun areContentsTheSame(oldItem: NotificationItem, newItem: NotificationItem): Boolean {
4650
return oldItem == newItem
4751
}
4852
}

0 commit comments

Comments
 (0)