JianShu/app/src/main/java/edu/whut/jianshu/Activity/sendPerceptionActivity.kt

121 lines
5.3 KiB
Kotlin
Raw Normal View History

2024-05-15 12:14:20 +08:00
package edu.whut.jianshu.Activity
2024-04-25 10:33:59 +08:00
2024-04-27 14:00:50 +08:00
import android.content.Context
2024-04-25 10:33:59 +08:00
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
2024-04-25 10:33:59 +08:00
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import cn.bmob.v3.Bmob
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.QueryListener
2024-04-25 10:33:59 +08:00
import cn.bmob.v3.listener.SaveListener
import com.bumptech.glide.Glide
2024-05-15 12:14:20 +08:00
import edu.whut.jianshu.Adapter.selectedBook
import edu.whut.jianshu.Adapter.send_perceptionAdapter
import edu.whut.jianshu.Data.entity.Book_info_bmob
import edu.whut.jianshu.Data.entity.Perception_bmob
import edu.whut.jianshu.Data.entity.User_bmob
import edu.whut.jianshu.Data.vo.Book_Shelf
import edu.whut.jianshu.R
2024-04-25 10:33:59 +08:00
import kotlinx.android.synthetic.main.activity_sendperception.*
class sendPerceptionActivity:AppCompatActivity() {
private var bookList = ArrayList<Book_Shelf>()
2024-04-27 14:00:50 +08:00
lateinit var userId:String
private lateinit var adapter: send_perceptionAdapter
2024-04-25 10:33:59 +08:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sendperception)
2024-04-27 14:00:50 +08:00
userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString()
mycontent.requestFocus()
// 弹出软键盘
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(mycontent, InputMethodManager.SHOW_IMPLICIT)
// 防止自动弹出软键盘时页面被顶起
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
adapter = send_perceptionAdapter(bookList)
2024-04-25 10:33:59 +08:00
val layoutManager =
StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL)//第一个参数是列数
send_recyclerview.layoutManager = layoutManager
send_recyclerview.adapter = adapter
inits()
send_return.setOnClickListener {
// val intent= Intent(this,PerceptionActivity::class.java)
// startActivity(intent)
2024-04-25 10:33:59 +08:00
finish()
}
submit.setOnClickListener {
if(selectedBook.equals("")){
Toast.makeText(this,"请选择一本书!",Toast.LENGTH_SHORT).show()
}
else {
val perception = Perception_bmob()
perception.perception = mycontent.text.toString()
perception.userid = userId
2024-04-25 10:33:59 +08:00
perception.b_name = selectedBook
perception.save(object : SaveListener<String>() {
override fun done(objectId: String?, e: BmobException?) {
if (e == null) {
Toast.makeText(
this@sendPerceptionActivity, "发布成功!",
Toast.LENGTH_SHORT
).show()
val intent =
Intent(this@sendPerceptionActivity, PerceptionActivity::class.java)
startActivity(intent)
finish()
} else {
Toast.makeText(
this@sendPerceptionActivity,
"发布失败" + e.message,
Toast.LENGTH_SHORT
).show()
}
}
})
}
}
}
private fun inits(){
2024-04-26 19:57:44 +08:00
val bmobQuery = BmobQuery<Book_info_bmob>()
bmobQuery.findObjects(object : FindListener<Book_info_bmob>() {
override fun done(lists: List<Book_info_bmob>, e: BmobException?) {
2024-04-25 10:33:59 +08:00
if (e == null) {
bookList.clear()
lists?.let {
bookList.addAll(it.map { b ->
// 使用安全调用和Elvis操作符提供默认值或进行错误处理
val url = b.picture?.url ?: "default_url_or_handling_case"
Book_Shelf(b.name,url)
})
2024-04-25 10:33:59 +08:00
}
adapter.notifyDataSetChanged()
2024-04-25 10:33:59 +08:00
} else {
Log.d("error", "error")
}
}
})
val queryUser = BmobQuery<User_bmob>()
queryUser.getObject(userId, object : QueryListener<User_bmob>() {
override fun done(user: User_bmob?, e: BmobException?) {
if (e == null) {
user?.let {
Glide.with(this@sendPerceptionActivity) // 传入 Context
2024-05-22 18:50:49 +08:00
.load(it.face?.url ?: "https://bmob-cdn-31452.bmobpay.com/2024/04/30/6f0e4b19405153ed80f2f1bdfa10da0b.png") // 加载图片的 URL
.placeholder(R.drawable.pre_load) // 设置占位图
.error(R.drawable.fail_load) // 设置加载失败时显示的图片
.into(send_avatar) // 将图片加载到指定的 ImageView 中
2024-04-25 10:33:59 +08:00
}
} else {
Log.e("QueryUser", e.toString())
2024-04-25 10:33:59 +08:00
}
}
})
}
}