Commit e77895a0 authored by Filip Wiesner's avatar Filip Wiesner

NewsTask animated (BUG: stucks at fast swipe)

parent cad14190
......@@ -53,7 +53,7 @@ dependencies {
implementation "org.jetbrains.anko:anko-commons:$anko_version"
implementation 'androidx.core:core-ktx:1.0.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.25.3'
implementation "org.jetbrains.kotlin:kotlin-reflect:1.2.70"
implementation "org.jetbrains.kotlin:kotlin-reflect:1.2.71"
implementation 'com.github.magneticflux:kotlin-livedata-utils:0.3.3'
//Navigation
......
......@@ -3,6 +3,7 @@
package="com.cvut.blackbird">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<application
android:name=".BlackBirdAC"
......
......@@ -24,7 +24,7 @@ class BlackBirdMain : AppCompatActivity() {
supportActionBar?.hide()
navController = NavHostFragment.findNavController(navHost)
setupViewModels()
// setupViewModels()
if (UserInfo.username.isBlank())
navController.navigate(R.id.toAuth)
......
package com.cvut.blackbird.flows.tasks
import android.animation.LayoutTransition
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Build
import android.text.Html
import android.util.Log
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
......@@ -14,13 +16,14 @@ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.view.animation.AnimationUtils
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.doOnLayout
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.cvut.blackbird.BlackBirdAC
import com.cvut.blackbird.R
import com.cvut.blackbird.extensions.*
import com.cvut.blackbird.model.entities.Event
......@@ -29,6 +32,7 @@ import com.cvut.blackbird.model.entities.News
import com.cvut.blackbird.support.kolor.Kolor
import kotlinx.android.synthetic.main.exam_list_row.view.*
import kotlinx.android.synthetic.main.tasks_news_layout.view.*
import kotlinx.android.synthetic.main.tasks_pinned.view.*
import org.joda.time.DateTime
import org.joda.time.Days
......@@ -164,37 +168,34 @@ class NewsViewHolder(view: View, context: Context) : TaskViewHolder(view, contex
val newsTask = (task as? NewsTask) ?: throw TypeCastException("You have to pass NewsTask to NewsViewHolder dumbass")
val news = newsTask.news.asReversed()
val pager = view.news_pager
(view as CardView).layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
view.news_dotTabs.setupWithViewPager(pager)
pager.adapter = object : PagerAdapter() {
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val article = TextView(context).apply {
setupNewsText()
setHtmlText(news[position].title)
setupArticleLayout()
tag = position
setOnClickListener { clickListener(task) }
}
//TODO Make array of Article Views and measure them?
container.addView(article)
return article
}
override fun destroyItem(container: ViewGroup, position: Int, item: Any) =
container.removeView(item as TextView)
container.removeView(item as View)
override fun getCount() = news.size
override fun isViewFromObject(view: View, item: Any) = view == item
}
pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) = Unit
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) = Unit
override fun onPageSelected(position: Int) {
newsTask.lastClickedStory = news[position]
}
pager.addOnSelectedListener { position ->
newsTask.lastClickedStory = news[position]
pager.requestLayout()
}
})
(itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams)
.isFullSpan = true
view.setOnClickListener { clickListener(task) }
}
fun TextView.setHtmlText(text: String) = setText(
......@@ -203,13 +204,20 @@ class NewsViewHolder(view: View, context: Context) : TaskViewHolder(view, contex
} else Html.fromHtml(text)
)
fun TextView.setupNewsText() {
fun TextView.setupArticleLayout() {
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT).apply {
setMargins(0,8.dpToPx, 0,0) }
setMargins(0,8.dpToPx, 0,0)
}
setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
setTextColor(Color.WHITE)
}
fun ViewPager.addOnSelectedListener(listener: (position: Int) -> Unit) {
addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
override fun onPageScrollStateChanged(state: Int) = Unit
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) = Unit
override fun onPageSelected(position: Int) { listener(position) }
})
}
}
\ No newline at end of file
......@@ -34,10 +34,9 @@ abstract class BlackBirdModel {
protected suspend fun refreshToken(): Result<Unit> {
Log.d(BlackBirdAC.LOG_TAG, "Refreshing token")
val result: Result<Unit>
try {
return try {
val response = authService.refreshToken().execute()
result = if (response.isSuccessful && response.body() != null) {
if (response.isSuccessful && response.body() != null) {
AuthInfo.accessToken = response.body()!!.token
Log.d(BlackBirdAC.LOG_TAG, "Token refreshed")
Success(Unit)
......@@ -45,9 +44,8 @@ abstract class BlackBirdModel {
Failure(response.errorBody()?.string() ?: "No error message")
}
} catch (e: Throwable) {
return Failure(e.localizedMessage)
Failure(e.localizedMessage)
}
return result
}
......
......@@ -17,13 +17,13 @@ class NewsRoot(
fun getNewsLink(): String =
if (false) {
when (UserInfo.facultyAbbr) {
Constants.FACULTY_FEL -> "http://www.fel.cvut.cz/aktuality/rss.xml"
Constants.FACULTY_FEL -> "https://www.fel.cvut.cz/aktuality/rss.xml"
Constants.FACULTY_FIT -> "https://fit.cvut.cz/rss-novinky.xml"
else -> throw InvalidKeyException("Faculty ${UserInfo.facultyAbbr} is not supported!")
}
} else {
when (UserInfo.facultyAbbr) {
Constants.FACULTY_FEL -> "http://www.fel.cvut.cz/en/aktuality/rss-en.xml"
Constants.FACULTY_FEL -> "https://www.fel.cvut.cz/en/aktuality/rss-en.xml"
Constants.FACULTY_FIT -> "https://fit.cvut.cz/rss-news.xml"
else -> throw InvalidKeyException("Faculty ${UserInfo.facultyAbbr} is not supported!")
}
......
......@@ -2,81 +2,19 @@ package com.cvut.blackbird.support.dynamicViewPager
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import androidx.viewpager.widget.ViewPager
import android.view.animation.Animation
import android.view.animation.Transformation
import android.widget.LinearLayout
import com.google.gson.util.VersionUtils
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.PagerAdapter
import com.github.florent37.kotlin.pleaseanimate.core.Expectations
import com.github.florent37.kotlin.pleaseanimate.please
class WrappingViewPager : ViewPager {
private var mAnimStarted = false
class MeasuringViewPager : ViewPager {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
var heightMeasureSpec = heightMeasureSpec
if (!mAnimStarted && null != adapter) {
var height: Int
val child = (adapter as PagerAdapter).instantiateItem(LinearLayout(context), currentItem) as View
child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
height = child.measuredHeight
if (height < minimumHeight) {
height = minimumHeight
}
// Not the best place to put this animation, but it works pretty good.
val newHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
if (layoutParams.height != 0 && heightMeasureSpec != newHeight) {
val targetHeight = height
val currentHeight = layoutParams.height
val heightChange = targetHeight - currentHeight
val a = object : Animation() {
override fun applyTransformation(interpolatedTime: Float, t: Transformation) {
if (interpolatedTime >= 1) {
layoutParams.height = targetHeight
} else {
val stepHeight = (heightChange * interpolatedTime).toInt()
layoutParams.height = currentHeight + stepHeight
}
requestLayout()
}
override fun willChangeBounds(): Boolean {
return true
}
}
val v = findViewWithTag<View>(currentItem)
v.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
a.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation) {
mAnimStarted = true
}
override fun onAnimationEnd(animation: Animation) {
mAnimStarted = false
}
override fun onAnimationRepeat(animation: Animation) = Unit
})
a.duration = 100
mAnimStarted = true
startAnimation(a)
} else {
heightMeasureSpec = newHeight
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
super.onMeasure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(v.getMeasuredHeight(), View.MeasureSpec.EXACTLY))
}
}
\ No newline at end of file
......@@ -6,7 +6,8 @@
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:elevation="4dp"
app:cardCornerRadius="18dp">
app:cardCornerRadius="18dp"
android:animateLayoutChanges="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/news_layout"
......@@ -24,15 +25,16 @@
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_newspaper" />
<com.cvut.blackbird.support.dynamicViewPager.WrappingViewPager
<com.cvut.blackbird.support.dynamicViewPager.MeasuringViewPager
android:id="@+id/news_pager"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/news_ic"
app:layout_constraintTop_toTopOf="@+id/news_ic" />
app:layout_constraintTop_toTopOf="@+id/news_ic"
android:animateLayoutChanges="true"/>
<com.google.android.material.tabs.TabLayout
android:id="@+id/news_dotTabs"
......
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.70'
ext.kotlin_version = '1.2.71'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0-alpha10'
classpath 'com.android.tools.build:gradle:3.3.0-alpha12'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha05"
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha06"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
......
#Wed Sep 19 14:46:37 CEST 2018
#Mon Oct 01 10:11:39 CEST 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment