Skip to content

Commit

Permalink
FavoriteFragment: 修复bug #3
Browse files Browse the repository at this point in the history
  • Loading branch information
ztc1997 committed Mar 13, 2016
1 parent d34c93b commit eba3a25
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 17 deletions.
52 changes: 52 additions & 0 deletions app/src/main/kotlin/com/rayfantasy/icode/extra/UpdateAbleList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2016 Alex Zhang aka. ztc1997
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.rayfantasy.icode.extra

import java.util.*

class UpdateAbleList<E> : LinkedList<E> {
constructor() : super()

constructor(collection: MutableCollection<out E>?) : super(collection)

override fun add(element: E): Boolean {
removeDuplicateElement(element)
return super.add(element)
}

override fun add(index: Int, element: E) {
removeDuplicateElement(element)
super.add(index, element)
}

override fun addAll(elements: Collection<E>): Boolean {
elements.forEach { removeDuplicateElement(it) }
return super.addAll(elements)
}

override fun addAll(index: Int, elements: Collection<E>): Boolean {
elements.forEach { removeDuplicateElement(it) }
return super.addAll(index, elements)
}

private fun removeDuplicateElement(element: E) {
forEach {
if (it != null && it.equals(element))
remove(it)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.android.volley.Request
import com.raizlabs.android.dbflow.sql.language.SQLite
import com.raizlabs.android.dbflow.sql.language.Join
import com.raizlabs.android.dbflow.sql.language.NameAlias
import com.raizlabs.android.dbflow.sql.language.Select
import com.rayfantasy.icode.R
import com.rayfantasy.icode.databinding.FragmentMainBinding
import com.rayfantasy.icode.model.ICodeTheme
import com.rayfantasy.icode.extra.UpdateAbleList
import com.rayfantasy.icode.postutil.PostUtil
import com.rayfantasy.icode.postutil.bean.CodeGood
import com.rayfantasy.icode.postutil.bean.CodeGood_Table
import com.rayfantasy.icode.postutil.bean.Favorite
import com.rayfantasy.icode.postutil.bean.Favorite_Table
import com.rayfantasy.icode.ui.adapter.CodeListAdapter
import com.rayfantasy.icode.ui.adapter.LoadMoreAdapter
import kotlinx.android.synthetic.main.fragment_favorite.*
import kotlinx.android.synthetic.main.fragment_favorite.view.*
import org.apache.commons.collections4.list.SetUniqueList
import org.jetbrains.anko.support.v4.onRefresh

class FavoriteFragment : FragmentBase() {
companion object {
const val LOAD_ONCE = 10
}
private lateinit var adapter: CodeListAdapter

private val adapter by lazy { CodeListAdapter(activity, UpdateAbleList(getCacheData())) { loadCodeGoods(false) } }
private val isRefreshing: Boolean
get() = request != null
private var request: Request<*>? = null
Expand All @@ -39,45 +39,45 @@ class FavoriteFragment : FragmentBase() {
}

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater?.inflate(R.layout.fragment_favorite,container,false)
return inflater?.inflate(R.layout.fragment_favorite, container, false)
}

private fun initRecyclerView() {
val layoutManager = LinearLayoutManager(activity)
view.favorite_recycler_view.layoutManager = layoutManager
adapter = CodeListAdapter(activity, SetUniqueList.setUniqueList(getCacheData())) { loadCodeGoods(false) }
view.favorite_recycler_view.adapter = adapter
}

private fun loadCodeGoods(refresh: Boolean) {
//如果正在刷新,则不再发起新的刷新请求
if (isRefreshing)
return

//生成加载条件,目前加载3个,方便测试
if(PostUtil.user == null){
if (PostUtil.user == null) {
adapter.footerState = LoadMoreAdapter.FOOTER_STATE_FAILED
return
}
val condition = "SELECT a.* FROM icode.code_good a JOIN (SELECT * FROM icode.favorite WHERE userId = ${PostUtil.user!!.id}) b on a.id = b.goodId ORDER BY b.createat"
val condition = "JOIN (SELECT * FROM favorite WHERE userId = ${PostUtil.user!!.id}) b on a.id = b.goodId ORDER BY b.createat DESC"
request = PostUtil.selectCodeGood(condition) {
onSuccess {
if (isDetached) return@onSuccess
view.favo_swipe.isRefreshing = false
request = null

if (it.isEmpty() ) {
//if (it.isEmpty() ) {
//如果结果为空,则表示没有更多内容了
adapter.footerState = LoadMoreAdapter.FOOTER_STATE_NO_MORE
} else {
//} else {
if (refresh) {
adapter.codeGoods.clear()
}
//否则将结果加入codeGoods,并刷新adapter
adapter.codeGoods.addAll(it)
if (refresh) adapter.notifyDataSetChanged()
else adapter.notifyItemRangeInserted(adapter.itemCount - 1 - it.size, it.size)
else adapter.notifyDataSetChanged()
cacheData(adapter.codeGoods)
}
//}
onFailed { t, rc ->
request = null
if (isDetached || view == null) return@onFailed
Expand All @@ -93,15 +93,20 @@ class FavoriteFragment : FragmentBase() {
PostUtil.cancel(request)
request = null
}

fun cacheData(data: List<CodeGood>) {
data.forEach {
it.loadContentFromCache()
it.save()
}
}
fun getCacheData() = SQLite.select(CodeGood_Table.id)
.from(CodeGood::class.java).leftOuterJoin(Favorite::class.java)
.on(CodeGood_Table.id.withTable().eq(Favorite_Table.goodId.withTable()))

fun getCacheData() = Select()
.from(CodeGood::class.java).`as`("a")
.join(Favorite::class.java, Join.JoinType.CROSS).`as`("b")
.on(CodeGood_Table.id.withTable(NameAlias("a")).eq(Favorite_Table.goodId.withTable(NameAlias("b"))))
.where(CodeGood_Table.id.withTable(NameAlias("a")).`is`(PostUtil.user!!.id))
.orderBy(Favorite_Table.createat, false)
.queryList()

}

0 comments on commit eba3a25

Please sign in to comment.