|
@@ -1,193 +1,29 @@
|
|
|
package org.brynhild.graduation.activity.teacher
|
|
|
|
|
|
-
|
|
|
-import android.Manifest
|
|
|
-import android.content.Context
|
|
|
-import android.content.Intent
|
|
|
-import android.content.pm.PackageManager
|
|
|
-import android.os.Build
|
|
|
import android.os.Bundle
|
|
|
-import android.util.Log
|
|
|
-import android.view.LayoutInflater
|
|
|
-import android.view.Menu
|
|
|
-import android.view.MenuItem
|
|
|
-import android.widget.Toast
|
|
|
-import androidx.core.view.GravityCompat
|
|
|
-import androidx.recyclerview.widget.GridLayoutManager
|
|
|
-import com.bumptech.glide.Glide
|
|
|
-import com.google.gson.Gson
|
|
|
+import com.google.android.material.bottomnavigation.BottomNavigationView
|
|
|
+import androidx.appcompat.app.AppCompatActivity
|
|
|
+import androidx.navigation.findNavController
|
|
|
+import androidx.navigation.ui.AppBarConfiguration
|
|
|
+import androidx.navigation.ui.setupActionBarWithNavController
|
|
|
+import androidx.navigation.ui.setupWithNavController
|
|
|
import org.brynhild.graduation.R
|
|
|
-import org.brynhild.graduation.activity.common.AccountInfoActivity
|
|
|
import org.brynhild.graduation.activity.common.BaseActivity
|
|
|
-import org.brynhild.graduation.activity.common.LoginActivity
|
|
|
-import org.brynhild.graduation.common.config.LoginConfiguration
|
|
|
-import org.brynhild.graduation.common.config.MyApplication
|
|
|
-import org.brynhild.graduation.common.constant.FileStorageConstant
|
|
|
-import org.brynhild.graduation.common.transfer.Result
|
|
|
-import org.brynhild.graduation.common.transfer.handler.ResponseHandler
|
|
|
-import org.brynhild.graduation.common.transfer.utils.ServiceCreator
|
|
|
-import org.brynhild.graduation.common.utils.JsonUtils
|
|
|
import org.brynhild.graduation.databinding.ActivityTeacherMainPageBinding
|
|
|
-import org.brynhild.graduation.databinding.NavHeaderBinding
|
|
|
-import org.brynhild.graduation.network.entiity.User
|
|
|
-import org.brynhild.graduation.service.http.AccountService
|
|
|
-import org.brynhild.graduation.service.mqtt.MqttListenService
|
|
|
-import retrofit2.Call
|
|
|
-import retrofit2.Callback
|
|
|
-import retrofit2.Response
|
|
|
|
|
|
class TeacherMainPageActivity : BaseActivity() {
|
|
|
|
|
|
- //
|
|
|
- private var currentPage = 0
|
|
|
-
|
|
|
- private var lastBack = 0L
|
|
|
- private lateinit var activityMainPageBinding: ActivityTeacherMainPageBinding
|
|
|
- private lateinit var navHeaderBinding: NavHeaderBinding
|
|
|
-
|
|
|
- private fun getPermission() {
|
|
|
- if (Build.VERSION.SDK_INT >= 23) {
|
|
|
- val REQUEST_CODE_CONTACT = 101
|
|
|
- val permissions = arrayOf<String>(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
|
|
- //验证是否许可权限
|
|
|
- for (str in permissions) {
|
|
|
- if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
|
|
|
- //申请权限
|
|
|
- this.requestPermissions(permissions, REQUEST_CODE_CONTACT)
|
|
|
- }
|
|
|
- }
|
|
|
- FileStorageConstant.initStorage()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+ private lateinit var binding: ActivityTeacherMainPageBinding
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
|
- activityMainPageBinding = ActivityTeacherMainPageBinding.inflate(LayoutInflater.from(this))
|
|
|
- setContentView(activityMainPageBinding.root)
|
|
|
- setSupportActionBar(activityMainPageBinding.toolBar)
|
|
|
- supportActionBar?.let {
|
|
|
- it.setDisplayHomeAsUpEnabled(true)
|
|
|
- it.setHomeAsUpIndicator(R.drawable.ic_menu)
|
|
|
- it.title = "Favorite"
|
|
|
- }
|
|
|
-
|
|
|
- getPermission()
|
|
|
-
|
|
|
- val layoutManager = GridLayoutManager(this, 2)
|
|
|
- activityMainPageBinding.recyclerView.layoutManager = layoutManager
|
|
|
- activityMainPageBinding.navView.setCheckedItem(R.id.navFavorite)
|
|
|
- currentPage = R.id.navFavorite
|
|
|
-
|
|
|
- activityMainPageBinding.navView.setNavigationItemSelectedListener { it ->
|
|
|
- when (it.itemId) {
|
|
|
- R.id.navExit -> {
|
|
|
- LoginConfiguration.login_password = ""
|
|
|
- saveLoginInfo()
|
|
|
- val i = Intent(MyApplication.context, LoginActivity::class.java)
|
|
|
- startActivity(i)
|
|
|
- finish()
|
|
|
- }
|
|
|
- }
|
|
|
- activityMainPageBinding.drawerLayout.closeDrawers()
|
|
|
- true
|
|
|
- }
|
|
|
- activityMainPageBinding.swipeRefresh.setOnRefreshListener {
|
|
|
- Thread {
|
|
|
- when (currentPage) {
|
|
|
-
|
|
|
- R.id.navExit -> {
|
|
|
- LoginConfiguration.login_password = ""
|
|
|
- saveLoginInfo()
|
|
|
- val serviceIntent =
|
|
|
- Intent(MyApplication.context, MqttListenService::class.java)
|
|
|
- stopService(serviceIntent)
|
|
|
- val i = Intent(MyApplication.context, LoginActivity::class.java)
|
|
|
- startActivity(i)
|
|
|
- finish()
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }.start()
|
|
|
- }
|
|
|
- val headerView = activityMainPageBinding.navView.inflateHeaderView(R.layout.nav_header)
|
|
|
- navHeaderBinding = NavHeaderBinding.bind(headerView)
|
|
|
- navHeaderBinding.navProfile.setOnClickListener {
|
|
|
- val intent = Intent(this, AccountInfoActivity::class.java)
|
|
|
- intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
|
|
- this.startActivity(intent)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private fun loadUserInfo() {
|
|
|
- Thread {
|
|
|
- val service = ServiceCreator.create(AccountService::class.java)
|
|
|
- service.accountInfo(LoginConfiguration.userInfo!!.token)
|
|
|
- .enqueue(object : Callback<Result> {
|
|
|
- override fun onResponse(call: Call<Result>, response: Response<Result>) {
|
|
|
- ResponseHandler.handle(response, {
|
|
|
- val json = Gson().toJson(it.data)
|
|
|
- val user = JsonUtils.fromJson<User>(json)
|
|
|
- if (user != null) {
|
|
|
- navHeaderBinding.userText.text = user.name
|
|
|
- navHeaderBinding.mailText.text = user.email
|
|
|
- Glide.with(this@TeacherMainPageActivity)
|
|
|
- .load(user.avatar ?: R.drawable.nav_icon_default)
|
|
|
- .into(navHeaderBinding.iconImage)
|
|
|
- }
|
|
|
- }, {
|
|
|
- Toast.makeText(MyApplication.context, "请求用户基本信息失败", Toast.LENGTH_SHORT)
|
|
|
- .show()
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- override fun onFailure(call: Call<Result>, t: Throwable) {
|
|
|
- Toast.makeText(MyApplication.context, "请求用户基本信息失败", Toast.LENGTH_SHORT)
|
|
|
- .show()
|
|
|
- }
|
|
|
- })
|
|
|
- }.start()
|
|
|
- }
|
|
|
-
|
|
|
- override fun onResume() {
|
|
|
- super.onResume()
|
|
|
- loadUserInfo()
|
|
|
- }
|
|
|
|
|
|
- override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
|
|
- return true
|
|
|
- }
|
|
|
+ binding = ActivityTeacherMainPageBinding.inflate(layoutInflater)
|
|
|
+ setContentView(binding.root)
|
|
|
|
|
|
- override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
- when (item.itemId) {
|
|
|
- android.R.id.home -> activityMainPageBinding.drawerLayout.openDrawer(GravityCompat.START)
|
|
|
- R.id.add -> Toast.makeText(this, "You clicked Backup", Toast.LENGTH_SHORT).show()
|
|
|
- R.id.delete -> Toast.makeText(this, "You clicked Delete", Toast.LENGTH_SHORT).show()
|
|
|
- R.id.settings -> Toast.makeText(this, "You clicked Settings", Toast.LENGTH_SHORT).show()
|
|
|
- }
|
|
|
- return true
|
|
|
- }
|
|
|
-
|
|
|
- override fun onBackPressed() {
|
|
|
- if (lastBack == 0L || System.currentTimeMillis() - lastBack > 2000) {
|
|
|
- Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show()
|
|
|
- lastBack = System.currentTimeMillis()
|
|
|
- } else {
|
|
|
- val serviceIntent = Intent(MyApplication.context, MqttListenService::class.java)
|
|
|
- stopService(serviceIntent)
|
|
|
- finish()
|
|
|
- }
|
|
|
- }
|
|
|
+ val navView: BottomNavigationView = binding.navView
|
|
|
|
|
|
- private fun saveLoginInfo() {
|
|
|
- val accountName = LoginConfiguration.login_username
|
|
|
- val accountPassword = LoginConfiguration.login_password
|
|
|
- val sp = getSharedPreferences("account", Context.MODE_PRIVATE)
|
|
|
- with(sp.edit()) {
|
|
|
- putString("accountName", accountName)
|
|
|
- putString("accountPassword", accountPassword)
|
|
|
- apply()
|
|
|
- }
|
|
|
- Log.d("LoginActivity", "save info:name:$accountName,password:$accountPassword")
|
|
|
+ val navController = findNavController(R.id.nav_host_fragment_activity_teacher_main_page)
|
|
|
+ navView.setupWithNavController(navController)
|
|
|
}
|
|
|
}
|