Skip to content

Commit

Permalink
1. optimise login captcha display
Browse files Browse the repository at this point in the history
2. refactor topic detail view code by divide all list to 2 lists.
3. remove diff for topic detail page
  • Loading branch information
fan123199 committed Jul 30, 2018
1 parent 38324f8 commit a7b5d10
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 157 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "im.fdx.v2ex"
minSdkVersion 21
targetSdkVersion 28
versionCode 24
versionName "2.3.0"
versionCode 25
versionName "2.3.2"
}
lintOptions {
checkReleaseBuilds false
Expand All @@ -23,7 +23,7 @@ android {

googlePlay {
Properties prop = getSignConfig()
if (prop != null&&false) {
if (prop != null) {
keyAlias prop['keyAlias']
keyPassword prop['keyPassword']
storeFile file(prop['storeFile'])
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
</activity>
<activity
android:name=".ui.LoginActivity"
android:exported="true"
android:label="@string/title_activity_login"
android:windowSoftInputMode="adjustResize" />
<activity
Expand Down
20 changes: 9 additions & 11 deletions app/src/main/java/im/fdx/v2ex/MyApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import android.content.SharedPreferences
import android.preference.PreferenceManager
import android.support.v7.app.AppCompatDelegate.*
import androidx.core.content.edit
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import im.fdx.v2ex.utils.Keys
Expand All @@ -25,34 +26,31 @@ class MyApp : Application() {
}
}

lateinit var mPrefs: SharedPreferences
internal lateinit var mPrefs: SharedPreferences
internal var isLogin = false

internal var curTextSize = 0


private fun isNightModeOn(): Boolean {
return mPrefs.getBoolean("NIGHT_MODE", false)
}


fun setLogin(login: Boolean) {
isLogin = login
if (login) {
mPrefs.edit().putBoolean(Keys.PREF_KEY_IS_LOGIN, true).apply()
mPrefs.edit {
putBoolean(Keys.PREF_KEY_IS_LOGIN, true)
}
} else {
mPrefs.edit().remove(Keys.PREF_KEY_IS_LOGIN).apply()
mPrefs.edit {
remove(Keys.PREF_KEY_IS_LOGIN)
}
}
}

fun isLogin(): Boolean = isLogin

override fun onCreate() {
super.onCreate()
INSTANCE = this
mPrefs = PreferenceManager.getDefaultSharedPreferences(this)
PreferenceManager.setDefaultValues(this, R.xml.preference, false)
setDefaultNightMode(if (isNightModeOn()) MODE_NIGHT_YES else MODE_NIGHT_NO)
setDefaultNightMode(if (mPrefs.getBoolean("NIGHT_MODE", false)) MODE_NIGHT_YES else MODE_NIGHT_NO)
XLog.init(when {
BuildConfig.DEBUG -> LogLevel.ALL
else -> LogLevel.NONE
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/java/im/fdx/v2ex/model/BaseModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ import android.os.Parcelable
/**
* Created by fdx on 2017/3/18.
*/
abstract class BaseModel : Parcelable {

}
@Deprecated("没有用处")
abstract class BaseModel : Parcelable
8 changes: 4 additions & 4 deletions app/src/main/java/im/fdx/v2ex/network/Parser.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package im.fdx.v2ex.network

import im.fdx.v2ex.model.NotificationModel
import im.fdx.v2ex.ui.details.ReplyModel
import im.fdx.v2ex.ui.details.Reply
import im.fdx.v2ex.ui.main.Comment
import im.fdx.v2ex.ui.main.Topic
import im.fdx.v2ex.ui.member.Member
Expand Down Expand Up @@ -256,17 +256,17 @@ class Parser(private val htmlStr: String) {
return nodeModels
}

fun getReplies(): ArrayList<ReplyModel> {
fun getReplies(): ArrayList<Reply> {

val replyModels = ArrayList<ReplyModel>()
val replyModels = ArrayList<Reply>()

val items = doc.getElementsByAttributeValueStarting("id", "r_")
for (item in items) {

// <div id="r_4157549" class="cell">
val id = item.id().substring(2)

val replyModel = ReplyModel()
val replyModel = Reply()
val memberModel = Member()
val avatar = item.getElementsByClass("avatar").attr("src")
val username = item.getElementsByTag("strong").first().getElementsByAttributeValueStarting("href", "/member/").first().text()
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/im/fdx/v2ex/ui/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import im.fdx.v2ex.network.NetManager.SIGN_IN_URL
import im.fdx.v2ex.pref
import im.fdx.v2ex.utils.Keys
import im.fdx.v2ex.utils.extensions.logd
import im.fdx.v2ex.utils.extensions.setStatusBarColor
import im.fdx.v2ex.utils.extensions.setUpToolbar
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.item_verify_code.*
Expand Down Expand Up @@ -51,6 +52,7 @@ class LoginActivity : BaseActivity() {
setContentView(R.layout.activity_login)

setUpToolbar()
setStatusBarColor(R.color.primary)
progressBar = findViewById(R.id.pb_login)

val usernamePref = pref.getString("username", "")
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/im/fdx/v2ex/ui/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SettingsActivity : AppCompatActivity() {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity)
jobSchedule = activity?.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
when {
MyApp.get().isLogin() -> {
MyApp.get().isLogin -> {

addPreferencesFromResource(R.xml.preference_login)
findPreference("group_user").title = sharedPreferences.getString("username", getString(R.string.user))
Expand All @@ -70,7 +70,7 @@ class SettingsActivity : AppCompatActivity() {
activity!!.finish()
activity!!.toast("已退出登录")
}
.setNegativeButton(R.string.cancel) { dialogInterface, i ->
.setNegativeButton(R.string.cancel) { _, _ ->

}
.show()
Expand Down
38 changes: 11 additions & 27 deletions app/src/main/java/im/fdx/v2ex/ui/details/DetailsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import android.widget.TextView
import com.elvishew.xlog.XLog
import im.fdx.v2ex.MyApp
import im.fdx.v2ex.R
import im.fdx.v2ex.model.BaseModel
import im.fdx.v2ex.network.*
import im.fdx.v2ex.network.NetManager.dealError
import im.fdx.v2ex.ui.BaseActivity
Expand All @@ -36,14 +35,11 @@ import kotlinx.android.synthetic.main.footer_reply.*
import okhttp3.*
import org.jetbrains.anko.share
import org.jetbrains.anko.toast
import org.jsoup.nodes.Element
import java.io.IOException
import java.util.regex.Pattern

class DetailsActivity : BaseActivity() {

private lateinit var mAdapter: DetailsAdapter
// private val mAllContent = mutableListOf<BaseModel>()
private var mMenu: Menu? = null

private lateinit var tvToolbar: TextView
Expand Down Expand Up @@ -80,7 +76,7 @@ class DetailsActivity : BaseActivity() {
} else if (intent.action == "im.fdx.v2ex.reply") {
logd("MSG_GET LocalBroadCast")
token = intent.getStringExtra("token")
val rm = intent.getParcelableArrayListExtra<ReplyModel>("replies")
val rm = intent.getParcelableArrayListExtra<Reply>("replies")
mAdapter.addItems(rm)
if (intent.getBooleanExtra("bottom", false)) {
detail_recycler_view.scrollToPosition(mAdapter.itemCount - 1)
Expand Down Expand Up @@ -112,7 +108,7 @@ class DetailsActivity : BaseActivity() {
filter.addAction(Keys.ACTION_LOGOUT)
filter.addAction("im.fdx.v2ex.reply")
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, filter)
setFootView(MyApp.get().isLogin())
setFootView(MyApp.get().isLogin)

setUpToolbar()
tvToolbar = findViewById(R.id.tv_toolbar)
Expand All @@ -121,7 +117,6 @@ class DetailsActivity : BaseActivity() {
//// 这个Scroll 到顶部的bug,是focus的原因,focus会让系统自动滚动
val mLayoutManager = LinearLayoutManager(this)
detail_recycler_view.layoutManager = mLayoutManager
detail_recycler_view.smoothScrollToPosition(POSITION_START)
detail_recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {

private var currentPosition = 0
Expand Down Expand Up @@ -166,7 +161,7 @@ class DetailsActivity : BaseActivity() {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}

override fun afterTextChanged(s: Editable) = if (s.isNullOrEmpty()) {
override fun afterTextChanged(s: Editable) = if (s.isEmpty()) {
iv_send.isClickable = false
iv_send.imageTintList = null
} else {
Expand Down Expand Up @@ -200,7 +195,7 @@ class DetailsActivity : BaseActivity() {
data != null -> data.pathSegments[1]
intent.getParcelableExtra<Parcelable>("model") != null -> {
val topicModel = intent.getParcelableExtra<Topic>("model")
mAdapter.mAllList.add(0, topicModel)
mAdapter.topic[0] = topicModel
mAdapter.notifyDataSetChanged()
topicModel.id
}
Expand Down Expand Up @@ -244,7 +239,7 @@ class DetailsActivity : BaseActivity() {
topicHeader = parser.parseResponseToTopic(topicId)
val repliesFirstPage = parser.getReplies()

if (MyApp.get().isLogin()) {
if (MyApp.get().isLogin) {
token = parser.getVerifyCode()

if (token == null) {
Expand All @@ -271,19 +266,14 @@ class DetailsActivity : BaseActivity() {
once = parser.getOnceNum()
}

val mAllContent = mutableListOf<BaseModel>()
mAllContent.clear()
mAllContent.add(0, topicHeader!!)
mAllContent.addAll(repliesFirstPage)

XLog.tag("DetailsActivity").d("got page 1 , next is more page")

val totalPage = parser.getPageValue()[1] // [2,3]

currentPage = parser.getPageValue()[0]
runOnUiThread {
swipe_details.isRefreshing = false
mAdapter.updateItems(mAllContent)
mAdapter.updateItems(topicHeader!!, repliesFirstPage)
if (totalPage == 1 && scrollToBottom) {
detail_recycler_view.scrollToPosition(mAdapter.itemCount - 1)
}
Expand All @@ -297,20 +287,14 @@ class DetailsActivity : BaseActivity() {
})
}

private fun parseIsFavored(body: Element): Boolean {
val p = Pattern.compile("un(?=favorite/topic/\\d{1,10}\\?t=)")
val matcher = p.matcher(body.outerHtml())
return matcher.find()
}

private fun getMoreRepliesByOrder(totalPage: Int, scrollToBottom: Boolean) {
val intentGetMoreReply = Intent(this@DetailsActivity, MoreReplyService::class.java)
intentGetMoreReply.action = "im.fdx.v2ex.get.other.more"
intentGetMoreReply.putExtra("page", totalPage)
intentGetMoreReply.putExtra("topic_id", mTopicId)
intentGetMoreReply.putExtra("bottom", scrollToBottom)
startService(intentGetMoreReply)
XLog.tag("DetailsActivity").d("yes I startIntentService")
logd("yes I startIntentService")
}


Expand All @@ -328,7 +312,7 @@ class DetailsActivity : BaseActivity() {

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_details, menu)
if (MyApp.get().isLogin()) {
if (MyApp.get().isLogin) {
menu.findItem(R.id.menu_favor).isVisible = true
menu.findItem(R.id.menu_reply)?.isVisible = true
} else {
Expand All @@ -354,11 +338,11 @@ class DetailsActivity : BaseActivity() {
swipe_details.isRefreshing = true
getRepliesPageOne(mTopicId, false)
}
R.id.menu_item_share -> share("来自V2EX的帖子:${(mAdapter.mAllList[0] as Topic).title} \n" +
" ${NetManager.HTTPS_V2EX_BASE}/t/${(mAdapter.mAllList[0] as Topic).id}")
R.id.menu_item_share -> share("来自V2EX的帖子:${(mAdapter.topic[0]).title} \n" +
" ${NetManager.HTTPS_V2EX_BASE}/t/${mAdapter.topic[0].id}")
R.id.menu_item_open_in_browser -> {

val topicId = (mAdapter.mAllList[0] as Topic).id
val topicId = mAdapter.topic[0].id
val url = NetManager.HTTPS_V2EX_BASE + "/t/" + topicId
val uri = Uri.parse(url)
val intent = Intent(Intent.ACTION_VIEW, uri)
Expand Down
Loading

0 comments on commit a7b5d10

Please sign in to comment.