Skip to content

Commit

Permalink
1. back to use proguard and fix gson, 2.fix endless scroll show bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fan123199 committed Oct 18, 2018
1 parent 8f8f045 commit 5a0a61a
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 135 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ android {
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.googlePlay
}
}
Expand All @@ -61,9 +62,9 @@ dependencies {
implementation "androidx.cardview:cardview:1.0.0"
implementation "androidx.recyclerview:recyclerview:1.0.0"
implementation "androidx.legacy:legacy-support-v13:1.0.0"
implementation ("androidx.appcompat:appcompat:1.0.0") {
implementation ("androidx.appcompat:appcompat:1.0.0")/* {
exclude group: 'androidx.browser', module: 'browser'
}
}*/
implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation "androidx.browser:browser:1.0.0" //已经被appcompat包含
implementation "androidx.preference:preference:1.0.0"
Expand Down
2 changes: 2 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*;
}

-keep class * implements im.fdx.v2ex.model.VModel

#Glide
-keep class com.bumptech.glide.**

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/im/fdx/v2ex/model/NotificationModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class NotificationModel(var time: String? = "",
var topic: Topic? = Topic(),
var member: Member? = Member(),
var content: String? = "",
var id: String? = "") : Parcelable {
var id: String? = "") : Parcelable, VModel() {
}
7 changes: 7 additions & 0 deletions app/src/main/java/im/fdx/v2ex/model/VModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package im.fdx.v2ex.model


/**
* used for proguard
*/
abstract class VModel
3 changes: 2 additions & 1 deletion app/src/main/java/im/fdx/v2ex/ui/main/Comment.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package im.fdx.v2ex.ui.main

import android.os.Parcelable
import im.fdx.v2ex.model.VModel
import kotlinx.android.parcel.Parcelize

@Parcelize
data class Comment(var title: String = "",
var created: Long = 0,
var content: String = "") : Parcelable
var content: String = "") : Parcelable, VModel()
3 changes: 2 additions & 1 deletion app/src/main/java/im/fdx/v2ex/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.core.view.GravityCompat
import com.google.android.material.navigation.NavigationView
import de.hdodenhof.circleimageview.CircleImageView
import im.fdx.v2ex.*
import im.fdx.v2ex.network.NetManager
Expand Down Expand Up @@ -50,7 +51,7 @@ import org.jetbrains.anko.toast
import java.io.IOException


class MainActivity : BaseActivity(), com.google.android.material.navigation.NavigationView.OnNavigationItemSelectedListener {
class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener {

private lateinit var mDrawer: androidx.drawerlayout.widget.DrawerLayout
private lateinit var mViewPager: androidx.viewpager.widget.ViewPager
Expand Down
51 changes: 44 additions & 7 deletions app/src/main/java/im/fdx/v2ex/ui/main/SearchResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,75 @@ package im.fdx.v2ex.ui.main
import com.google.gson.annotations.SerializedName

data class SearchResult(
val hits: MutableList<HitsItem>? = null,

@field:SerializedName("hits")
val hits: List<HitsItem>? = null,

@field:SerializedName("took")
val took: Int? = null,

@field:SerializedName("total")
val total: Int? = null,

@field:SerializedName("timed_out")
val timedOut: Boolean? = null
)

data class HitsItem(

@field:SerializedName("highlight")
val highlight: Highlight? = null,

@field:SerializedName("_index")
val index: String? = null,

@field:SerializedName("_type")
val type: String? = null,
@SerializedName("_source")

@field:SerializedName("_source")
val source: Source? = null,
@SerializedName("_id")

@field:SerializedName("_id")
val id: String? = null,

@field:SerializedName("_score")
val score: Double? = null
)


data class Source(

@field:SerializedName("node")
val node: Int? = null,

@field:SerializedName("replies")
val replies: Int? = null,

@field:SerializedName("created")
val created: String? = null,

@field:SerializedName("member")
val member: String? = null,

@field:SerializedName("id")
val id: Int? = null,

@field:SerializedName("title")
val title: String? = null,

@field:SerializedName("content")
val content: String? = null
)


data class Highlight(

@field:SerializedName("reply_list.content")
val replyListContent: List<String?>? = null,

@field:SerializedName("title")
val title: List<String?>? = null,

@field:SerializedName("postscript_list.content")
val postscriptListContent: List<String?>? = null,

@field:SerializedName("content")
val content: List<String?>? = null
)
)
35 changes: 25 additions & 10 deletions app/src/main/java/im/fdx/v2ex/ui/main/TopicsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class TopicsFragment : Fragment() {

mSwipeLayout = layout.findViewById(R.id.swipe_container)
mSwipeLayout.initTheme()
mSwipeLayout.setOnRefreshListener { loadTopic() }
mSwipeLayout.setOnRefreshListener { refresh() }

//找出recyclerview,并赋予变量 //fdx最早的水平
mRecyclerView = layout.findViewById(R.id.rv_container)
Expand All @@ -112,7 +112,7 @@ class TopicsFragment : Fragment() {
}


setUpFabAnimation()
// setUpFabAnimation()


mAdapter = TopicsRVAdapter(activity!!)
Expand All @@ -131,6 +131,7 @@ class TopicsFragment : Fragment() {
return layout
}

//禁用隐藏fab
private fun setUpFabAnimation() {
fab?.let { fab ->
mRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
Expand Down Expand Up @@ -172,14 +173,24 @@ class TopicsFragment : Fragment() {

}

fun loadTopic(currentPage: Int = 1) {
fun loadTopic(currentPage: Int) {

if (currentMode == FROM_SEARCH) {
makeQuery(query, (currentPage - 1) * NUMBER_PER_PAGE)
makeQuery(query, currentPage)
} else {
getTopics(mRequestURL, currentPage)
}
}

fun refresh() {
mScrollListener.pageToLoad = 1
if (currentMode == FROM_SEARCH) {
makeQuery(query)
} else {
getTopics(mRequestURL)
}
}

private fun getTopics(requestURL: String, currentPage: Int = 1) {

vCall(if (currentPage != 1) "$requestURL?p=$currentPage" else requestURL)
Expand Down Expand Up @@ -247,21 +258,24 @@ class TopicsFragment : Fragment() {
/**
* nextIndex, not page index, is the item offset
*/
private fun makeQuery(query: String, nextIndex: Int = 0) {
private fun makeQuery(query: String, currentPage: Int = 1) {


val nextIndex = (currentPage - 1) * NUMBER_PER_PAGE

val url: HttpUrl = HttpUrl.Builder()
.scheme("https")
.host("www.sov2ex.com")
.addEncodedPathSegments("/api/search")
.addEncodedPathSegments("api/search")
.addEncodedQueryParameter("q", query)
.addEncodedQueryParameter("sort", "created")
.addEncodedQueryParameter("from", nextIndex.toString()) // 偏移量
.addEncodedQueryParameter("from", nextIndex.toString()) // 偏移量, 默认0
.addEncodedQueryParameter("size", NUMBER_PER_PAGE.toString()) //数量,默认10
// .addEncodedQueryParameter("node") //节点名称
.build()

showRefresh(true)
OkHttpClient().newCall(Request.Builder()
HttpHelper.OK_CLIENT.newCall(Request.Builder()
.addHeader("accept", "application/json")
.url(url)
.build())
Expand All @@ -272,7 +286,6 @@ class TopicsFragment : Fragment() {
}

override fun onResponse(call: Call?, response: Response?) {
showRefresh(false)
val body = response?.body()!!.string()
loge(body)
val result: SearchResult = Gson().fromJson(body, SearchResult::class.java)
Expand All @@ -294,6 +307,8 @@ class TopicsFragment : Fragment() {
} ?: return

activity?.runOnUiThread {
mSwipeLayout.isRefreshing = false
mScrollListener.loading = false
if (topics.isEmpty()) {
flContainer.showNoContent(true)
mAdapter.clearAndNotify()
Expand All @@ -302,7 +317,7 @@ class TopicsFragment : Fragment() {
if (mScrollListener.pageToLoad == 1) {
topics.let { mAdapter.updateItems(it) }
} else {
mScrollListener.pageAfterLoaded = nextIndex
mScrollListener.pageAfterLoaded = currentPage
topics.let { mAdapter.addAllItems(it) }
}
}
Expand Down
Loading

0 comments on commit 5a0a61a

Please sign in to comment.