Skip to content

Commit

Permalink
add edit progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ekibun committed Feb 19, 2019
1 parent 4b91b04 commit f149204
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 15 deletions.
44 changes: 36 additions & 8 deletions app/src/main/java/soko/ekibun/bangumi/api/bangumi/Bangumi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ interface Bangumi {
Pair(tip.trim(':',' '),
it.text().substring(tip.length).trim())
}
val eps_count = infobox?.firstOrNull { it.first == "话数" }?.second?.toIntOrNull()?:subject.eps_count
var eps_count = infobox?.firstOrNull { it.first == "话数" }?.second?.toIntOrNull()?:subject.eps_count
//air-date
val air_date = infobox?.firstOrNull { it.first in arrayOf("放送开始", "上映年度", "开始") }?.second?.replace("/", "-")?.
replace("", "-")?.replace("", "-")?.replace("", "")?:""
Expand Down Expand Up @@ -230,7 +230,20 @@ interface Bangumi {
img.replace("/c/", "/s/"),
img.replace("/c/", "/g/"))
//TODO Collection
//no eps
//prg
val ep_status = doc.selectFirst("input[name=watchedeps]")?.attr("value")?.toIntOrNull()?:0
val vol_status = doc.selectFirst("input[name=watched_vols]")?.attr("value")?.toIntOrNull()?:0
var vol_count = 0
var has_vol = false
doc.select(".prgText")?.forEach {
when(it.selectFirst(".type")?.text()){
"Vol." -> {
has_vol = true
vol_count = it.ownText()?.trim(' ', '/')?.toIntOrNull()?:0
}
else -> eps_count = it.ownText()?.trim(' ', '/')?.toIntOrNull()?:0
}
}
//crt
val crt = doc.select(".subject_section").filter { it.select(".subtitle")?.text() == "角色介绍" }.getOrNull(0)?.select("li")?.map {
val a = it.selectFirst("a.avatar")
Expand Down Expand Up @@ -358,6 +371,7 @@ interface Bangumi {
//formhash
val formhash = if(doc.selectFirst(".guest") != null) "" else doc.selectFirst("input[name=formhash]")?.attr("value")
Subject(subject.id, subject.url, type, name, name_cn, summary, eps_count, air_date, air_weekday, rating, rank, images, infobox = infobox,
ep_status = ep_status, vol_count = vol_count, vol_status = vol_status, has_vol = has_vol,
crt=crt, topic = topic, blog = blog, linked = linked, commend = commend, tags = tags, typeString = typeString, formhash = formhash, interest = interest)
}
}
Expand Down Expand Up @@ -653,7 +667,20 @@ interface Bangumi {
val name = data.attr("data-subject-name")
val name_cn = data.attr("data-subject-name-cn")
val img = getImageUrl(it.selectFirst("img"))
val eps_count = it.selectFirst(".prgBatchManagerForm .grey")?.text()?.trim(' ', '/')?.toIntOrNull()?:0
val ep_status = it.selectFirst("input[name=watchedeps]")?.attr("value")?.toIntOrNull()?:0
var eps_count = it.selectFirst(".prgBatchManagerForm .grey")?.text()?.trim(' ', '/')?.toIntOrNull()?:0
val vol_status = it.selectFirst("input[name=watched_vols]")?.attr("value")?.toIntOrNull()?:0
var vol_count = 0
var has_vol = false
it.select(".prgText")?.forEach {
when(it.selectFirst(".type")?.text()){
"Chap." -> eps_count = it.ownText()?.trim(' ', '/')?.toIntOrNull()?:0
"Vol." -> {
has_vol = true
vol_count = it.ownText()?.trim(' ', '/')?.toIntOrNull()?:0
}
}
}
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val now = Date().time
var cat = "MAIN"
Expand Down Expand Up @@ -686,11 +713,12 @@ interface Bangumi {
val watched_eps = it.selectFirst("input[name=watchedeps]")?.attr("value")?.toIntOrNull()?:0
val watched_vols = it.selectFirst("input[name=watched_vols]")?.attr("value")?.toIntOrNull()?:0
ret += SubjectCollection(name, id, watched_eps, watched_vols, 0, Subject(
id, "$SERVER/subject/$id", type, name, name_cn, eps = eps, eps_count = eps_count,
images = Images(img.replace("/g/", "/l/"),
img.replace("/g/", "/m/"),
img.replace("/g/", "/c/"),
img.replace("/g/", "/s/"), img)))
id, "$SERVER/subject/$id", type, name, name_cn, eps = eps,
eps_count = eps_count, ep_status = ep_status, vol_count = vol_count, vol_status = vol_status, has_vol = has_vol,
images = Images(img.replace("/s/", "/l/"),
img.replace("/s/", "/m/"),
img.replace("/s/", "/c/"), img,
img.replace("/s/", "/s/"))))
}
return@buildHttpCall ret
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ data class Subject(
var tags: List<Pair<String, Int>>? = null,
var typeString: String? = null,
var formhash: String? = null,
var interest: Collection? = null
var interest: Collection? = null,
var ep_status: Int = 0,
var vol_status: Int = 0,
var vol_count: Int = 0,
var has_vol: Boolean = false
){
fun getPrettyName(): String{
return ((if(name_cn.isNullOrEmpty()) name else name_cn)?:"").replace("&amp;", "&")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package soko.ekibun.bangumi.ui.subject

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Rect
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.app.AppCompatActivity
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import kotlinx.android.synthetic.main.dialog_edit_progress.view.*
import okhttp3.FormBody
import soko.ekibun.bangumi.R
import soko.ekibun.bangumi.api.ApiHelper
import soko.ekibun.bangumi.api.bangumi.Bangumi
import soko.ekibun.bangumi.api.bangumi.bean.Subject

class EditProgressDialog: DialogFragment() {
companion object {
fun showDialog(context: AppCompatActivity, subject: Subject, formhash: String, ua: String, callback: ()->Unit){
val dialog = EditProgressDialog()
dialog.subject = subject
dialog.formhash = formhash
dialog.callback = callback
dialog.ua = ua
dialog.show(context.supportFragmentManager, "edit_progress")
}
}

private fun getKeyBoardHeight(): Int{
val rect = Rect()
activity?.window?.decorView?.getWindowVisibleDisplayFrame(rect)
val metrics = DisplayMetrics()
(activity?.getSystemService(Context.WINDOW_SERVICE) as? WindowManager)?.defaultDisplay?.getMetrics(metrics)
return metrics.heightPixels - rect.bottom
}

lateinit var subject: Subject
lateinit var formhash: String
lateinit var ua: String
lateinit var callback: ()->Unit
@SuppressLint("SetTextI18n")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.dialog_edit_progress, container)

view.item_eps.setText(subject.ep_status.toString())
view.item_eps_info.text = "${ if(subject.eps_count == 0) "" else "/${subject.eps_count}" }"
view.item_vol.setText(subject.vol_status.toString())
view.item_vol_info.text = "${ if(subject.vol_count == 0) "" else "/${subject.vol_count}" }"
view.item_vol_panel.visibility = if(subject.has_vol) View.VISIBLE else View.GONE

view.item_outside.setOnClickListener {
dialog.dismiss()
}
view.item_submit.setOnClickListener {
dialog.dismiss()
val body = FormBody.Builder()
.add("referer", "subject")
.add("submit", "更新")
.add("watchedeps", view.item_eps.text.toString())
if(subject.has_vol) body.add("watched_vols", view.item_vol.text.toString())

ApiHelper.buildHttpCall("${Bangumi.SERVER}/subject/set/watched/${subject.id}", mapOf("User-Agent" to ua), body.build()){
it.code() == 200
}.enqueue(ApiHelper.buildCallback(context,{
if(!it) return@buildCallback
subject.ep_status = view.item_eps.text.toString().toIntOrNull()?:0
subject.vol_status = view.item_vol.text.toString().toIntOrNull()?:0
callback()
},{}))
}

activity?.window?.decorView?.viewTreeObserver?.addOnGlobalLayoutListener{
view.item_keyboard.layoutParams?.let{
it.height = getKeyBoardHeight()
view.item_keyboard.layoutParams = it
}
}

dialog.window?.attributes?.let{
it.dimAmount = 0.6f
dialog.window?.attributes = it
}
dialog.window?.setWindowAnimations(R.style.AnimDialog)
dialog.window?.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
return view
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppTheme_Dialog)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class EditSubjectDialog: DialogFragment() {
val dialog = EditSubjectDialog()
dialog.subject = subject
dialog.status = status
dialog.status = status
dialog.formhash = formhash
dialog.callback = callback
dialog.ua = ua
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class SubjectPresenter(private val context: SubjectActivity){
WebActivity.launchUrl(context, "${subject.url}/characters")
}

subjectView.detail.item_progress_edit.setOnClickListener {
EditProgressDialog.showDialog(context, this.subject, context.formhash, context.ua){
refresh()
}
}

context.title_expand.setOnClickListener {
WebActivity.launchUrl(context, subject.url)
}
Expand Down Expand Up @@ -295,9 +301,9 @@ class SubjectPresenter(private val context: SubjectActivity){
}

private fun loadComment(subject: Subject, page: Int){
if(page == 1)
subjectView.commentAdapter.setNewData(null)
Bangumi.getComments(subject, page, context.ua).enqueue(ApiHelper.buildCallback(context, {
if(page == 1)
subjectView.commentAdapter.setNewData(null)
if(it?.isEmpty() == true)
subjectView.commentAdapter.loadMoreEnd()
else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ import kotlinx.android.synthetic.main.subject_episode.view.*
import kotlinx.android.synthetic.main.subject_topic.*
import soko.ekibun.bangumi.R
import soko.ekibun.bangumi.api.bangumi.Bangumi
import soko.ekibun.bangumi.api.bangumi.bean.Episode
import soko.ekibun.bangumi.api.bangumi.bean.Subject
import soko.ekibun.bangumi.api.bangumi.bean.SubjectType
import soko.ekibun.bangumi.api.bangumi.bean.*
import soko.ekibun.bangumi.ui.main.fragment.calendar.CalendarAdapter
import soko.ekibun.bangumi.ui.view.DragPhotoView
import soko.ekibun.bangumi.ui.web.WebActivity
Expand Down Expand Up @@ -263,6 +261,9 @@ class SubjectView(private val context: SubjectActivity){
.show()
else WebActivity.launchUrl(context, subject.url)
}

detail.item_progress.visibility = if(subject.formhash?.isNotEmpty() == true && subject.interest?.status?.type == CollectionStatusType.DO && subject.type in listOf(SubjectType.ANIME, SubjectType.REAL, SubjectType.BOOK)) View.VISIBLE else View.GONE
detail.item_progress_info.text = "看到 ${ if(subject.has_vol) "${subject.vol_status}${ if(subject.vol_count == 0) "" else "/${subject.vol_count}" }" else ""}${subject.ep_status}${ if(subject.eps_count == 0) "" else "/${subject.eps_count}" }"
}

fun updateEpisode(episodes: List<Episode>){
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/res/layout/activity_subject.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,29 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/item_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_progress_info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:padding="12dp"
android:textColor="?android:textColorPrimary"
android:ellipsize="end"/>
<TextView
android:id="@+id/item_progress_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:focusable="true"
android:padding="12dp"/>
</LinearLayout>
<include android:id="@+id/item_episodes"
android:visibility="gone"
layout="@layout/subject_episode"/>
Expand Down
76 changes: 76 additions & 0 deletions app/src/main/res/layout/dialog_edit_progress.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<View android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:id="@+id/item_outside"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:background="?android:colorBackground"
android:orientation="vertical">
<LinearLayout
android:id="@+id/item_eps_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/item_eps"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:maxLines="1"/>
<TextView
android:id="@+id/item_eps_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/item_vol_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/item_vol"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:maxLines="1"/>
<TextView
android:id="@+id/item_vol_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/item_buttons"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp"/>
<TextView
android:id="@+id/item_submit"
android:layout_width="wrap_content"
android:tint="?android:textColorSecondary"
android:padding="12dp"
android:tintMode="src_in"
android:background="?android:attr/selectableItemBackground"
android:layout_height="wrap_content"
android:textColor="?colorAccent"
android:text="更新"/>
</LinearLayout>
<View android:id="@+id/item_keyboard"
android:layout_width="match_parent"
android:layout_height="0dp"/>
</LinearLayout>
</LinearLayout>

0 comments on commit f149204

Please sign in to comment.