Skip to content

Commit

Permalink
Progress sync and display
Browse files Browse the repository at this point in the history
  • Loading branch information
royaff0 committed Jun 24, 2017
1 parent 234cd41 commit ab48228
Show file tree
Hide file tree
Showing 13 changed files with 266 additions and 68 deletions.
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile "com.android.support:recyclerview-v7:25.3.1"
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

Expand Down
3 changes: 3 additions & 0 deletions common/src/main/java/com/sqrtf/common/api/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ interface ApiService {

@POST("/api/watch/favorite/bangumi/{bangumi_id}")
fun uploadFavoriteStatus(@Path("bangumi_id") bangumiId: String, @Body body: FavoriteChangeRequest): Observable<MessageResponse>

@POST("/api/watch/history/{episode_id}")
fun uploadWatchHistory(@Path("episode_id") episodeId: String, @Body body: HistoryChangeRequest): Observable<MessageResponse>
}
4 changes: 3 additions & 1 deletion common/src/main/java/com/sqrtf/common/api/Requests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ val FavoriteStatus_WATCHING = 3
val FavoriteStatus_PAUSE = 4
val FavoriteStatus_ABANDONED = 5

data class FavoriteChangeRequest(val status: Int)
data class FavoriteChangeRequest(val status: Int)

data class HistoryChangeRequest(val bangumi_id: String, val last_watch_position: Long, val percentage: Float, val is_finished: Boolean)
1 change: 1 addition & 0 deletions common/src/main/java/com/sqrtf/common/model/Episode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ public class Episode {
public String duration;
public String id;

public WatchProgress watch_progress;
}
10 changes: 10 additions & 0 deletions common/src/main/java/com/sqrtf/common/model/WatchProgress.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.sqrtf.common.model;

/**
* Created by roya on 2017/6/23.
*/

public class WatchProgress {
public Float percentage;
public Double last_watch_time;
}
76 changes: 76 additions & 0 deletions common/src/main/java/com/sqrtf/common/view/ProgressCoverView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.sqrtf.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.sqrtf.common.R;

/**
* Created by roya on 2017/6/23.
*/

public class ProgressCoverView extends View {

private float progress = 0f;
private Paint colorPaint;
private Paint colorSecondaryPaint;

public ProgressCoverView(Context context) {
this(context, null);
}

public ProgressCoverView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

public ProgressCoverView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProgressCoverView);
progress = a.getFloat(R.styleable.ProgressCoverView_progress, 0f);
int color = a.getColor(R.styleable.ProgressCoverView_color, 0);
int colorSecondary = a.getColor(R.styleable.ProgressCoverView_colorSecondary, 0);
a.recycle();

colorPaint = new Paint();
colorPaint.setColor(color);

colorSecondaryPaint = new Paint();
colorSecondaryPaint.setColor(colorSecondary);
}

public void setColor(int color) {
colorPaint = new Paint();
colorPaint.setColor(color);
postInvalidate();
}

public void setColorSecondary(int colorSecondary) {
colorSecondaryPaint = new Paint();
colorSecondaryPaint.setColor(colorSecondary);
postInvalidate();
}

public void setProgress(float progress) {
this.progress = progress;
postInvalidate();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int r1 = (int) (getMeasuredWidth() * progress);
canvas.drawRect(0, 0, r1, getMeasuredHeight(), colorPaint);
canvas.drawRect(r1, 0, getMeasuredWidth(), getMeasuredHeight(), colorSecondaryPaint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.sqrtf.common.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

public class ScrollStartLayoutManager extends LinearLayoutManager {

public ScrollStartLayoutManager(Context context) {
super(context);
}

public ScrollStartLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}

public ScrollStartLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}

private static class CenterSmoothScroller extends LinearSmoothScroller {

CenterSmoothScroller(Context context) {
super(context);
}

@Override
public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
return boxStart - viewStart;
}

@Override
protected int calculateTimeForScrolling(int dx) {
return super.calculateTimeForScrolling(dx) * 3;
}
}
}
7 changes: 7 additions & 0 deletions common/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@
<declare-styleable name="FixedAspectRatioFrameLayout">
<attr name="aspectRatio" format="float"/>
</declare-styleable>

<declare-styleable name="ProgressCoverView">
<attr name="progress" format="float"/>
<attr name="color" format="color"/>
<attr name="colorSecondary" format="color"/>
</declare-styleable>

</resources>
3 changes: 0 additions & 3 deletions mobile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
android:label="@string/title_activity_player"
android:screenOrientation="sensorLandscape"
android:theme="@style/FullscreenTheme" />
<activity
android:name=".PlayPaddingActivity"
android:theme="@style/PaddingViewTheme" />
<activity android:name=".FavoriteActivity" />
<activity android:name=".AllBangumiActivity" />
<activity
Expand Down
97 changes: 84 additions & 13 deletions mobile/src/main/java/com/sqrtf/megumin/DetailActivity.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.sqrtf.megumin

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.text.TextUtils
Expand All @@ -17,22 +17,30 @@ import com.sqrtf.common.activity.BaseActivity
import com.sqrtf.common.api.ApiClient
import com.sqrtf.common.api.ApiHelper
import com.sqrtf.common.api.FavoriteChangeRequest
import com.sqrtf.common.api.HistoryChangeRequest
import com.sqrtf.common.cache.JsonUtil
import com.sqrtf.common.model.Bangumi
import com.sqrtf.common.model.BangumiDetail
import com.sqrtf.common.model.Episode
import com.sqrtf.common.view.ScrollStartLayoutManager
import com.sqrtf.common.view.ProgressCoverView
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers


class DetailActivity : BaseActivity() {

companion object {
public fun intent(context: Context, bgm: Bangumi): Intent {
fun intent(context: Context, bgm: Bangumi): Intent {
val intent = Intent(context, DetailActivity::class.java)
val json = JsonUtil.toJson(bgm)
intent.putExtra(INTENT_KEY_BANGMUMI, json)
return intent
}

private val INTENT_KEY_BANGMUMI = "INTENT_KEY_BANGMUMI"
private val REQUEST_CODE = 0x81
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -49,8 +57,12 @@ class DetailActivity : BaseActivity() {
checkNotNull(bgm)

setData(bgm!!)
loadData(bgm.id)
}

ApiClient.getInstance().getBangumiDetail(bgm.id)
private fun loadData(bgmId: String) {
showToast("load data")
ApiClient.getInstance().getBangumiDetail(bgmId)
.withLifecycle()
.subscribe({
setData(it.getData())
Expand All @@ -70,8 +82,47 @@ class DetailActivity : BaseActivity() {
}
}

private fun playVideo(episode: Episode) {
assert(!TextUtils.isEmpty(episode.id))

ApiClient.getInstance().getEpisodeDetail(episode.id)
.withLifecycle()
.subscribe({
startActivityForResult(PlayerActivity.intent(this,
it.video_files[0].url,
episode.id,
episode.bangumi_id),
DetailActivity.REQUEST_CODE)
}, {
toastErrors()
})
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == DetailActivity.REQUEST_CODE
&& resultCode == Activity.RESULT_OK
&& data != null) {
val id = data.getStringExtra(PlayerActivity.RESULT_KEY_ID)
val bgmId = data.getStringExtra(PlayerActivity.RESULT_KEY_ID_2)
val duration = data.getLongExtra(PlayerActivity.RESULT_KEY_DURATION, 0)
val position = data.getLongExtra(PlayerActivity.RESULT_KEY_POSITION, 0)
ApiClient.getInstance().uploadWatchHistory(id,
HistoryChangeRequest(bgmId,
position / 100,
position.toFloat() / duration,
duration == position))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
Consumer {
loadData(bgmId)
},
ignoreErrors())
}
}

private fun setData(detail: Bangumi) {
var iv = findViewById(R.id.image) as ImageView?
val iv = findViewById(R.id.image) as ImageView?
val ivCover = findViewById(R.id.image_cover) as ImageView
val ctitle = findViewById(R.id.title) as TextView
val subtitle = findViewById(R.id.subtitle) as TextView
Expand Down Expand Up @@ -131,29 +182,37 @@ class DetailActivity : BaseActivity() {
}

if (detail is BangumiDetail && detail.episodes != null && detail.episodes.isNotEmpty()) {
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recyclerView.layoutManager = ScrollStartLayoutManager(this, ScrollStartLayoutManager.HORIZONTAL, false)
recyclerView.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
inner class VH(v: View) : RecyclerView.ViewHolder(v) {
val view = v
val tv = v.findViewById(R.id.tv) as TextView
val image = v.findViewById(R.id.image) as ImageView
val progress = v.findViewById(R.id.progress) as ProgressCoverView
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, p1: Int) {
if (holder is VH) {
val d = detail.episodes[p1]

holder.tv.text = (p1 + 1).toString() + ". " + d.name_cn
if (d.watch_progress?.percentage != null
&& d.watch_progress?.percentage!! < 0.15f) {
d.watch_progress?.percentage = 0.15f
}
holder.progress.setProgress(d.watch_progress?.percentage ?: 0f)

Glide.with(this@DetailActivity)
.load(ApiHelper.fixHttpUrl(d.thumbnail))
.into(holder.image)

if (d.status != 0) {
holder.tv.text = (p1 + 1).toString() + ". " + d.name_cn
Glide.with(this@DetailActivity)
.load(ApiHelper.fixHttpUrl(d.thumbnail))
.into(holder.image)
holder.tv.alpha = 1f
holder.view.setOnClickListener {
startActivity(PlayPaddingActivity.intent(this@DetailActivity, d.id))
playVideo(d)
}
} else {
holder.image.setImageDrawable(null)
// holder.tv.text = "未更新"
holder.tv.text = (p1 + 1).toString() + ". " + d.name_cn
holder.tv.alpha = 0.3f
holder.view.setOnClickListener(null)
}
}
Expand All @@ -168,6 +227,18 @@ class DetailActivity : BaseActivity() {
}

}

detail.episodes
.map { it?.watch_progress?.last_watch_time }
.withIndex()
.filter { it.value != null }
.sortedBy { it.value }
.lastOrNull()
?.let {
recyclerView.post {
recyclerView.smoothScrollToPosition(it.index)
}
}
}

}
Expand Down
Loading

0 comments on commit ab48228

Please sign in to comment.