Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamGrzybkowski committed May 14, 2017
2 parents 4537c5d + a144112 commit 2b7e929
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 72 deletions.
65 changes: 57 additions & 8 deletions app/src/main/java/com/adamg/hnreader/adapter/CommentsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import android.app.Activity
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.widget.LinearLayout
import com.adamg.hnreader.R
import com.adamg.hnreader.models.Comment
import com.adamg.hnreader.views.comments.CommentCardModel
import kotlinx.android.synthetic.main.comment_card.view.*


class CommentsAdapter(var context: Activity, var comments: List<Comment>): RecyclerView.Adapter<CommentsAdapter.CommentViewHolder>() {
class CommentsAdapter(var context: Activity,
var commentCardModels: MutableList<CommentCardModel>,
var commentsListener: CommentsListener): RecyclerView.Adapter<CommentsAdapter.CommentViewHolder>() {

private val cardViewMaxWeight = 20

Expand All @@ -21,20 +26,64 @@ class CommentsAdapter(var context: Activity, var comments: List<Comment>): Recyc
}

override fun onBindViewHolder(holder: CommentViewHolder, position: Int) {
holder.bindComment(comments[position])
holder.bindComment(commentCardModels[position])
}

override fun getItemCount() = comments.size
override fun getItemCount() = commentCardModels.size


inner class CommentViewHolder(val view: View): RecyclerView.ViewHolder(view) {
fun bindComment(comment: Comment){
view.commentAuthor.text = comment.user
view.commentAgo.text = comment.time_ago
view.commentContent.text = comment.content

fun bindComment(commentCardModel: CommentCardModel){
view.commentAuthor.text = commentCardModel.comment.user
view.commentAgo.text = commentCardModel.comment.time_ago
view.commentContent.text = commentCardModel.comment.content
if(commentCardModel.comment.comments.isNotEmpty()) {
view.linearLayoutShowMoreComments.visibility = VISIBLE
view.textViewAnswerCount.text = String.format("(%d)", commentCardModel.comment.comments.size)
if(commentCardModel.isExpanded) {
view.textViewHideReplies.visibility = VISIBLE
view.textViewShowReplies.visibility = GONE
} else {
view.textViewShowReplies.visibility = VISIBLE
view.textViewHideReplies.visibility = GONE
}
} else {
view.linearLayoutShowMoreComments.visibility = GONE
}
val layoutParams = view.cardView.layoutParams as LinearLayout.LayoutParams
layoutParams.weight = (cardViewMaxWeight - comment.level).toFloat()
layoutParams.weight = (cardViewMaxWeight - commentCardModel.comment.level).toFloat()
view.cardView.layoutParams = layoutParams
view.textViewShowReplies.setOnClickListener {
commentCardModels.addAll(commentCardModels.indexOf(commentCardModel)+1, commentCardModel.comment.comments.map { comment -> CommentCardModel(comment) })
commentCardModel.isExpanded = true
notifyDataSetChanged()
commentsListener.onCommentsStateChanged(commentCardModels)
}
view.textViewHideReplies.setOnClickListener {
val allCommentsToDelete = getAllComments(commentCardModel.comment.comments)
commentCardModels.removeAll(allCommentsToDelete.map { comment -> CommentCardModel(comment) })
commentCardModel.isExpanded = false
notifyDataSetChanged()
commentsListener.onCommentsStateChanged(commentCardModels)
}
}


private fun getAllComments(comments: List<Comment>): List<Comment>{
var commentList: MutableList<Comment> = mutableListOf()
comments.forEach {
commentList.add(it)
if (it.comments.isNotEmpty()) {
commentList.addAll(getAllComments(it.comments))
}
}
return commentList
}

}

interface CommentsListener {
fun onCommentsStateChanged(commentCardModels: MutableList<CommentCardModel>)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class ItemsAdapter(var context: Context, var stories: List<Item>): RecyclerView.

inner class NewItemsViewHolder(val view: View): RecyclerView.ViewHolder(view), View.OnClickListener {
fun bindStory(story: Item){
view.storyNumber.text = (adapterPosition+1).toString() + "."
view.storyTitle.text = story.title
view.storyBy.text = story.user
view.storyPoints.text = story.points.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class JobsAdapter(var context: Context, var stories: List<Item>): RecyclerView.A

inner class JobsViewHolder(val view: View): RecyclerView.ViewHolder(view), View.OnClickListener {
fun bindStory(story: Item){
view.storyNumber.text = (adapterPosition+1).toString() + "."
view.storyTitle.text = story.title
view.storyTime.text = story.timeAgo
view.storyDomain.text = story.domain
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.adamg.hnreader.views.comments

import com.adamg.hnreader.models.Comment

class CommentCardModel(val comment: Comment, var isExpanded: Boolean = false) {

override fun equals(other: Any?): Boolean {
val commentCardModel = other as CommentCardModel
return comment == commentCardModel.comment
}

override fun hashCode(): Int{
var result = comment.hashCode()
result = 31 * result + isExpanded.hashCode()
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import com.adamg.hnreader.adapter.CommentsAdapter
import com.adamg.hnreader.base.BaseFragmentMvp
import com.adamg.hnreader.dagger.component.CommentsComponent
import com.adamg.hnreader.dagger.component.DaggerCommentsComponent
import com.adamg.hnreader.models.Comment
import com.evernote.android.state.State
import kotlinx.android.synthetic.main.fragment_comments.*

class CommentsFragment: BaseFragmentMvp<CommentsView, CommentsPresenter>(), CommentsView, SwipeRefreshLayout.OnRefreshListener {
class CommentsFragment: BaseFragmentMvp<CommentsView, CommentsPresenter>(), CommentsView,
SwipeRefreshLayout.OnRefreshListener, CommentsAdapter.CommentsListener {

lateinit private var commentsComponent: CommentsComponent
lateinit private var adapter: CommentsAdapter
Expand All @@ -42,7 +42,7 @@ class CommentsFragment: BaseFragmentMvp<CommentsView, CommentsPresenter>(), Com
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
contentView.setOnRefreshListener(this)
adapter = CommentsAdapter(activity, listOf())
adapter = CommentsAdapter(activity, mutableListOf(), this)
recycleView.adapter = adapter
recycleView.layoutManager = LinearLayoutManager(context)
if (state is CommentsModel.Loading) {
Expand All @@ -66,7 +66,7 @@ class CommentsFragment: BaseFragmentMvp<CommentsView, CommentsPresenter>(), Com
is CommentsModel.EmptyResult -> showEmptyResultState()
is CommentsModel.Error -> showErrorState(model.error)
is CommentsModel.Loading -> showLoadingState()
is CommentsModel.Result -> showResultState(model.comments)
is CommentsModel.Result -> showResultState(model.commentCardModels)
}
}

Expand All @@ -77,11 +77,13 @@ class CommentsFragment: BaseFragmentMvp<CommentsView, CommentsPresenter>(), Com
recycleView.visibility = View.GONE
}

private fun showResultState(comments: List<Comment>) {
private fun showResultState(commentCardModel: List<CommentCardModel>) {
contentView.isRefreshing = false
errorView.visibility = View.GONE
recycleView.visibility = View.VISIBLE
adapter.comments = comments
val mutableComments = mutableListOf<CommentCardModel>()
mutableComments.addAll(commentCardModel)
adapter.commentCardModels = mutableComments
adapter.notifyDataSetChanged()
}

Expand All @@ -97,6 +99,10 @@ class CommentsFragment: BaseFragmentMvp<CommentsView, CommentsPresenter>(), Com
recycleView.visibility = View.GONE
}

override fun onCommentsStateChanged(commentCardModels: MutableList<CommentCardModel>) {
state = CommentsModel.Result(commentCardModels)
}

override fun createPresenter() = commentsComponent.presenter()

override fun injectDependencies() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ sealed class CommentsModel: Parcelable {
override fun writeToParcel(dest: Parcel?, flags: Int) {}
}

class Result(val comments: List<Comment>): CommentsModel(){
class Result(val commentCardModels: List<CommentCardModel>): CommentsModel(){
companion object {
@JvmField val CREATOR: Parcelable.Creator<Result> = object : Parcelable.Creator<Result> {
override fun createFromParcel(source: Parcel): Result = Result(source)
override fun newArray(size: Int): Array<Result?> = arrayOfNulls(size)
}
}

constructor(source: Parcel) : this(ArrayList<Comment>().apply{ source.readList(this, Comment::class.java.classLoader) })
constructor(source: Parcel) : this(ArrayList<CommentCardModel>().apply{ source.readList(this, Comment::class.java.classLoader) })

override fun describeContents() = 0

override fun writeToParcel(dest: Parcel?, flags: Int) {
dest?.writeList(comments)
dest?.writeList(commentCardModels)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CommentsPresenter @Inject constructor(val hackerNewsApi: HackerNewsApi): B
if (item.comments.isEmpty()) {
view?.render(CommentsModel.EmptyResult())
} else {
view?.render(CommentsModel.Result(getAllComments(item.comments)))
view?.render(CommentsModel.Result(item.comments.map { comment -> CommentCardModel(comment) }))
}
},
{ error: Throwable -> error.message?.let{
Expand All @@ -29,15 +29,4 @@ class CommentsPresenter @Inject constructor(val hackerNewsApi: HackerNewsApi): B
)
compositeSubscription?.add(subscription)
}

private fun getAllComments(comments: List<Comment>): List<Comment>{
var commentList: MutableList<Comment> = mutableListOf()
comments.forEach {
commentList.add(it)
if (it.comments.isNotEmpty()) {
commentList.addAll(getAllComments(it.comments))
}
}
return commentList
}
}
56 changes: 52 additions & 4 deletions app/src/main/res/layout/comment_card.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools">

<LinearLayout
android:orientation="horizontal"
Expand All @@ -14,15 +15,14 @@
<android.support.v7.widget.CardView
android:id="@+id/cardView"
android:layout_margin="4dp"
android:layout_weight="15"
android:layout_weight="20"
card_view:cardBackgroundColor="@color/card_background"
android:layout_width="0dp"
android:layout_height="wrap_content">

<LinearLayout
android:orientation="vertical"
android:paddingLeft="@dimen/comment_card_padding"
android:paddingRight="@dimen/comment_card_padding"
android:padding="@dimen/padding_normal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

Expand Down Expand Up @@ -53,6 +53,54 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<LinearLayout
android:visibility="visible"
android:id="@+id/linearLayoutShowMoreComments"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:clickable="true"
android:padding="@dimen/padding_normal"
android:id="@+id/textViewShowReplies"
android:textColor="@color/font_gray_light"
android:textAlignment="center"
android:text="@string/show_replies_comments"
android:background="?attr/selectableItemBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:clickable="true"
android:padding="@dimen/padding_normal"
android:visibility="gone"
android:id="@+id/textViewHideReplies"
android:textColor="@color/font_gray_light"
android:textAlignment="center"
android:text="@string/hide_replies_comments"
android:background="?attr/selectableItemBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</FrameLayout>

<TextView
android:id="@+id/textViewAnswerCount"
android:textColor="@color/font_gray_light"
android:textAlignment="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="(20)"/>

</LinearLayout>


</LinearLayout>

</android.support.v7.widget.CardView>
Expand Down
31 changes: 12 additions & 19 deletions app/src/main/res/layout/job_card.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView
android:layout_marginBottom="4dp"
Expand All @@ -12,23 +13,12 @@
android:layout_height="wrap_content">

<RelativeLayout
android:layout_margin="4dp"
android:padding="@dimen/padding_normal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textSize="20sp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:id="@+id/storyNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_marginRight="8dp"
android:layout_toRightOf="@+id/storyNumber"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
Expand All @@ -39,7 +29,8 @@
android:layout_marginBottom="4dp"
android:id="@+id/storyTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
tools:text="Some Job title, this could also take more then one line :D"/>

<RelativeLayout
android:layout_width="match_parent"
Expand All @@ -53,7 +44,8 @@
android:layout_gravity="center_vertical"
android:id="@+id/storyTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
tools:text="2 hours ago"/>

<TextView
android:layout_alignParentRight="true"
Expand All @@ -62,7 +54,8 @@
android:id="@+id/storyDomain"
android:lines="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
tools:text="google.com"/>

</RelativeLayout>

Expand Down
Loading

0 comments on commit 2b7e929

Please sign in to comment.