Skip to content

Commit

Permalink
Improved RelatedActivity. Beta 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
feelfreelinux committed Jan 21, 2018
1 parent e3fd4ff commit de73b6c
Show file tree
Hide file tree
Showing 30 changed files with 399 additions and 60 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ android {
applicationId "io.github.feelfreelinux.wykopmobilny"
minSdkVersion 17
targetSdkVersion 27
versionCode 19
versionName "0.4.2"
versionCode 20
versionName "0.5.0"

def credentialsPropertiesFile = rootProject.file("credentials.properties")
def credentialsProperties = new Properties()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.github.feelfreelinux.wykopmobilny.models.dataclass.*
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.DigResponse
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.FavoriteResponse
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.LinkVoteResponse
import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.VoteResponse
import io.reactivex.Single

interface LinksApi {
Expand All @@ -17,6 +18,8 @@ interface LinksApi {

fun commentVoteUp(linkId: Int) : Single<LinkVoteResponse>
fun commentVoteDown(linkId: Int) : Single<LinkVoteResponse>
fun relatedVoteUp(relatedId: Int) : Single<VoteResponse>
fun relatedVoteDown(relatedId: Int) : Single<VoteResponse>
fun commentVoteCancel(linkId: Int) : Single<LinkVoteResponse>
fun commentDelete(commentId: Int) : Single<LinkComment>
fun commentAdd(body : String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ class LinksRepository(val retrofit: Retrofit, val userTokenRefresher: UserTokenR
.retryWhen(userTokenRefresher)
.compose<LinkVoteResponse>(ErrorHandlerTransformer())

override fun relatedVoteUp(relatedId: Int): Single<VoteResponse> =
linksApi.relatedVoteUp(relatedId)
.retryWhen(userTokenRefresher)
.compose<VoteResponse>(ErrorHandlerTransformer())


override fun relatedVoteDown(relatedId: Int): Single<VoteResponse> =
linksApi.relatedVoteDown(relatedId)
.retryWhen(userTokenRefresher)
.compose<VoteResponse>(ErrorHandlerTransformer())

override fun commentVoteCancel(linkId: Int): Single<LinkVoteResponse> =
linksApi.commentVoteCancel(linkId)
.retryWhen(userTokenRefresher)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ interface LinksRetrofitApi {
fun voteDown(@Path("linkId") linkId : Int,
@Path("voteType") reason : Int) : Single<WykopApiResponse<DigResponse>>

@GET("/links/relatedvoteup/0/{relatedId}/appkey/$APP_KEY")
fun relatedVoteUp(@Path("relatedId") relatedId : Int) : Single<WykopApiResponse<VoteResponse>>

@GET("/links/relatedvotedown/1/{relatedId}/appkey/$APP_KEY")
fun relatedVoteDown(@Path("relatedId") relatedId : Int) : Single<WykopApiResponse<VoteResponse>>

@GET("/links/voteremove/{linkId}/appkey/$APP_KEY")
fun voteRemove(@Path("linkId") linkId : Int) : Single<WykopApiResponse<DigResponse>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package io.github.feelfreelinux.wykopmobilny.models.dataclass
data class Related(
val id : Int,
val url : String,
val voteCount : Int,
var voteCount : Int,
val author : Author,
val title : String
val title : String,
var userVote : Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.github.feelfreelinux.wykopmobilny.models.fragments

data class SortedPagedDataModel<out T : Any> (val page : Int, val sortby : String, val model : T)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models.RelatedResp
class RelatedMapper {
companion object : Mapper<RelatedResponse, Related> {
override fun map(value: RelatedResponse): Related {
return Related(value.id, value.url, value.voteCount, AuthorMapper.map(value.author), value.title)
return Related(value.id, value.url, value.voteCount, AuthorMapper.map(value.author), value.title, value.userVote ?: 0)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package io.github.feelfreelinux.wykopmobilny.models.pojo.apiv2.models

import com.squareup.moshi.Json

class RelatedResponse (
@Json(name="id")
val id : Int,
@Json(name="url")
val url : String,
@Json(name="vote_count")
val voteCount : Int,
@Json(name="author")
val author : AuthorResponse,
val title : String
@Json(name="title")
val title : String,

@Json(name="user_vote")
val userVote : Int?
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import android.view.ViewGroup
import io.github.feelfreelinux.wykopmobilny.R
import io.github.feelfreelinux.wykopmobilny.models.dataclass.Related
import io.github.feelfreelinux.wykopmobilny.ui.adapters.viewholders.RelatedViewHolder
import io.github.feelfreelinux.wykopmobilny.ui.widgets.link.related.RelatedWidgetPresenterFactory
import io.github.feelfreelinux.wykopmobilny.utils.usermanager.UserManagerApi
import javax.inject.Inject

class RelatedListAdapter @Inject constructor(val userManagerApi: UserManagerApi) : RecyclerView.Adapter<RelatedViewHolder>() {
class RelatedListAdapter @Inject constructor(val userManagerApi: UserManagerApi, val relatedWidgetPresenterFactory: RelatedWidgetPresenterFactory) : RecyclerView.Adapter<RelatedViewHolder>() {
val dataset = ArrayList<Related>()

override fun onBindViewHolder(holder: RelatedViewHolder?, position: Int) {
Expand All @@ -19,5 +20,5 @@ class RelatedListAdapter @Inject constructor(val userManagerApi: UserManagerApi)
override fun getItemCount(): Int = dataset.size

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RelatedViewHolder =
RelatedViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.link_related_list_item, parent, false), userManagerApi)
RelatedViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.link_related_list_item, parent, false), userManagerApi, relatedWidgetPresenterFactory.create())
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package io.github.feelfreelinux.wykopmobilny.ui.adapters.viewholders
import android.support.v7.widget.RecyclerView
import android.view.View
import io.github.feelfreelinux.wykopmobilny.models.dataclass.Related
import io.github.feelfreelinux.wykopmobilny.ui.widgets.link.related.RelatedWidgetPresenter
import io.github.feelfreelinux.wykopmobilny.utils.usermanager.UserManagerApi
import kotlinx.android.synthetic.main.link_related_list_item.view.*

class RelatedViewHolder(val view: View, val userManagerApi: UserManagerApi) : RecyclerView.ViewHolder(view) {
class RelatedViewHolder(val view: View, val userManagerApi: UserManagerApi, val relatedWidgetPresenter: RelatedWidgetPresenter) : RecyclerView.ViewHolder(view) {
fun bindView(related: Related) {
view.relatedView.setRelatedData(related, userManagerApi)
view.relatedView.setRelatedData(related, userManagerApi, relatedWidgetPresenter)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.view.ViewGroup
import io.github.feelfreelinux.wykopmobilny.R
import io.github.feelfreelinux.wykopmobilny.base.BaseActivity
import io.github.feelfreelinux.wykopmobilny.base.BaseFragment
import io.github.feelfreelinux.wykopmobilny.ui.modules.search.SearchFragmentNotifier
import kotlinx.android.synthetic.main.activity_mywykop.*

class FavoriteFragment : BaseFragment() {
Expand Down Expand Up @@ -36,4 +37,13 @@ class FavoriteFragment : BaseFragment() {
(activity as BaseActivity).supportActionBar?.setTitle(R.string.favourite)

}

override fun onPause() {
super.onPause()
if (isRemoving)
for (i in 0 until pagerAdapter.registeredFragments.size()) {
(pagerAdapter.registeredFragments.valueAt(i) as FavoriteFragmentNotifier)
.removeDataFragment()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.feelfreelinux.wykopmobilny.ui.modules.favorite

interface FavoriteFragmentNotifier {
fun removeDataFragment()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.res.Resources
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.util.SparseArray
import android.view.ViewGroup
import io.github.feelfreelinux.wykopmobilny.R
import io.github.feelfreelinux.wykopmobilny.ui.modules.favorite.entry.EntryFavoriteFragment
import io.github.feelfreelinux.wykopmobilny.ui.modules.favorite.links.LinksFavoriteFragment
Expand All @@ -12,13 +14,28 @@ import io.github.feelfreelinux.wykopmobilny.ui.modules.mywykop.tags.MyWykopTagsF
import io.github.feelfreelinux.wykopmobilny.ui.modules.mywykop.users.MyWykopUsersFragment

class FavoritePagerAdapter(val resources : Resources, fragmentManager: FragmentManager) : FragmentPagerAdapter(fragmentManager) {
override fun getItem(position: Int): Fragment = when(position) {
0 -> LinksFavoriteFragment.newInstance()
else -> EntryFavoriteFragment.newInstance()
val registeredFragments = SparseArray<Fragment>()

override fun getItem(position: Int): Fragment {
return when(position) {
0 -> LinksFavoriteFragment.newInstance()
else -> EntryFavoriteFragment.newInstance()
}
}

override fun getCount() = 2

override fun instantiateItem(container: ViewGroup, position: Int): Any {
val fragment = super.instantiateItem(container, position) as Fragment
registeredFragments.put(position, fragment)
return fragment
}

override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
registeredFragments.removeAt(position)
super.destroyItem(container, position, `object`)
}

override fun getPageTitle(position: Int): CharSequence {
super.getPageTitle(position)
return when(position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import io.github.feelfreelinux.wykopmobilny.models.fragments.PagedDataModel
import io.github.feelfreelinux.wykopmobilny.models.fragments.getDataFragmentInstance
import io.github.feelfreelinux.wykopmobilny.models.fragments.removeDataFragment
import io.github.feelfreelinux.wykopmobilny.ui.adapters.FeedAdapter
import io.github.feelfreelinux.wykopmobilny.ui.modules.favorite.FavoriteFragmentNotifier
import io.github.feelfreelinux.wykopmobilny.utils.printout
import javax.inject.Inject

class EntryFavoriteFragment : BaseFeedFragment<Entry>(), EntryFavoriteView {
class EntryFavoriteFragment : BaseFeedFragment<Entry>(), EntryFavoriteView, FavoriteFragmentNotifier {
@Inject override lateinit var feedAdapter : FeedAdapter
@Inject lateinit var presenter : EntryFavoritePresenter
lateinit var dataFragment : DataFragment<PagedDataModel<List<Entry>>>
Expand Down Expand Up @@ -48,8 +50,7 @@ class EntryFavoriteFragment : BaseFeedFragment<Entry>(), EntryFavoriteView {
presenter.unsubscribe()
}

override fun onPause() {
super.onPause()
if (isRemoving) supportFragmentManager.removeDataFragment(dataFragment)
override fun removeDataFragment() {
supportFragmentManager?.removeDataFragment(dataFragment)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import io.github.feelfreelinux.wykopmobilny.models.fragments.PagedDataModel
import io.github.feelfreelinux.wykopmobilny.models.fragments.getDataFragmentInstance
import io.github.feelfreelinux.wykopmobilny.models.fragments.removeDataFragment
import io.github.feelfreelinux.wykopmobilny.ui.adapters.LinkAdapter
import io.github.feelfreelinux.wykopmobilny.ui.modules.favorite.FavoriteFragmentNotifier
import io.github.feelfreelinux.wykopmobilny.utils.printout
import javax.inject.Inject

class LinksFavoriteFragment : BaseFeedFragment<Link>(), LinksFavoriteView {
class LinksFavoriteFragment : BaseFeedFragment<Link>(), LinksFavoriteView, FavoriteFragmentNotifier {
@Inject override lateinit var feedAdapter : LinkAdapter
@Inject lateinit var presenter : LinksFavoritePresenter
lateinit var dataFragment : DataFragment<PagedDataModel<List<Link>>>
Expand Down Expand Up @@ -49,8 +51,7 @@ class LinksFavoriteFragment : BaseFeedFragment<Link>(), LinksFavoriteView {
presenter.unsubscribe()
}

override fun onPause() {
super.onPause()
if (isRemoving) supportFragmentManager.removeDataFragment(dataFragment)
override fun removeDataFragment() {
supportFragmentManager?.removeDataFragment(dataFragment)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class LinkDetailsActivity : BaseActivity(), LinkDetailsView, SwipeRefreshLayout.
@Inject lateinit var suggestionsApi : SuggestApi
@Inject lateinit var clipboardHelper : ClipboardHelperApi
@Inject lateinit var presenter: LinkDetailsPresenter
private lateinit var linkFragmentData: DataFragment<Link>
private lateinit var linkFragmentData: DataFragment<Pair<Link, String>> // link, sortby
@Inject lateinit var adapter : LinkDetailsAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -111,7 +111,8 @@ class LinkDetailsActivity : BaseActivity(), LinkDetailsView, SwipeRefreshLayout.
swiperefresh.setOnRefreshListener(this)
linkFragmentData = supportFragmentManager.getDataFragmentInstance(EXTRA_FRAGMENT_KEY + linkId)
if (linkFragmentData.data != null) {
adapter.link = linkFragmentData.data
adapter.link = linkFragmentData.data?.first
presenter.sortBy = linkFragmentData.data?.second ?: "best"
adapter.notifyDataSetChanged()
loadingView.isVisible = false
} else {
Expand All @@ -122,37 +123,41 @@ class LinkDetailsActivity : BaseActivity(), LinkDetailsView, SwipeRefreshLayout.
presenter.updateLink()
}

when(presenter.sortBy) {
"new" -> supportActionBar?.setSubtitle(R.string.sortby_newest)
"old" -> supportActionBar?.setSubtitle(R.string.sortby_oldest)
"best" -> supportActionBar?.setSubtitle(R.string.sortby_best)
}
setSubtitle()
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater?.inflate(R.menu.link_details_menu, menu)
return true
}

fun setSubtitle() {
supportActionBar?.setSubtitle(when(presenter.sortBy) {
"new" -> R.string.sortby_newest
"old" -> R.string.sortby_oldest
else -> R.string.sortby_best
})
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.refresh -> onRefresh()
R.id.sortbyBest -> {
presenter.sortBy = "best"
supportActionBar?.setSubtitle(R.string.sortby_best)
setSubtitle()
presenter.loadComments()
swiperefresh.isRefreshing = true
}
R.id.sortbyNewest -> {
presenter.sortBy = "new"
supportActionBar?.setSubtitle(R.string.sortby_newest)
setSubtitle()
presenter.loadComments()
swiperefresh.isRefreshing = true
}

R.id.sortbyOldest -> {
presenter.sortBy = "old"
supportActionBar?.setSubtitle(R.string.sortby_oldest)
setSubtitle()
presenter.loadComments()
swiperefresh.isRefreshing = true
}
Expand All @@ -169,7 +174,7 @@ class LinkDetailsActivity : BaseActivity(), LinkDetailsView, SwipeRefreshLayout.
override fun onSaveInstanceState(outState: Bundle) {
outState.putString(EXTRA_SORTBY, presenter.sortBy)
super.onSaveInstanceState(outState)
linkFragmentData.data = adapter.link
adapter.link?.let { linkFragmentData.data = Pair(adapter.link!!, presenter.sortBy) }
}

override fun onPause() {
Expand Down
Loading

0 comments on commit de73b6c

Please sign in to comment.