Skip to content

Commit

Permalink
rakuen tab
Browse files Browse the repository at this point in the history
  • Loading branch information
ekibun committed Apr 24, 2019
1 parent be1c34b commit 729d7a4
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 28 deletions.
7 changes: 4 additions & 3 deletions app/src/main/java/soko/ekibun/bangumi/api/bangumi/Bangumi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ interface Bangumi {

//超展开
fun getRakuen(type: String, ua: String): Call<List<Rakuen>>{
val url = "$SERVER/m" + if(type.isEmpty()) "" else "?type=$type"
val url = "$SERVER/rakuen/topiclist" + if(type.isEmpty()) "" else "?type=$type"
return ApiHelper.buildHttpCall(url, mapOf("User-Agent" to ua)){
val doc = Jsoup.parse(it.body()?.string()?:"")
val ret = ArrayList<Rakuen>()
Expand Down Expand Up @@ -441,7 +441,7 @@ interface Bangumi {
val user = it.selectFirst(".inner")?.selectFirst("a")
val userId = Regex("""/user/([^/]*)""").find(user?.attr("href")?:"")?.groupValues?.get(1)?:""
val userName = user?.text()?:""
val userSign = it.selectFirst(".inner")?.selectFirst(".tip_j")?.text()?.let{it.substring(1, it.length-1)}?:""
val userSign = it.selectFirst(".inner")?.selectFirst(".tip_j")?.text()?:""
val message = it.selectFirst(".topic_content")?.html()
?:it.selectFirst(".message")?.html()
?:it.selectFirst(".cmt_sub_content")?.html()?:""
Expand Down Expand Up @@ -476,14 +476,15 @@ interface Bangumi {
val errorLink = HttpUtil.getUrl(error?.selectFirst("a")?.attr("href")?:"", URI.create(SERVER))
val group = doc.selectFirst("#pageHeader")?.selectFirst("span")?.text()?:""
val title = doc.selectFirst("#pageHeader")?.selectFirst("h1")?.ownText()?:""
val image = getImageUrl(doc.selectFirst("#pageHeader")?.selectFirst("img"))
val form = doc.selectFirst("#ReplyForm")
val post = HttpUtil.getUrl("${form?.attr("action")}?ajax=1", URI.create(SERVER))
val formhash = form?.selectFirst("input[name=formhash]")?.attr("value")
val lastview = form?.selectFirst("input[name=lastview]")?.attr("value")
val links = LinkedHashMap<String, String>()
doc.selectFirst("#pageHeader")?.select("a")?.filter { !it.text().isNullOrEmpty() }?.forEach {
links[it.text()]= HttpUtil.getUrl(it.attr("href")?:"", URI.create(SERVER)) }
return@buildHttpCall Topic(user_id, group, title, replies, post, formhash, lastview, links, error?.text(), errorLink)
return@buildHttpCall Topic(user_id, group, title, image, replies, post, formhash, lastview, links, error?.text(), errorLink)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package soko.ekibun.bangumi.api.bangumi.bean

data class Topic(
val user_id: String?,
val user_id: String?, // Unused
val group: String,
val title: String,
val image: String,
val replies: List<TopicPost>,
val post: String,
val formhash: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package soko.ekibun.bangumi.ui.main.fragment.home.fragment.rakuen

import android.os.Bundle
import android.support.v7.widget.PopupMenu
import android.view.LayoutInflater
import android.view.View
import kotlinx.android.synthetic.main.fragment_rakuen.*
import kotlinx.android.synthetic.main.item_rakuen_tab.view.*
import soko.ekibun.bangumi.R
import soko.ekibun.bangumi.api.bangumi.Bangumi
import soko.ekibun.bangumi.ui.main.fragment.home.fragment.HomeTabFragment
Expand All @@ -21,6 +24,24 @@ class RakuenFragment: HomeTabFragment(R.layout.fragment_rakuen){
}
item_pager?.adapter = adapter
item_tabs?.setupWithViewPager(item_pager)

val topicTab = LayoutInflater.from(view.context).inflate(R.layout.item_rakuen_tab, item_tabs, false)
val popup = PopupMenu(view.context, topicTab)
popup.menuInflater.inflate(R.menu.list_topic_filter, popup.menu)
popup.setOnMenuItemClickListener{
if(adapter.selectedFilter != it.itemId) adapter.reset(1)
adapter.selectedFilter = it.itemId
topicTab.item_filter.text = it.title
adapter.loadTopicList()
true
}
topicTab.item_filter.text = popup.menu.findItem(adapter.selectedFilter)?.title
topicTab.setOnClickListener {
if(item_pager?.currentItem != 1) item_pager?.currentItem = 1
else popup.show()
}

item_tabs?.getTabAt(1)?.customView = topicTab
item_new?.setOnClickListener {
WebActivity.launchUrl(view.context, "${Bangumi.SERVER}/rakuen/new_topic")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package soko.ekibun.bangumi.ui.main.fragment.home.fragment.rakuen

import android.annotation.SuppressLint
import android.content.Context
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
Expand All @@ -19,6 +20,7 @@ import soko.ekibun.bangumi.ui.topic.TopicActivity

class RakuenPagerAdapter(context: Context, val fragment: RakuenFragment, private val pager: ViewPager, private val scrollTrigger: (Boolean)->Unit) : PagerAdapter(){
private val tabList = context.resources.getStringArray(R.array.topic_list)
var selectedFilter = R.id.topic_filter_all

init{
pager.addOnPageChangeListener(object: ViewPager.OnPageChangeListener{
Expand Down Expand Up @@ -63,12 +65,25 @@ class RakuenPagerAdapter(context: Context, val fragment: RakuenFragment, private
return item.second
}

fun reset(position : Int){
val item = items[position]?:return
topicCall[position]?.cancel()
item.first.isUseEmpty(false)
item.first.setNewData(null)
}

@SuppressLint("UseSparseArrays")
private var topicCall = HashMap<Int, Call<List<Rakuen>>>()
fun loadTopicList(position: Int = pager.currentItem){
val item = items[position]?:return
item.first.isUseEmpty(false)
topicCall[position]?.cancel()
topicCall[position] = Bangumi.getRakuen(listOf("", "group", "subject", "ep", "mono")[position], (fragment.activity as? MainActivity)?.ua?:"")
topicCall[position] = Bangumi.getRakuen(if(position == 1) when(selectedFilter){
R.id.topic_filter_join -> "my_group"
R.id.topic_filter_post -> "my_group&filter=topic"
R.id.topic_filter_reply -> "my_group&filter=reply"
else -> "group"
} else listOf("", "group", "subject", "ep", "mono")[position], (fragment.activity as? MainActivity)?.ua?:"")
item.second.isRefreshing = true
topicCall[position]?.enqueue(ApiHelper.buildCallback(item.second.context, {
item.first.isUseEmpty(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PostAdapter(data: MutableList<TopicPost>? = null) :
helper.addOnClickListener(R.id.item_edit)
helper.addOnClickListener(R.id.item_avatar)
helper.itemView.item_user.text = item.nickname
helper.itemView.item_user_sign.text = item.sign
helper.itemView.item_user_sign.text = if(item.sign.length < 2) "" else item.sign.substring(1, item.sign.length-1)
val subFloor = if (item.sub_floor > 0) "-${item.sub_floor}" else ""
helper.itemView.item_time.text = "#${item.floor}$subFloor - ${item.dateline}"
helper.itemView.offset.visibility = if (item.isSub) View.VISIBLE else View.GONE
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/soko/ekibun/bangumi/ui/topic/TopicView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class TopicView(private val context: TopicActivity){
context.title_slice_0.maxWidth = context.title_expand.width - if(context.title_slice_divider.visibility == View.VISIBLE) 2*context.title_slice_divider.width + context.title_slice_1.width else 0
}

if(!topic.replies.isEmpty())
if(topic.replies.isNotEmpty())
GlideUtil.with(context.item_cover_blur)
?.load(HttpUtil.getUrl(topic.replies.firstOrNull()?.avatar?:"", URI.create(Bangumi.SERVER)))
?.load(HttpUtil.getUrl(if(topic.image.isEmpty()) topic.replies.firstOrNull()?.avatar?:"" else topic.image, URI.create(Bangumi.SERVER)))
?.apply(RequestOptions.placeholderOf(context.item_cover_blur.drawable))
?.apply(RequestOptions.bitmapTransform(BlurTransformation(25, 8)))
?.into(context.item_cover_blur)
Expand Down
43 changes: 23 additions & 20 deletions app/src/main/java/soko/ekibun/bangumi/ui/web/WebActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,33 +183,36 @@ class WebActivity : SwipeBackActivity() {
return false
}

private fun getRakuen(page: String?): String?{
val url = page?.split("#")?.get(0)?:return page
var regex = Regex("""/m/topic/([^/]*)/([0-9]*)$""")
var model = regex.find(url)?.groupValues?.get(1)?:""
var id = regex.find(url)?.groupValues?.get(2)?.toIntOrNull()
if(id != null) return "${Bangumi.SERVER}/rakuen/topic/$model/$id"

regex = Regex("""/([^/]*)/topic/([0-9]*)$""")
model = regex.find(url)?.groupValues?.get(1)?:""
id = regex.find(url)?.groupValues?.get(2)?.toIntOrNull()
if(id != null) return "${Bangumi.SERVER}/rakuen/topic/$model/$id"

return url
}

private val bgmHosts = arrayOf("bgm.tv", "bangumi.tv", "chii.in")
fun jumpUrl(context: Context, page: String?, openUrl: String): Boolean{
val url = page?.split("#")?.get(0)
if(url == null || url.isNullOrEmpty() || url == openUrl) return false
val rakuen = getRakuen(url)
if(url == null || url.isNullOrEmpty() || rakuen == getRakuen(openUrl)) return false
if(!isBgmPage(url)) return false
val post = Regex("""#post_([0-9]+)$""").find(page)?.groupValues?.get(1)?.toIntOrNull()?:0
//Topic
var regex = Regex("""/m/topic/[^/]*/([0-9]*)$""")
var id = regex.find(url)?.groupValues?.get(1)?.toIntOrNull()
if(id != null){
TopicActivity.startActivity(context, url, post)
return true }
regex = Regex("""/rakuen/topic/([^/]*)/([0-9]*)$""")
var model = regex.find(url)?.groupValues?.get(1)?:""
id = regex.find(url)?.groupValues?.get(2)?.toIntOrNull()
if(id != null){
TopicActivity.startActivity(context, "${Bangumi.SERVER}/m/topic/$model/$id", post)
return true }
regex = Regex("""/([^/]*)/topic/([0-9]*)$""")
model = regex.find(url)?.groupValues?.get(1)?:""
id = regex.find(url)?.groupValues?.get(2)?.toIntOrNull()
if(id != null){
TopicActivity.startActivity(context, "${Bangumi.SERVER}/m/topic/$model/$id", post)
return true }
if(rakuen?.contains("/rakuen/") == true){
TopicActivity.startActivity(context, rakuen, post)
return true
}
//Subject
regex = Regex("""/subject/([0-9]*)$""")
id = regex.find(url)?.groupValues?.get(1)?.toIntOrNull()
val regex = Regex("""/subject/([0-9]*)$""")
val id = regex.find(url)?.groupValues?.get(1)?.toIntOrNull()
if(id != null){
SubjectActivity.startActivity(context, Subject(id, url))
return true }
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/layout/fragment_rakuen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
app:tabSelectedTextColor="?colorAccent"
android:background="?android:colorBackground"
android:stateListAnimator="@animator/elevation"
app:layout_constraintTop_toTopOf="parent"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
android:layout_height="48dp"/>
<android.support.v4.view.ViewPager
android:id="@+id/item_pager"
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/res/layout/item_rakuen_tab.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:focusable="true"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="@+id/item_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp"
android:textColor="@drawable/color_selectable"
tools:text="全部小组"
app:layout_constraintBottom_toTopOf="@android:id/title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="SmallSp"/>

<TextView
android:id="@android:id/title"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Widget.TabWidget"
android:textColor="@drawable/color_selectable"
android:text="@string/rakuen_topic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
12 changes: 12 additions & 0 deletions app/src/main/res/menu/list_topic_filter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/topic_filter_all"
android:title="@string/all_group"/>
<item android:id="@+id/topic_filter_join"
android:title="@string/joined_group"/>
<item android:id="@+id/topic_filter_post"
android:title="@string/self_post"/>
<item android:id="@+id/topic_filter_reply"
android:title="@string/self_reply"/>
</menu>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,10 @@
<string name="notify_inbox">收件箱</string>
<string name="notify_notify">电波提醒</string>

<string name="rakuen_topic">话题</string>
<string name="all_group">全部小组</string>
<string name="joined_group">已加入小组</string>
<string name="self_post">我发表的</string>
<string name="self_reply">我回复的</string>

</resources>

0 comments on commit 729d7a4

Please sign in to comment.