Skip to content
Snippets Groups Projects
Commit c3f8ef7c authored by John's avatar John
Browse files

Load more branches

parent bdac2720
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -96,7 +96,7 @@ ext {
retrofitVersion = '2.3.0'
okHttpVersion = '3.9.1'
butterknifeVersion = '8.8.1'
parcelerVersion = '1.1.9'
parcelerVersion = '1.1.10'
reptarVersion = '2.5.1'
adapterLayout = '1.1.2'
materialDialogsVersion = '0.9.6.0'
Loading
Loading
@@ -143,7 +143,7 @@ dependencies {
 
implementation 'org.greenrobot:eventbus:3.1.1'
 
implementation 'io.reactivex.rxjava2:rxjava:2.1.7'
implementation 'io.reactivex.rxjava2:rxjava:2.1.8'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
 
implementation "com.uber.autodispose:autodispose-kotlin:$autodisposeVersion"
Loading
Loading
@@ -169,7 +169,7 @@ dependencies {
implementation 'com.github.Commit451:Gimbal:2.0.2'
implementation 'com.github.Commit451:Teleprinter:2.0.0'
implementation 'com.github.Commit451:Jounce:1.0.2'
implementation 'com.github.Commit451:ForegroundViews:2.4.0'
implementation 'com.github.Commit451:ForegroundViews:2.4.4'
implementation 'com.github.Commit451:MorphTransitions:2.0.0'
implementation "com.github.Commit451:Alakazam:2.0.0"
implementation 'com.github.Commit451:Lift:2.0.1'
Loading
Loading
@@ -195,7 +195,7 @@ dependencies {
 
implementation 'com.github.alorma:diff-textview:1.3.0'
 
implementation 'com.wdullaer:materialdatetimepicker:3.4.0'
implementation 'com.wdullaer:materialdatetimepicker:3.5.0'
 
implementation 'com.github.novoda:simple-chrome-custom-tabs:0.1.5'
 
Loading
Loading
Loading
Loading
@@ -48,6 +48,14 @@ class BranchAdapter(private val ref: Ref?, private val listener: BranchAdapter.L
notifyDataSetChanged()
}
 
fun addEntries(entries: Collection<Branch>) {
if (!entries.isEmpty()) {
val start = values.size
this.values.addAll(entries)
notifyItemRangeChanged(start, this.values.size)
}
}
private fun getEntry(position: Int): Branch {
return values[position]
}
Loading
Loading
Loading
Loading
@@ -277,7 +277,10 @@ interface GitLabService {
/* --- REPOSITORY --- */
 
@GET(API_VERSION + "/projects/{id}/repository/branches?order_by=last_activity_at")
fun getBranches(@Path("id") projectId: Long): Single<List<Branch>>
fun getBranches(@Path("id") projectId: Long): Single<Response<List<Branch>>>
@GET
fun getBranches(@Url url: String): Single<Response<List<Branch>>>
 
@GET(API_VERSION + "/projects/{id}/repository/contributors")
fun getContributors(@Path("id") projectId: String): Single<List<Contributor>>
Loading
Loading
Loading
Loading
@@ -2,6 +2,7 @@ package com.commit451.gitlab.fragment
 
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
Loading
Loading
@@ -20,7 +21,9 @@ import com.commit451.gitlab.adapter.BranchAdapter
import com.commit451.gitlab.extension.with
import com.commit451.gitlab.model.Ref
import com.commit451.gitlab.model.api.Branch
import com.commit451.gitlab.rx.CustomSingleObserver
import com.commit451.gitlab.rx.CustomResponseSingleObserver
import com.commit451.gitlab.util.LinkHeaderParser
import com.commit451.gitlab.util.OnScrollLoadMoreListener
import timber.log.Timber
 
/**
Loading
Loading
@@ -30,8 +33,8 @@ class PickBranchFragment : ButterKnifeFragment() {
 
companion object {
 
private val EXTRA_PROJECT_ID = "project_id"
private val EXTRA_REF = "ref"
private const val EXTRA_PROJECT_ID = "project_id"
private const val EXTRA_REF = "ref"
 
fun newInstance(projectId: Long, ref: Ref?): PickBranchFragment {
val fragment = PickBranchFragment()
Loading
Loading
@@ -43,14 +46,20 @@ class PickBranchFragment : ButterKnifeFragment() {
}
}
 
@BindView(R.id.list) lateinit var listProjects: RecyclerView
@BindView(R.id.message_text) lateinit var textMessage: TextView
@BindView(R.id.progress) lateinit var progress: View
@BindView(R.id.list)
lateinit var listProjects: RecyclerView
@BindView(R.id.message_text)
lateinit var textMessage: TextView
@BindView(R.id.progress)
lateinit var progress: View
 
lateinit var adapterBranches: BranchAdapter
 
var projectId: Long = 0
 
var nextPageUrl: Uri? = null
var loading: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
projectId = arguments?.getLong(EXTRA_PROJECT_ID)!!
Loading
Loading
@@ -73,22 +82,26 @@ class PickBranchFragment : ButterKnifeFragment() {
activity?.finish()
}
})
listProjects.layoutManager = LinearLayoutManager(activity)
val layoutManager = LinearLayoutManager(activity)
listProjects.layoutManager = layoutManager
listProjects.adapter = adapterBranches
listProjects.addOnScrollListener(OnScrollLoadMoreListener(layoutManager, {
!loading && nextPageUrl != null
}, {
loadMore()
}))
 
loadData()
}
 
override fun loadData() {
if (view == null) {
return
}
loading = true
progress.visibility = View.VISIBLE
textMessage.visibility = View.GONE
 
App.get().gitLab.getBranches(projectId)
.with(this)
.subscribe(object : CustomSingleObserver<List<Branch>>() {
.subscribe(object : CustomResponseSingleObserver<List<Branch>>() {
 
override fun error(e: Throwable) {
Timber.e(e)
Loading
Loading
@@ -96,10 +109,31 @@ class PickBranchFragment : ButterKnifeFragment() {
textMessage.visibility = View.VISIBLE
}
 
override fun success(branches: List<Branch>) {
override fun responseNonNullSuccess(branches: List<Branch>) {
loading = false
nextPageUrl = LinkHeaderParser.parse(response()).next
progress.visibility = View.GONE
adapterBranches.setEntries(branches)
}
})
}
fun loadMore() {
loading = true
App.get().gitLab.getBranches(nextPageUrl.toString())
.with(this)
.subscribe(object : CustomResponseSingleObserver<List<Branch>>() {
override fun error(e: Throwable) {
Timber.e(e)
loading = false
}
override fun responseNonNullSuccess(branches: List<Branch>) {
loading = false
nextPageUrl = LinkHeaderParser.parse(response()).next
adapterBranches.addEntries(branches)
}
})
}
}
Loading
Loading
@@ -11,10 +11,10 @@ import timber.log.Timber
*/
object LinkHeaderParser {
 
private val PREV_PAGE_SUFFIX = "rel=\"prev\""
private val NEXT_PAGE_SUFFIX = "rel=\"next\""
private val FIRST_PAGE_SUFFIX = "rel=\"first\""
private val LAST_PAGE_SUFFIX = "rel=\"last\""
private const val PREV_PAGE_SUFFIX = "rel=\"prev\""
private const val NEXT_PAGE_SUFFIX = "rel=\"next\""
private const val FIRST_PAGE_SUFFIX = "rel=\"first\""
private const val LAST_PAGE_SUFFIX = "rel=\"last\""
 
fun parse(response: Response<*>): PaginationData {
var prev: Uri? = null
Loading
Loading
package com.commit451.gitlab.util
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
class OnScrollLoadMoreListener(private val layoutManager: LinearLayoutManager, private val shouldLoadMore: () -> Boolean, private val loadMore: () -> Unit) : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val visibleItemCount = layoutManager.childCount
val totalItemCount = layoutManager.itemCount
val firstVisibleItem = layoutManager.findFirstVisibleItemPosition()
if (firstVisibleItem + visibleItemCount >= totalItemCount && shouldLoadMore.invoke()) {
loadMore.invoke()
}
}
}
\ No newline at end of file
apply plugin: 'com.github.ben-manes.versions'
apply plugin: 'com.commit451.updatewrapper'
buildscript {
ext.kotlinVersion = '1.2.10'
ext.kotlinVersion = '1.2.20'
repositories {
jcenter()
maven { url "https://jitpack.io" }
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment