Skip to content

Commit eba3a25

Browse files
committed
FavoriteFragment: 修复bug #3
1 parent d34c93b commit eba3a25

File tree

2 files changed

+74
-17
lines changed

2 files changed

+74
-17
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2016 Alex Zhang aka. ztc1997
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.rayfantasy.icode.extra
18+
19+
import java.util.*
20+
21+
class UpdateAbleList<E> : LinkedList<E> {
22+
constructor() : super()
23+
24+
constructor(collection: MutableCollection<out E>?) : super(collection)
25+
26+
override fun add(element: E): Boolean {
27+
removeDuplicateElement(element)
28+
return super.add(element)
29+
}
30+
31+
override fun add(index: Int, element: E) {
32+
removeDuplicateElement(element)
33+
super.add(index, element)
34+
}
35+
36+
override fun addAll(elements: Collection<E>): Boolean {
37+
elements.forEach { removeDuplicateElement(it) }
38+
return super.addAll(elements)
39+
}
40+
41+
override fun addAll(index: Int, elements: Collection<E>): Boolean {
42+
elements.forEach { removeDuplicateElement(it) }
43+
return super.addAll(index, elements)
44+
}
45+
46+
private fun removeDuplicateElement(element: E) {
47+
forEach {
48+
if (it != null && it.equals(element))
49+
remove(it)
50+
}
51+
}
52+
}

app/src/main/kotlin/com/rayfantasy/icode/ui/fragment/FavoriteFragment.kt

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
88
import com.android.volley.Request
9-
import com.raizlabs.android.dbflow.sql.language.SQLite
9+
import com.raizlabs.android.dbflow.sql.language.Join
10+
import com.raizlabs.android.dbflow.sql.language.NameAlias
11+
import com.raizlabs.android.dbflow.sql.language.Select
1012
import com.rayfantasy.icode.R
11-
import com.rayfantasy.icode.databinding.FragmentMainBinding
12-
import com.rayfantasy.icode.model.ICodeTheme
13+
import com.rayfantasy.icode.extra.UpdateAbleList
1314
import com.rayfantasy.icode.postutil.PostUtil
1415
import com.rayfantasy.icode.postutil.bean.CodeGood
1516
import com.rayfantasy.icode.postutil.bean.CodeGood_Table
1617
import com.rayfantasy.icode.postutil.bean.Favorite
1718
import com.rayfantasy.icode.postutil.bean.Favorite_Table
1819
import com.rayfantasy.icode.ui.adapter.CodeListAdapter
1920
import com.rayfantasy.icode.ui.adapter.LoadMoreAdapter
20-
import kotlinx.android.synthetic.main.fragment_favorite.*
2121
import kotlinx.android.synthetic.main.fragment_favorite.view.*
22-
import org.apache.commons.collections4.list.SetUniqueList
2322
import org.jetbrains.anko.support.v4.onRefresh
2423

2524
class FavoriteFragment : FragmentBase() {
2625
companion object {
2726
const val LOAD_ONCE = 10
2827
}
29-
private lateinit var adapter: CodeListAdapter
28+
29+
private val adapter by lazy { CodeListAdapter(activity, UpdateAbleList(getCacheData())) { loadCodeGoods(false) } }
3030
private val isRefreshing: Boolean
3131
get() = request != null
3232
private var request: Request<*>? = null
@@ -39,45 +39,45 @@ class FavoriteFragment : FragmentBase() {
3939
}
4040

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

4545
private fun initRecyclerView() {
4646
val layoutManager = LinearLayoutManager(activity)
4747
view.favorite_recycler_view.layoutManager = layoutManager
48-
adapter = CodeListAdapter(activity, SetUniqueList.setUniqueList(getCacheData())) { loadCodeGoods(false) }
4948
view.favorite_recycler_view.adapter = adapter
5049
}
50+
5151
private fun loadCodeGoods(refresh: Boolean) {
5252
//如果正在刷新,则不再发起新的刷新请求
5353
if (isRefreshing)
5454
return
5555

5656
//生成加载条件,目前加载3个,方便测试
57-
if(PostUtil.user == null){
57+
if (PostUtil.user == null) {
5858
adapter.footerState = LoadMoreAdapter.FOOTER_STATE_FAILED
5959
return
6060
}
61-
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"
61+
val condition = "JOIN (SELECT * FROM favorite WHERE userId = ${PostUtil.user!!.id}) b on a.id = b.goodId ORDER BY b.createat DESC"
6262
request = PostUtil.selectCodeGood(condition) {
6363
onSuccess {
6464
if (isDetached) return@onSuccess
6565
view.favo_swipe.isRefreshing = false
6666
request = null
6767

68-
if (it.isEmpty() ) {
68+
//if (it.isEmpty() ) {
6969
//如果结果为空,则表示没有更多内容了
7070
adapter.footerState = LoadMoreAdapter.FOOTER_STATE_NO_MORE
71-
} else {
71+
//} else {
7272
if (refresh) {
7373
adapter.codeGoods.clear()
7474
}
7575
//否则将结果加入codeGoods,并刷新adapter
7676
adapter.codeGoods.addAll(it)
7777
if (refresh) adapter.notifyDataSetChanged()
78-
else adapter.notifyItemRangeInserted(adapter.itemCount - 1 - it.size, it.size)
78+
else adapter.notifyDataSetChanged()
7979
cacheData(adapter.codeGoods)
80-
}
80+
//}
8181
onFailed { t, rc ->
8282
request = null
8383
if (isDetached || view == null) return@onFailed
@@ -93,15 +93,20 @@ class FavoriteFragment : FragmentBase() {
9393
PostUtil.cancel(request)
9494
request = null
9595
}
96+
9697
fun cacheData(data: List<CodeGood>) {
9798
data.forEach {
9899
it.loadContentFromCache()
99100
it.save()
100101
}
101102
}
102-
fun getCacheData() = SQLite.select(CodeGood_Table.id)
103-
.from(CodeGood::class.java).leftOuterJoin(Favorite::class.java)
104-
.on(CodeGood_Table.id.withTable().eq(Favorite_Table.goodId.withTable()))
103+
104+
fun getCacheData() = Select()
105+
.from(CodeGood::class.java).`as`("a")
106+
.join(Favorite::class.java, Join.JoinType.CROSS).`as`("b")
107+
.on(CodeGood_Table.id.withTable(NameAlias("a")).eq(Favorite_Table.goodId.withTable(NameAlias("b"))))
108+
.where(CodeGood_Table.id.withTable(NameAlias("a")).`is`(PostUtil.user!!.id))
109+
.orderBy(Favorite_Table.createat, false)
105110
.queryList()
106111

107112
}

0 commit comments

Comments
 (0)