Skip to content

Commit

Permalink
Merge pull request #71 from ininmm/task/refactor_set_post_rank
Browse files Browse the repository at this point in the history
Refactor SetPostRankAPI and add tests
  • Loading branch information
ininmm authored Dec 28, 2020
2 parents e956e07 + 9e17f80 commit c774ba9
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 126 deletions.
33 changes: 33 additions & 0 deletions app/src/main/java/tw/y_studio/ptt/api/PostAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package tw.y_studio.ptt.api

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.FormBody
import okhttp3.Request
import okhttp3.RequestBody
import org.json.JSONObject
import tw.y_studio.ptt.model.PartialPost
import tw.y_studio.ptt.model.Post
import tw.y_studio.ptt.model.PostRank
import tw.y_studio.ptt.utils.Log
import tw.y_studio.ptt.utils.model.PartialPostTypeAdapter
import tw.y_studio.ptt.utils.model.PostTypeAdapter
import java.io.IOException

class PostAPI : BaseAPIHelper() {

Expand Down Expand Up @@ -79,4 +82,34 @@ class PostAPI : BaseAPIHelper() {
}
return data
}

@Throws(Exception::class)
fun setPostRank(
boardName: String,
aid: String,
pttId: String,
rank: PostRankMark
) {
val body: RequestBody = FormBody.Builder().build()
val request = Request.Builder()
.post(body)
.url(
"$hostUrl/api/Rank/$boardName/$aid?pttid=$pttId&rank=${rank.value}"
)
.build()
Log("SetPostRankAPIHelper", "" + request.toString())
val call = okHttpClient!!.newCall(request)
val response = call.execute()
val code = response.code // can be any value
if (!response.isSuccessful && code != 200) {
// error
throw Exception("Error Code : $code")
} else {
response.body ?: throw IOException("No response body!")
}
}
}

enum class PostRankMark(val value: Int) {
Like(1), Dislike(-1), None(0)
}
94 changes: 0 additions & 94 deletions app/src/main/java/tw/y_studio/ptt/api/SetPostRankAPIHelper.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package tw.y_studio.ptt.source.remote.post

import tw.y_studio.ptt.api.PostRankMark
import tw.y_studio.ptt.model.PartialPost
import tw.y_studio.ptt.model.Post
import tw.y_studio.ptt.model.PostRank

interface IPostRemoteDataSource {
fun getPost(board: String, fileName: String): Post

fun setPostRank(
boardName: String,
aid: String,
pttId: String,
rank: PostRankMark
)

fun getPostRank(board: String, aid: String): PostRank

fun getPostList(broadName: String, page: Int): List<PartialPost>

fun disposeAll()
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
package tw.y_studio.ptt.source.remote.post

import tw.y_studio.ptt.api.PostAPI
import tw.y_studio.ptt.api.PostRankMark
import tw.y_studio.ptt.model.PartialPost
import tw.y_studio.ptt.model.Post
import tw.y_studio.ptt.model.PostRank

class PostRemoteDataSourceImpl(
private val postAPIHelper: PostAPI
private val postAPI: PostAPI
) : IPostRemoteDataSource {
override fun getPost(board: String, fileName: String): Post {
return postAPIHelper.getPost(board, fileName)
return postAPI.getPost(board, fileName)
}

override fun setPostRank(
boardName: String,
aid: String,
pttId: String,
rank: PostRankMark
) {
postAPI.setPostRank(boardName, aid, pttId, rank)
}

override fun getPostRank(board: String, aid: String): PostRank {
return postAPIHelper.getPostRank(board, aid)
return postAPI.getPostRank(board, aid)
}

override fun getPostList(broadName: String, page: Int): List<PartialPost> {
return postAPIHelper.getPostList(broadName, page)
return postAPI.getPostList(broadName, page)
}

override fun disposeAll() {
postAPIHelper.close()
postAPI.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import androidx.annotation.ColorInt
import androidx.appcompat.widget.PopupMenu
import androidx.recyclerview.widget.RecyclerView
import tw.y_studio.ptt.R
import tw.y_studio.ptt.api.SetPostRankAPIHelper
import tw.y_studio.ptt.api.SetPostRankAPIHelper.iRank
import tw.y_studio.ptt.api.PostRankMark
import tw.y_studio.ptt.databinding.ArticleReadFragmentLayoutBinding
import tw.y_studio.ptt.di.Injection
import tw.y_studio.ptt.fragment.LoginPageFragment
Expand Down Expand Up @@ -264,19 +263,19 @@ class ArticleReadFragment : BaseFragment() {
val popupMenu = PopupMenu(currentActivity, view)
popupMenu.menuInflater.inflate(R.menu.post_article_rank_menu, popupMenu.menu)
popupMenu.setOnMenuItemClickListener { item ->
var rank = iRank.non
var rank = PostRankMark.None
when (item.itemId) {
R.id.post_article_rank_like -> rank = iRank.like
R.id.post_article_rank_dislike -> rank = iRank.dislike
R.id.post_article_rank_non -> rank = iRank.non
R.id.post_article_rank_like -> rank = PostRankMark.Like
R.id.post_article_rank_dislike -> rank = PostRankMark.Dislike
R.id.post_article_rank_non -> rank = PostRankMark.None
}
setRank(rank)
true
}
popupMenu.show()
}

private fun rehreshRank() {
private fun refreshRank() {
r1 = Runnable {
runOnUI { binding.articleReadFragmentRefreshLayout.isRefreshing = true }
GattingData = true
Expand Down Expand Up @@ -315,36 +314,36 @@ class ArticleReadFragment : BaseFragment() {
}

private var mDialog: ProgressDialog? = null
private fun setRank(rank_: iRank) {
private fun setRank(rank: PostRankMark) {
mDialog = ProgressDialog.show(currentActivity, "", "Please wait.").apply {
getWindow()!!.setBackgroundDrawableResource(R.drawable.dialog_background)
object : Thread() {
override fun run() {
try {
val setPostRankAPI: SetPostRankAPIHelper
val id = currentActivity
.getSharedPreferences("MainSetting", Context.MODE_PRIVATE)
.getString("APIPTTID", "")
if (id.isNullOrEmpty()) {
throw Exception("No Ptt id")
}
val p = Pattern.compile(
"www.ptt.cc/bbs/([-a-zA-Z0-9_]{2,})/([M|G].[-a-zA-Z0-9._]{1,30}).htm"
)
val m = p.matcher(orgUrl)
setPostRankAPI = if (m.find()) {
if (m.find()) {
val aid = AidConverter.urlToAid(orgUrl)
SetPostRankAPIHelper(
context, aid.boardTitle, aid.aid
postRemoteDataSource.setPostRank(
aid.boardTitle,
aid.aid,
id,
rank
)
} else {
throw Exception("error")
// DebugUtils.Log("onAR", "not match");
}
val id = currentActivity
.getSharedPreferences("MainSetting", Context.MODE_PRIVATE)
.getString("APIPTTID", "")
if (id!!.length == 0) {
throw Exception("No Ptt id")
}
setPostRankAPI[id, rank_]
runOnUI {
dismiss()
rehreshRank()
refreshRank()
}
} catch (e: Exception) {
runOnUI {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/tw/y_studio/ptt/utils/GsonExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tw.y_studio.ptt.utils

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
* Created by Michael.Lien
* on 2020/12/25
*/
inline fun <reified T> Gson.fromJson(json: String): T = fromJson(json, object : TypeToken<T>() {}.type)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.junit.After
import org.junit.Before
import org.junit.Test
import tw.y_studio.ptt.api.PostAPI
import tw.y_studio.ptt.api.PostRankMark
import tw.y_studio.ptt.model.PartialPost
import tw.y_studio.ptt.model.Post

Expand Down Expand Up @@ -59,12 +60,12 @@ class PostRemoteDataSourceTest {
Truth.assertThat(
result == Post(
title = "",
"",
"",
"",
"",
"",
emptyList()
classString = "",
date = "",
auth = "",
authNickName = "",
content = "",
comments = emptyList()
)
)
}
Expand All @@ -75,6 +76,13 @@ class PostRemoteDataSourceTest {
postRemoteDataSource.getPostList("", 0)
}

@Test
fun set_post_rank_then_success() {
every { postAPI.setPostRank(any(), any(), any(), any()) } just Runs
postRemoteDataSource.setPostRank("", "", "", PostRankMark.None)
verify { postAPI.setPostRank(any(), any(), any(), any()) }
}

@Test
fun dispose_all_then_check_invoke() {
every { postAPI.close() } just Runs
Expand Down

0 comments on commit c774ba9

Please sign in to comment.