Add gallery comments

This commit is contained in:
seven332 committed 2017-08-11 16:04:11 +08:00
1 parent 48af04e53e
commit 96095152e9
11 files changed
+130 -10

No files matched your search

+3
View File
@@ -83,6 +83,9 @@ dependencies {
compile "com.github.seven332.viewstate:viewstate:$view_state_library_version"
kapt "com.github.seven332.viewstate:viewstate-compiler:$view_state_library_version"
// Html
compile 'com.github.seven332:android-html:0.1.1'
// Widgets
compile "com.github.seven332:drawerlayout:$drawer_layout_library_version"
compile "com.github.seven332:refreshlayout:$refresh_layout_library_version"
@@ -16,6 +16,7 @@
package com.hippo.ehviewer.client
import com.hippo.ehviewer.client.data.Comment
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.parser.parseGalleryDetail
import com.hippo.ehviewer.client.parser.parseGalleryList
@@ -32,14 +33,14 @@ import io.reactivex.Single
interface EhClient {
fun galleryList(url: String): Single<Pair<List<GalleryInfo>, Int>>
fun galleryDetail(url: String): Single<GalleryInfo>
fun galleryDetail(url: String): Single<Pair<GalleryInfo, List<Comment>>>
}
private class EClient(val noi: Noi) : EhClient {
override fun galleryList(url: String): Single<Pair<List<GalleryInfo>, Int>> =
noi.http(url).asResponse().parse { parseGalleryList(it) }
override fun galleryDetail(url: String): Single<GalleryInfo> =
override fun galleryDetail(url: String): Single<Pair<GalleryInfo, List<Comment>>> =
noi.http(url).asResponse().parse { parseGalleryDetail(it) }
}
@@ -47,7 +48,7 @@ private class LofiClient(val noi: Noi) : EhClient {
override fun galleryList(url: String): Single<Pair<List<GalleryInfo>, Int>> =
noi.http(url).asResponse().parse { parseLofiGalleryList(it) }
override fun galleryDetail(url: String): Single<GalleryInfo> =
override fun galleryDetail(url: String): Single<Pair<GalleryInfo, List<Comment>>> =
noi.http(url).asResponse().parse { parseLofiGalleryDetail(it) }
}
@@ -191,7 +191,9 @@ private fun parseComment(element: Element): Comment? {
return comment
}
fun parseGalleryDetail(body: String): GalleryInfo {
fun parseGalleryDetail(body: String): Pair<GalleryInfo, List<Comment>> {
val document = Jsoup.parse(body)
return parseGalleryDetail(document) ?: throw ParseException("Can't parse gallery detail")
val info = parseGalleryDetail(document) ?: throw ParseException("Can't parse gallery detail")
val comments = parseComments(document)
return Pair(info, comments)
}
@@ -16,12 +16,13 @@
package com.hippo.ehviewer.client.parser
import com.hippo.ehviewer.client.data.Comment
import com.hippo.ehviewer.client.data.GalleryInfo
/*
* Created by Hippo on 2017/7/31.
*/
fun parseLofiGalleryDetail(body: String): GalleryInfo {
fun parseLofiGalleryDetail(body: String): Pair<GalleryInfo, List<Comment>> {
TODO()
}
@@ -45,8 +45,8 @@ open class GalleryDetailPen private constructor(
EH_CLIENT.galleryDetail(EH_URL.galleryDetailUrl(info.gid, info.token))
.observeOn(AndroidSchedulers.mainThread())
.register({
showContentBody(it, true)
.register({ (info, comments) ->
showContentBody(info, comments, true)
}, {
showContentTip(it, true)
})
@@ -25,6 +25,7 @@ import android.widget.ImageView
import android.widget.TextView
import com.google.android.flexbox.FlexboxLayout
import com.hippo.ehviewer.R
import com.hippo.ehviewer.client.data.Comment
import com.hippo.ehviewer.client.data.GalleryInfo
import com.hippo.ehviewer.client.data.TagSet
import com.hippo.ehviewer.client.lang
@@ -38,6 +39,7 @@ import com.hippo.ehviewer.util.drawable
import com.hippo.ehviewer.util.explain
import com.hippo.ehviewer.util.explainVividly
import com.hippo.ehviewer.util.find
import com.hippo.ehviewer.util.fromHtml
import com.hippo.ehviewer.util.prettyTime
import com.hippo.ehviewer.util.string
import com.hippo.ehviewer.widget.ViewTransition
@@ -69,7 +71,7 @@ interface GalleryDetailUi : MvpUi {
fun showProgress(animation: Boolean)
@StrategyType(value = SingleByTag::class, tag = TAG_SHOW_CONTENT)
fun showContentBody(info: GalleryInfo, animation: Boolean)
fun showContentBody(info: GalleryInfo, comments: List<Comment>, animation: Boolean)
@StrategyType(value = SingleByTag::class, tag = TAG_SHOW_CONTENT)
fun showContentTip(error: Throwable, animation: Boolean)
@@ -188,7 +190,22 @@ class GalleryDetailPaper(
}
}
override fun showContentBody(info: GalleryInfo, animation: Boolean) {
private fun ViewGroup.inflateComments(comments: List<Comment>) {
for (i in 0 until minOf(2, comments.size)) {
val comment = comments[i]
val view = inflater.inflate(R.layout.gallery_detail_comments_item, this, false) as ViewGroup
view.find<TextView>(R.id.user).text = comment.user
view.find<TextView>(R.id.date).text = comment.date.prettyTime(context)
view.find<TextView>(R.id.comment).text = comment.comment?.fromHtml()
addView(view, i)
}
view.find<TextView>(R.id.comments_tip).text = context.string(
if (comments.isEmpty()) R.string.gallery_detail_no_comment
else R.string.gallery_detail_more_comments
)
}
override fun showContentBody(info: GalleryInfo, comments: List<Comment>, animation: Boolean) {
contentTransition.show(contentBody, animation && !logic.isRestoring())
content.find<TextView>(R.id.language).text = info.language.lang().let { if (it != 0) it else R.string.language_unknown }.let { context.string(it) }
@@ -218,6 +235,7 @@ class GalleryDetailPaper(
}
content.find<TextView>(R.id.no_tag).visibility = View.GONE
}
content.find<ViewGroup>(R.id.comments).inflateComments(comments)
}
override fun showContentTip(error: Throwable, animation: Boolean) {
@@ -16,6 +16,8 @@
package com.hippo.ehviewer.util
import com.hippo.html.Html
/*
* Created by Hippo on 2017/7/26.
*/
@@ -169,3 +171,5 @@ fun String.strip(): String {
return substring(start, end)
}
fun String.fromHtml(): CharSequence = Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY)
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2017 Hippo Seven
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/comments"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/key_margin"
android:divider="@drawable/divider_key_margin"
android:showDividers="middle">
<TextView
android:id="@+id/comments_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="?attr/colorAccent"/>
</LinearLayout>
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2017 Hippo Seven
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/key_margin"
android:paddingRight="@dimen/key_margin">
<TextView
android:id="@+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:maxLines="5"
android:ellipsize="end"/>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/user"
android:layout_alignParentLeft="true"
android:layout_marginTop="8dp"
android:textColor="?android:attr/textColorPrimary"/>
</RelativeLayout>
@@ -37,6 +37,10 @@
<include layout="@layout/gallery_detail_tags"/>
<include layout="@layout/gallery_detail_divider"/>
<include layout="@layout/gallery_detail_comments"/>
</LinearLayout>
<TextView
+2
View File
@@ -35,6 +35,8 @@
<string name="gallery_detail_date">Date</string>
<string name="gallery_detail_more_info">More info</string>
<string name="gallery_detail_no_tag">No tag</string>
<string name="gallery_detail_more_comments">More comments</string>
<string name="gallery_detail_no_comment">No comment</string>
<string name="error_unknown">Unknown Error</string>
<string name="error_not_found">Not Found</string>