Skip to content

Commit c6f759c

Browse files
committed
添加搜索仓库和搜索用户排序功能
1 parent 6c99687 commit c6f759c

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

app/src/main/java/com/fmt/github/ext/BooleanExt.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ package com.fmt.github.ext
44

55
sealed class BooleanExt<out T>//巧用协变与密封类(增强版枚举)
66

7-
class Success<T>(val data: T) : BooleanExt<T>()
7+
class Success<T>(val data: T) : BooleanExt<T>()//data是协变点(get方法)
88

99
object OtherWise : BooleanExt<Nothing>()//Nothing是任何类的子类
1010

1111
inline fun <T> Boolean.yes(block: () -> T): BooleanExt<T> =//inline提升性能
12-
when {
13-
this -> {
14-
Success(block())
15-
}
16-
else -> {
17-
OtherWise
18-
}
12+
when {//类似于if-else if - else链,不提供参数,所有的分支条件都是简单的布尔表达式,而当一个分支的条件为真时则执行该分支
13+
this -> Success(block())
14+
else -> OtherWise
1915
}
2016

17+
fun <T> Boolean.no(block: () -> T): BooleanExt<T> = when {
18+
this -> OtherWise
19+
else -> Success(block())
20+
}
21+
2122
inline fun <T> BooleanExt<T>.otherwise(block: () -> T): T =
2223
when (this) {
2324
is Success -> this.data
2425
OtherWise -> block()
2526
}
27+

app/src/main/java/com/fmt/github/home/activity/CommonSearchActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ class CommonSearchActivity : BaseDataBindActivity<ActivityCommonSearchBinding>()
9797

9898
private fun searchReposOrUsers() {
9999
mSearchReposModel.searchKey.get()?.apply {
100-
this.isEmpty().yes {
101-
warningToast(R.string.please_enter_search_keywords)
102-
}.otherwise {
100+
this.isEmpty().no {
103101
mSortTv.visibility = View.VISIBLE
104102
mIsSearchRepos.yes {
105103
mReposFragment.searchReposByKey(this, mSort, mOrder)
106104
}.otherwise {
107105
mUsersFragment.searchUsersByKey(this, mSort, mOrder)
108106
}
107+
}.otherwise {
108+
warningToast(R.string.please_enter_search_keywords)
109109
}
110110
}
111111
}

app/src/main/java/com/fmt/github/home/activity/HomeActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class HomeActivity : BaseVMActivity(), NavigationView.OnNavigationItemSelectedLi
131131
}
132132

133133
override fun onOptionsItemSelected(item: MenuItem): Boolean {
134-
when (item.itemId) {
134+
when (item.itemId) {//增强版switch
135135
R.id.item_search_repos -> go2SearchActivity(true)
136136
R.id.item_search_users -> go2SearchActivity(false)
137137
}

0 commit comments

Comments
 (0)