Skip to content

Commit 9893185

Browse files
committed
review optimize code
1 parent c26afea commit 9893185

File tree

6 files changed

+91
-48
lines changed

6 files changed

+91
-48
lines changed

app/src/main/kotlin/com/hyphenate/chatdemo/MainActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ class MainActivity : BaseInitActivity<ActivityMainBinding>(), NavigationBarView.
257257
EaseConstant.API_ASYNC_ADD_CONTACT -> {
258258
if (errorCode == ChatError.EM_NO_ERROR){
259259
runOnUiThread{
260-
mContext.showToast(mContext.resources.getString(R.string.em_main_add_contact_success))
260+
showToast(R.string.em_main_add_contact_success)
261261
}
262262
}else{
263263
runOnUiThread{
264-
mContext.showToast(errorMessage.toString())
264+
showToast(errorMessage.toString())
265265
}
266266
}
267267
}

app/src/main/kotlin/com/hyphenate/chatdemo/ui/login/LoginFragment.kt

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import com.hyphenate.easeui.common.ChatError
4848
import com.hyphenate.easeui.common.bus.EaseFlowBus
4949
import com.hyphenate.easeui.common.extensions.catchChatException
5050
import com.hyphenate.easeui.common.extensions.hideSoftKeyboard
51-
import com.hyphenate.easeui.common.helper.EasePreferenceManager
5251
import kotlinx.coroutines.flow.SharingStarted
5352
import kotlinx.coroutines.flow.onCompletion
5453
import kotlinx.coroutines.flow.onStart
@@ -311,49 +310,56 @@ class LoginFragment : EaseBaseFragment<DemoFragmentLoginBinding>(), View.OnClick
311310
}
312311
}
313312

314-
private val spannable: SpannableString
313+
private var spannable: SpannableString? = null
315314
private get() {
316-
val language = PreferenceManager.getValue(DemoConstant.APP_LANGUAGE,Locale.getDefault().language)
317-
val isZh = language.startsWith("zh")
318-
val spanStr = SpannableString(getString(R.string.em_login_agreement))
319-
var start1 = 29
320-
var end1 = 45
321-
var start2 = 50
322-
var end2 = spanStr.length
323-
if (isZh) {
324-
start1 = 5
325-
end1 = 13
326-
start2 = 14
327-
end2 = spanStr.length
315+
if (field == null) {
316+
field = createSpannable()
328317
}
329-
//设置下划线
330-
//spanStr.setSpan(new UnderlineSpan(), 3, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
331-
spanStr.setSpan(object : MyClickableSpan() {
332-
override fun onClick(widget: View) {
333-
jumpToAgreement()
334-
}
335-
}, start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
336-
spanStr.setSpan(
337-
ForegroundColorSpan(ContextCompat.getColor(mContext, com.hyphenate.easeui.R.color.ease_color_primary)),
338-
start1,
339-
end1,
340-
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
341-
)
318+
return field
319+
}
342320

343-
//spanStr.setSpan(new UnderlineSpan(), 10, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
344-
spanStr.setSpan(object : MyClickableSpan() {
345-
override fun onClick(widget: View) {
346-
jumpToProtocol()
347-
}
348-
}, start2, end2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
349-
spanStr.setSpan(
350-
ForegroundColorSpan(ContextCompat.getColor(mContext, com.hyphenate.easeui.R.color.ease_color_primary)),
351-
start2,
352-
end2,
353-
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
354-
)
355-
return spanStr
321+
private fun createSpannable(): SpannableString {
322+
val language = PreferenceManager.getValue(DemoConstant.APP_LANGUAGE,Locale.getDefault().language)
323+
val isZh = language.startsWith("zh")
324+
val spanStr = SpannableString(getString(R.string.em_login_agreement))
325+
var start1 = 29
326+
var end1 = 45
327+
var start2 = 50
328+
var end2 = spanStr.length
329+
if (isZh) {
330+
start1 = 5
331+
end1 = 13
332+
start2 = 14
333+
end2 = spanStr.length
356334
}
335+
//设置下划线
336+
//spanStr.setSpan(new UnderlineSpan(), 3, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
337+
spanStr.setSpan(object : MyClickableSpan() {
338+
override fun onClick(widget: View) {
339+
jumpToAgreement()
340+
}
341+
}, start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
342+
spanStr.setSpan(
343+
ForegroundColorSpan(ContextCompat.getColor(mContext, com.hyphenate.easeui.R.color.ease_color_primary)),
344+
start1,
345+
end1,
346+
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
347+
)
348+
349+
//spanStr.setSpan(new UnderlineSpan(), 10, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
350+
spanStr.setSpan(object : MyClickableSpan() {
351+
override fun onClick(widget: View) {
352+
jumpToProtocol()
353+
}
354+
}, start2, end2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
355+
spanStr.setSpan(
356+
ForegroundColorSpan(ContextCompat.getColor(mContext, com.hyphenate.easeui.R.color.ease_color_primary)),
357+
start2,
358+
end2,
359+
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
360+
)
361+
return spanStr
362+
}
357363

358364
override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent): Boolean {
359365
if (actionId == EditorInfo.IME_ACTION_DONE) {
@@ -422,7 +428,8 @@ class LoginFragment : EaseBaseFragment<DemoFragmentLoginBinding>(), View.OnClick
422428
}
423429

424430
override fun onDestroyView() {
425-
spannable.clearSpans()
431+
spannable?.clearSpans()
432+
spannable = null
426433
super.onDestroyView()
427434
}
428435

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<solid android:color="@color/ease_neutral_10"/>
5+
<corners android:radius="@dimen/ease_corner_extra_small"/>
6+
<stroke android:color="@color/ease_neutral_10"/>
7+
8+
</shape>

app/src/main/res/layout/demo_activity_login.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
android:layout_height="match_parent">
77

88
<ImageView
9+
style="@style/splash_iv_bg_style"
910
android:layout_width="match_parent"
10-
android:layout_height="match_parent"
11-
android:scaleType="centerCrop"
12-
android:src="@drawable/splash_bg_light"/>
11+
android:layout_height="match_parent"/>
1312

1413
<FrameLayout
1514
android:id="@+id/fl_fragment"

app/src/main/res/layout/demo_fragment_login.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5-
android:background="@color/demo_background"
65
xmlns:app="http://schemas.android.com/apk/res-auto"
76
xmlns:tools="http://schemas.android.com/tools">
87

@@ -78,7 +77,7 @@
7877

7978
</RelativeLayout>
8079

81-
<Button
80+
<androidx.appcompat.widget.AppCompatButton
8281
android:id="@+id/btn_login"
8382
app:layout_constraintLeft_toLeftOf="parent"
8483
app:layout_constraintRight_toRightOf="parent"

app/src/main/res/values-night/styles.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,35 @@
2525
<item name="android:textColor">@color/ease_color_text_secondary</item>
2626
</style>
2727

28+
<style name="em_login_et_input">
29+
<item name="android:layout_width">0dp</item>
30+
<item name="android:layout_height">@dimen/ease_size_48</item>
31+
<item name="android:layout_marginLeft">@dimen/ease_size_16</item>
32+
<item name="android:layout_marginRight">@dimen/ease_size_16</item>
33+
<item name="android:maxWidth">400dp</item>
34+
<item name="android:background">@drawable/demo_login_et_bg_night</item>
35+
<item name="android:paddingStart">@dimen/ease_size_16</item>
36+
<item name="android:paddingEnd">@dimen/ease_size_16</item>
37+
<item name="android:textSize">@dimen/em_login_text_size</item>
38+
<item name="android:textAppearance">@style/Ease.TextAppearance.Title.Medium</item>
39+
<item name="android:textColor">@color/ease_color_text_on_primary</item>
40+
<item name="android:textColorHint">@color/ease_color_text_hint</item>
41+
<item name="android:gravity">center_vertical</item>
42+
<item name="android:maxLines">1</item>
43+
<item name="android:inputType">text</item>
44+
</style>
45+
46+
<style name="em_login_btn">
47+
<item name="android:layout_width">0dp</item>
48+
<item name="android:layout_height">wrap_content</item>
49+
<item name="android:layout_marginTop">@dimen/ease_size_24</item>
50+
<item name="android:layout_marginLeft">@dimen/ease_size_30</item>
51+
<item name="android:layout_marginRight">@dimen/ease_size_30</item>
52+
<item name="android:background">@drawable/demo_login_btn_bg</item>
53+
<item name="android:textColor">@color/ease_neutral_98</item>
54+
<item name="android:enabled">false</item>
55+
<item name="android:textAppearance">@style/Ease.TextAppearance.Headline.Small</item>
56+
</style>
57+
2858

2959
</resources>

0 commit comments

Comments
 (0)