Commit a2063287 authored by Filip Wiesner's avatar Filip Wiesner

News (Task) in ViewPager - WIP

parent 103984c5
package com.cvut.blackbird.flows.tasks
import android.animation.LayoutTransition
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
......@@ -10,30 +9,29 @@ import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.*
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.view.animation.AnimationUtils
import android.widget.LinearLayout
import android.widget.TextView
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.R
import com.cvut.blackbird.extensions.*
import com.cvut.blackbird.model.entities.Event
import com.cvut.blackbird.model.entities.EventType
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
import org.joda.time.Minutes
import android.view.animation.AnimationUtils
import android.widget.LinearLayout
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isGone
import androidx.core.view.marginTop
import com.cvut.blackbird.extensions.*
import kotlinx.android.synthetic.main.tasks_news_layout.view.*
sealed class Task(val id: Int)
......@@ -164,37 +162,31 @@ class NewsViewHolder(view: View, context: Context) : TaskViewHolder(view, contex
override fun bind(task: Task, clickListener: (Task) -> Unit) {
val newsTask = (task as? NewsTask) ?: throw TypeCastException("You have to pass NewsTask to NewsViewHolder dumbass")
val news = newsTask.news.asReversed()
// (view as CardView).layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
// view.news_storiesLayout.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
fun onStoryClicked(news: News) {
newsTask.lastClickedStory = news
clickListener(task)
}
view.news_header.setHtmlText(news.first().title)
view.news_header.setOnClickListener { onStoryClicked(news.first()) }
val storyLayout = view.news_storiesLayout
for (story in news.takeLast(news.size - 1)) {
storyLayout.addView(TextView(context).apply {
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT).apply {
setMargins(0,8.dpToPx, 0,0) }
setHtmlText(story.title)
setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
setTextColor(Color.WHITE)
val pager = view.news_pager
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)
setOnClickListener {
newsTask.lastClickedStory = news[position]
clickListener(task)
}
}
container.addView(article)
return article
}
setOnClickListener { onStoryClicked(story) }
})
override fun destroyItem(container: ViewGroup, position: Int, item: Any) =
container.removeView(item as TextView)
override fun getCount() = news.size
override fun isViewFromObject(view: View, item: Any) = view == item
}
view.news_expandBtn.setOnClickListener {
if (storyLayout.isGone) {
storyLayout.setVisible()
} else {
storyLayout.setGone()
}
}
(itemView.layoutParams as StaggeredGridLayoutManager.LayoutParams)
.isFullSpan = true
......@@ -207,5 +199,13 @@ class NewsViewHolder(view: View, context: Context) : TaskViewHolder(view, contex
} else Html.fromHtml(text)
)
}
fun TextView.setupNewsText() {
layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT).apply {
setMargins(0,8.dpToPx, 0,0) }
setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
setTextColor(Color.WHITE)
}
}
\ No newline at end of file
......@@ -34,15 +34,20 @@ abstract class BlackBirdModel {
protected suspend fun refreshToken(): Result<Unit> {
Log.d(BlackBirdAC.LOG_TAG, "Refreshing token")
val result = fetch(authService.refreshToken())
return when (result) {
is Success -> {
AuthInfo.accessToken = result.value.token
val result: Result<Unit>
try {
val response = authService.refreshToken().execute()
result = if (response.isSuccessful && response.body() != null) {
AuthInfo.accessToken = response.body()!!.token
Log.d(BlackBirdAC.LOG_TAG, "Token refreshed")
Success(Unit)
} else {
Failure(response.errorBody()?.string() ?: "No error message")
}
is Failure -> Failure(result.message)
else -> Failure("Unexpected error")
} catch (e: Throwable) {
return Failure(e.localizedMessage)
}
return result
}
......
package com.cvut.blackbird.support.dynamicViewPager
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.viewpager.widget.ViewPager
class WrappingViewPager : ViewPager {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
var heightMeasureSpec: Int
var height = 0
val child = getChildAt(1)
child.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
val h = child.measuredHeight
if (h > height) height = h
heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true">
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thickness="3dp"
android:useLevel="false">
<solid android:color="@color/colorAccent"/>
</shape>
</item>
<item>
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thickness="2dp"
android:useLevel="false">
<solid android:color="@color/colorPrimaryDark"/>
</shape>
</item>
</selector>
......@@ -14,7 +14,7 @@
android:layout_height="match_parent">
<ImageView
android:id="@+id/newsIc"
android:id="@+id/news_ic"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
......@@ -24,48 +24,36 @@
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_newspaper" />
<TextView
android:id="@+id/news_header"
<com.cvut.blackbird.support.dynamicViewPager.WrappingViewPager
android:id="@+id/news_pager"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textColor="@android:color/white"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/newsIc"
app:layout_constraintTop_toTopOf="parent"
tools:text="A header article that will be at the top, attracting user attention" />
<LinearLayout
android:id="@+id/news_storiesLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_height="50dp"
android:layout_marginStart="12dp"
android:layout_marginEnd="16dp"
android:orientation="vertical"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/news_header">
</LinearLayout>
app:layout_constraintStart_toEndOf="@+id/news_ic"
app:layout_constraintTop_toTopOf="@+id/news_ic" />
<Button
android:id="@+id/news_expandBtn"
style="@style/Widget.MaterialComponents.Button.TextButton.Dialog"
android:layout_width="wrap_content"
android:layout_height="35dp"
<com.google.android.material.tabs.TabLayout
android:id="@+id/news_dotTabs"
android:layout_width="160dp"
android:layout_height="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="MORE NEWS"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/news_storiesLayout"
app:layout_constraintVertical_bias="1.0" />
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/news_pager"
app:tabBackground="@drawable/dot_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp"
app:tabIndicatorFullWidth="false"
app:tabPadding="0dp"
app:tabMaxWidth="10dp"
app:tabMode="scrollable"
app:tabRippleColor="@android:color/transparent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
\ No newline at end of file
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