Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加图片签到加载动画 & 优化输入框使用体验 #13

Merged
merged 3 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:name=".App"
android:allowBackup="true"
Expand All @@ -39,21 +40,30 @@
android:usesCleartextTraffic="true"
tools:replace="android:icon,android:theme,android:roundIcon"
tools:targetApi="31">
<meta-data android:name="com.amap.api.v2.apikey" android:value="key">
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="key">

</meta-data>
<activity
android:name=".login.LoginActivity"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity android:name=".main.MainActivity" />
<activity android:name=".scan.ScanActivity" />
<activity
android:name=".login.LoginActivity"
android:name=".wrapper.WrapperActivity"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
<!-- <intent-filter>-->
<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- <action android:name="android.intent.action.MAIN" />-->
<!-- </intent-filter>-->
</activity>
<activity android:name=".main.MainActivity"/>
<activity android:name=".scan.ScanActivity"/>
<activity android:name=".wrapper.WrapperActivity"/>
<activity android:name=".photo.PhotoSignActivity"/>
<activity android:name=".photo.PhotoSignActivity" />
</application>

</manifest>
17 changes: 0 additions & 17 deletions app/src/main/java/com/cofbro/qian/homework/HomeworkFragment.kt

This file was deleted.

32 changes: 29 additions & 3 deletions app/src/main/java/com/cofbro/qian/photo/PhotoSignActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cofbro.qian.photo

import android.app.Dialog
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.fastjson.JSONObject
Expand All @@ -12,6 +13,7 @@ import com.cofbro.qian.utils.GlideEngine
import com.cofbro.qian.utils.ImageDownloader
import com.cofbro.qian.utils.getStringExt
import com.cofbro.qian.utils.showSignResult
import com.cofbro.qian.view.FullScreenDialog
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
Expand All @@ -24,6 +26,8 @@ import java.io.File
import java.util.ArrayList

class PhotoSignActivity : BaseActivity<PhotoSignViewModel, ActivityPhotoSignBinding>() {
private var loadingDialog: Dialog? = null
private var imageFile: File? = null
private var aid = ""
private var token = ""
private var objectId = ""
Expand Down Expand Up @@ -66,7 +70,9 @@ class PhotoSignActivity : BaseActivity<PhotoSignViewModel, ActivityPhotoSignBind
lifecycleScope.launch(Dispatchers.IO) {
val data = it.data?.body?.string()
withContext(Dispatchers.Main) {
hideLoadingView()
data?.showSignResult()
imageFile?.delete()
finish()
}
}
Expand All @@ -91,10 +97,17 @@ class PhotoSignActivity : BaseActivity<PhotoSignViewModel, ActivityPhotoSignBind
override fun onResult(result: ArrayList<LocalMedia>?) {
result?.get(0)?.let {
lifecycleScope.launch(Dispatchers.IO) {
ImageDownloader.download(this@PhotoSignActivity, it.path) { success, filepath ->
ImageDownloader.download(
this@PhotoSignActivity,
it.path
) { success, filepath ->
if (success) {
val file = File(filepath)
viewModel.uploadImage(URL.getUploadImagePath(token), file)
showLoadingView()
imageFile = File(filepath)
imageFile?.let {
viewModel.uploadImage(URL.getUploadImagePath(token), it)
}

}
}
}
Expand All @@ -110,4 +123,17 @@ class PhotoSignActivity : BaseActivity<PhotoSignViewModel, ActivityPhotoSignBind
val uid = CacheUtils.cache["uid"] ?: ""
viewModel.sign(URL.getSignWithPhoto(aid, uid, objectId))
}

private fun showLoadingView() {
lifecycleScope.launch(Dispatchers.Main) {
loadingDialog = FullScreenDialog(this@PhotoSignActivity)
loadingDialog?.setCancelable(false)
loadingDialog?.show()
}
}

private fun hideLoadingView() {
loadingDialog?.dismiss()
loadingDialog = null
}
}
153 changes: 153 additions & 0 deletions app/src/main/java/com/cofbro/qian/utils/KeyboardUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.cofbro.qian.utils

import android.R
import android.app.Activity
import android.content.res.Resources
import android.graphics.Rect
import android.os.Build
import android.view.View
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.view.WindowInsets
import android.view.WindowInsetsAnimation
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.annotation.RequiresApi
import androidx.core.view.WindowInsetsCompat
import kotlin.math.abs


object KeyboardUtil {
var imeHeight = 0
var sDecorViewInvisibleHeightPre = 0
private var onGlobalLayoutListener: OnGlobalLayoutListener? = null
private var mNavHeight = 0
private var sDecorViewDelta = 0
private fun getDecorViewInvisibleHeight(activity: Activity): Int {
val decorView = activity.window.decorView
?: return sDecorViewInvisibleHeightPre
val outRect = Rect()
decorView.getWindowVisibleDisplayFrame(outRect)
val delta = abs(decorView.bottom - outRect.bottom)
if (delta <= mNavHeight) {
sDecorViewDelta = delta
return 0
}
return delta - sDecorViewDelta
}

fun registerKeyboardHeightListener(activity: Activity, listener: KeyboardHeightListener) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
invokeAbove31(activity, listener)
} else {
invokeBelow31(activity, listener)
}
}

@RequiresApi(api = Build.VERSION_CODES.R)
private fun invokeAbove31(activity: Activity, listener: KeyboardHeightListener) {
activity.window.decorView.setWindowInsetsAnimationCallback(object :
WindowInsetsAnimation.Callback(DISPATCH_MODE_STOP) {
override fun onProgress(
windowInsets: WindowInsets,
list: List<WindowInsetsAnimation>
): WindowInsets {
val imeHeight = windowInsets.getInsets(WindowInsetsCompat.Type.ime()).bottom
val navHeight =
windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
val hasNavigationBar =
windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars()) &&
windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom > 0
listener.onKeyboardHeightChanged(
if (hasNavigationBar) Math.max(
imeHeight - navHeight,
0
) else imeHeight
)
return windowInsets
}
})
}

private fun invokeBelow31(activity: Activity, listener: KeyboardHeightListener) {
val flags = activity.window.attributes.flags
if (flags and WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS != 0) {
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
}
val contentView = activity.findViewById<FrameLayout>(R.id.content)
sDecorViewInvisibleHeightPre = getDecorViewInvisibleHeight(activity)
onGlobalLayoutListener = OnGlobalLayoutListener {
val height = getDecorViewInvisibleHeight(activity)
if (sDecorViewInvisibleHeightPre != height) {
listener.onKeyboardHeightChanged(height)
sDecorViewInvisibleHeightPre = height
}
}

//获取到导航栏高度之后再添加布局监听
mNavHeight = navBarHeight
// getNavigationBarHeight(activity, object : NavigationBarCallback() {
// fun onHeight(height: Int, hasNav: Boolean) {
// mNavHeight = height
// contentView.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener)
// }
// })
}

fun unregisterKeyboardHeightListener(activity: Activity) {
onGlobalLayoutListener = null
val contentView = activity.window.decorView.findViewById<View>(R.id.content)
?: return
contentView.viewTreeObserver.removeGlobalOnLayoutListener(onGlobalLayoutListener)
}

private val navBarHeight: Int
get() {
val res = Resources.getSystem()
val resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android")
return if (resourceId != 0) {
res.getDimensionPixelSize(resourceId)
} else {
0
}
}

// fun getNavigationBarHeight(activity: Activity, callback: NavigationBarCallback) {
// val view = activity.window.decorView
// val attachedToWindow = view.isAttachedToWindow
// if (attachedToWindow) {
// val windowInsets = ViewCompat.getRootWindowInsets(view)!!
// val height = windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
// val hasNavigationBar =
// windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars()) &&
// windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom > 0
// if (height > 0) {
// callback.onHeight(height, hasNavigationBar)
// } else {
// callback.onHeight(navBarHeight, hasNavigationBar)
// }
// } else {
// view.addOnAttachStateChangeListener(object : OnAttachStateChangeListener {
// override fun onViewAttachedToWindow(v: View) {
// val windowInsets = ViewCompat.getRootWindowInsets(v)!!
// val height =
// windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
// val hasNavigationBar =
// windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars()) &&
// windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom > 0
// if (height > 0) {
// callback.onHeight(height, hasNavigationBar)
// } else {
// callback.onHeight(navBarHeight, hasNavigationBar)
// }
// }
//
// override fun onViewDetachedFromWindow(v: View) {}
// })
// }
// }

interface KeyboardHeightListener {
fun onKeyboardHeightChanged(height: Int)
}
}

10 changes: 5 additions & 5 deletions app/src/main/java/com/cofbro/qian/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import com.hjq.toast.ToastUtils

fun String.showSignResult() {
var toast = ""
if (this.contains("success") || this.contains("签到成功")) {
toast = "签到成功"
toast = if (this.contains("success") || this.contains("签到成功")) {
"签到成功"
} else if (contains("签到过了")) {
toast = "您已经签到过啦~"
} else if (contains("失败")) {
toast = "签到失败!"
"您已经签到过啦~"
} else {
"签到失败!"
}
ToastUtils.show(toast)
}
37 changes: 37 additions & 0 deletions app/src/main/java/com/cofbro/qian/view/FullScreenDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.cofbro.qian.view

import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.core.view.WindowInsetsCompat.Type
import androidx.core.view.WindowInsetsCompat.Type.InsetsType
import com.cofbro.qian.R

class FullScreenDialog(context: Context) : AlertDialog(context, R.style.Dialog_Fullscreen) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(getLayoutResId())
window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
window?.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window?.statusBarColor = Color.TRANSPARENT
window?.navigationBarColor = Color.TRANSPARENT
val layoutParams = window?.attributes
window?.setBackgroundDrawable(null)
// 设置导航栏颜
window?.navigationBarColor = Color.TRANSPARENT
// 内容扩展到导航栏
window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL)
if (Build.VERSION.SDK_INT >= 28) {
layoutParams?.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
window?.attributes = layoutParams
}

private fun getLayoutResId(): Int = R.layout.dialog_full_screen
}
Loading
Loading