Skip to content

Commit

Permalink
Fix: Post Logic
Browse files Browse the repository at this point in the history
12/29
  • Loading branch information
wsi1212 committed Dec 29, 2023
1 parent 00c429a commit 806e573
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import okhttp3.Protocol
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody
import org.json.JSONException
import retrofit2.HttpException
import javax.inject.Inject

class Intercept
Expand Down Expand Up @@ -63,12 +65,18 @@ class Intercept
}
response = this.proceedWithToken(this.request())

if (response.code == 401 || response.code == 500) { // 가끔식 토큰 만료에서 500이 뜨기도함.... 서버이슈
runBlocking { tokenRepositoryImpl.clearData() }
response.close()
response = login()
Log.d("intercept:", "Here is 401 || 500 2")
// throw HttpException(retrofit2.Response.error<Any>(401, response.body!!))
if (response.code == 401) {
try {
runBlocking { tokenRepositoryImpl.clearData() }
Log.d("intercept:", "Here is 401 || 500 2")
response.close()
response = login()
} catch (e: JSONException) {
e.printStackTrace()
throw HttpException(retrofit2.Response.error<Any>(401, response.body!!))
}

//
}
}

Expand All @@ -79,7 +87,7 @@ class Intercept
// request에 토큰을 붙여서 새로운 request 생성 -> 진행
response = this.proceedWithToken(this.request())

return if (response.code == 401 || response.code == 500) { // 가끔식 토큰 만료에서 500이 뜨기도함.... 서버이슈
return if (response.code == 401) {
Log.d("TokenTest", "Here is Login")
runBlocking { tokenRepositoryImpl.clearData() }
Response.Builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.hs.dgsw.mentomenv2.adapter

import android.annotation.SuppressLint
import android.util.Log
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
Expand All @@ -22,23 +23,23 @@ class HomeAdapter(
binding: ItemHomeBinding,
) {
binding.item = item
Log.d("action: ", "${item.isExpended} + ${item.content}")
if (item.isExpended) {
binding.tvPreview.maxLines = Int.MAX_VALUE
binding.btnShowMore.visibility = View.GONE
} else {
binding.tvPreview.maxLines = 3
}

binding.tvPreview.post {
val layout = binding.tvPreview.layout
binding.tvPreview.post {
val layout = binding.tvPreview.layout

// 마지막 줄이 완전히 보이지 않는 경우에는 "..."이 있는 것으로 판단
val lastLineVisible = layout.getEllipsisCount(layout.lineCount - 1) == 0
// 마지막 줄이 완전히 보이지 않는 경우에는 "..."이 있는 것으로 판단
val lastLineVisible = layout.getEllipsisCount(layout.lineCount - 1) == 0

if (!lastLineVisible) {
binding.btnShowMore.visibility = View.VISIBLE
} else {
binding.btnShowMore.visibility = View.GONE
if (!lastLineVisible) {
binding.btnShowMore.visibility = View.VISIBLE
} else {
binding.btnShowMore.visibility = View.GONE
}
}
}
if (!item.imgUrls.isNullOrEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.take
import kr.hs.dgsw.mentomenv2.base.BaseViewModel
import kr.hs.dgsw.mentomenv2.domain.model.Post
import kr.hs.dgsw.mentomenv2.domain.usecase.post.GetAllPostUseCase
Expand All @@ -15,157 +16,135 @@ import javax.inject.Inject

@HiltViewModel
class HomeViewModel
@Inject
constructor(
private val getAllPostUseCase: GetAllPostUseCase,
private val getPostsByTagUseCase: GetPostsByTagUseCase,
) : BaseViewModel() {
val postState = MutableStateFlow<PostState>(PostState())
private val _errorFlow = MutableSharedFlow<String?>()
val errorFlow = _errorFlow.asSharedFlow()
private val isLoading: MutableLiveData<Boolean> = MutableLiveData(false)
var allPosts: List<Post>? = null
@Inject
constructor(
private val getAllPostUseCase: GetAllPostUseCase,
private val getPostsByTagUseCase: GetPostsByTagUseCase,
) : BaseViewModel() {
val postState = MutableStateFlow<PostState>(PostState())
private val _errorFlow = MutableSharedFlow<String?>()
val errorFlow = _errorFlow.asSharedFlow()
private val isLoading: MutableLiveData<Boolean> = MutableLiveData(false)
var allPosts: List<Post>? = null

init {
getAllPost()
}
init {
getAllPost()
}

fun getAllPost() {
if (postState.value.tag != "ALL") {
getAllPostUseCase.invoke().safeApiCall(
isLoading,
successAction = {
allPosts = it
postState.value =
PostState(
postList = it,
tag = "ALL",
)
},
errorAction = {
_errorFlow.tryEmit(Utils.NETWORK_ERROR_MESSAGE)
},
)
}
}
fun getAllPost() {
getAllPostUseCase.invoke().safeApiCall(
isLoading,
successAction = {
allPosts = it
postState.value =
PostState(
postList = it,
tag = "ALL",
)
},
errorAction = {
_errorFlow.tryEmit(Utils.NETWORK_ERROR_MESSAGE)
},
)
}

fun onClickDesignBtn() {
fun onClickDesignBtn() {
if (postState.value.tag == "DESIGN") {
getAllPost()
} else {
getPostsByTagUseCase("DESIGN").safeApiCall(
isLoading,
successAction = {
if (postState.value.tag != "DESIGN") {
postState.value =
PostState(
postList = it,
tag = "DESIGN",
)
} else {
postState.value =
PostState(
postList = allPosts,
tag = "ALL",
)
}
postState.value =
PostState(
postList = it,
tag = "DESIGN",
)
},
errorAction = {
_errorFlow.tryEmit(Utils.NETWORK_ERROR_MESSAGE)
},
)
}
}

fun onClickWebBtn() {
fun onClickWebBtn() {
if (postState.value.tag == "WEB") {
getAllPost()
} else {
getPostsByTagUseCase("WEB").safeApiCall(
isLoading,
successAction = {
if (postState.value.tag != "WEB") {
postState.value =
PostState(
postList = it,
tag = "WEB",
)
} else {
postState.value =
PostState(
postList = allPosts,
tag = "ALL",
)
}
postState.value =
PostState(
postList = it,
tag = "WEB",
)
},
errorAction = {
_errorFlow.tryEmit(Utils.NETWORK_ERROR_MESSAGE)
},
)
}
}

fun onClickAndroidBtn() {
fun onClickAndroidBtn() {
if (postState.value.tag == "ANDROID") {
getAllPost()
} else {
getPostsByTagUseCase("ANDROID").safeApiCall(
isLoading,
successAction = {
if (postState.value.tag != "ANDROID") {
postState.value =
PostState(
postList = it,
tag = "ANDROID",
)
} else {
postState.value =
PostState(
postList = allPosts,
tag = "ALL",
)
}
postState.value =
PostState(
postList = it,
tag = "ANDROID",
)
},
errorAction = {
_errorFlow.tryEmit(Utils.NETWORK_ERROR_MESSAGE)
},
)
}
}

fun onClickServerBtn() {
fun onClickServerBtn() {
if (postState.value.tag == "SERVER") {
getAllPost()
} else {
getPostsByTagUseCase("SERVER").safeApiCall(
isLoading,
successAction = {
if (postState.value.tag != "SERVER") {
postState.value =
PostState(
postList = it,
tag = "SERVER",
)
} else {
postState.value =
PostState(
postList = allPosts,
tag = "ALL",
)
}
postState.value =
PostState(
postList = it,
tag = "SERVER",
)
},
errorAction = {
_errorFlow.tryEmit(Utils.NETWORK_ERROR_MESSAGE)
},
)
}
}

fun onClickIOSBtn() {
fun onClickIOSBtn() {
if (postState.value.tag == "IOS") {
getAllPost()
} else {
getPostsByTagUseCase("IOS").safeApiCall(
isLoading,
successAction = {
if (postState.value.tag != "IOS") {
postState.value =
PostState(
postList = it,
tag = "IOS",
)
} else {
postState.value =
PostState(
postList = allPosts,
tag = "ALL",
)
}
postState.value =
PostState(
postList = it,
tag = "IOS",
)
},
errorAction = {
_errorFlow.tryEmit(Utils.NETWORK_ERROR_MESSAGE)
},
)
}
}
}
1 change: 0 additions & 1 deletion presentation/src/main/res/layout/item_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
android:fontFamily="@font/variable"
android:text="@{item.content}"
android:textSize="12sp"
app:layout_constraintBottom_toTopOf="@id/cv_preview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_profile"
Expand Down

0 comments on commit 806e573

Please sign in to comment.