package com.example.personalwork
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.BindingAdapter
import androidx.lifecycle.MutableLiveData
import androidx.navigation.findNavController
import com.example.personalwork.data.models.DaySum
import com.example.personalwork.fragment.bottom.ContactFragmentDirections
import com.example.personalwork.fragment.list.studentListFragmentDirections
import com.google.android.material.floatingactionbutton.FloatingActionButton
class BindingAdapters {
companion object{
@BindingAdapter("android:navigateToAddFragment")
@JvmStatic
fun navigateToAddFragment(view: FloatingActionButton, navigate: Boolean){
view.setOnClickListener{
if(navigate){
view.findNavController().navigate(R.id.action_contactFragment_to_addFragment)
}
}
}
@BindingAdapter("android:emptyDatabase")
@JvmStatic
fun emptyDatabase(view:View,emptyDatabase: MutableLiveData<Boolean>){
when(emptyDatabase.value){
true -> view.visibility = View.VISIBLE
false -> view.visibility = View.INVISIBLE
else -> {}
}
}
@BindingAdapter("android:sendDataToUpdateFragment")
@JvmStatic
fun sendDataToUpdateFragment(view: ConstraintLayout,currentItem: DaySum){
view.setOnClickListener{
val action = ContactFragmentDirections.actionContactFragmentToUpdateFragment(currentItem)
view.findNavController().navigate(action)
}
}
}
}
package com.example.personalwork
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupActionBarWithNavController
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//find nav.xml from activity_main.xml
val navHostFragment = supportFragmentManager.findFragmentById(R.id.navHostFragment) as NavHostFragment
val navController =navHostFragment.navController
setupActionBarWithNavController(navController)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.navHostFragment)
return navController.navigateUp() || super.onSupportNavigateUp()
}
}
package com.example.personalwork
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.example.personalwork.R
import com.example.personalwork.data.models.Week
import com.example.personalwork.databinding.FragmentUpgrament2Binding
import com.example.personalwork.utils.ApiClient
import kotlinx.coroutines.launch
class Upgrament2Fragment : Fragment() {
var _binding : FragmentUpgrament2Binding? = null
val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentUpgrament2Binding.inflate(inflater,container,false)
init()
setHasOptionsMenu(true)
return binding.root
}
fun init(){
val week = readUserDataFromSharedPreferences(requireContext())
binding.textView.setText(week.title)
binding.textStart.setText(week.starttime)
binding.textFinal.setText(week.finaltime)
binding.textAming.setText(week.aming.toString())
binding.editTextTextMultiLine.setText(week.description)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.update_fragment_menu,menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean{
when(item.itemId){
R.id.menu_save -> updateItem()
R.id.menu_delete -> deleteItem()
}
return super.onOptionsItemSelected(item)
}
private fun deleteItem() {
val id = requireid(requireContext());
lifecycleScope.launch{
try {
val deleteResponse = ApiClient.apiService.deleteWeek(id)
val result = deleteResponse.body()
if (result!=null){
Toast.makeText(requireContext(),"删除成功", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(requireContext(),"删除失败", Toast.LENGTH_SHORT).show()
}
}catch (e:Exception){
e.printStackTrace()
}
}
}
private fun updateItem() {
val week = newWeek(requireContext())
lifecycleScope.launch {
try {
val updateResponse = ApiClient.apiService.updateWeek(week)
val result = updateResponse.body()
if(result != null){
Toast.makeText(requireContext(),"修改成功", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(requireContext(),"修改失败", Toast.LENGTH_SHORT).show()
}
}catch (e:Exception){
e.printStackTrace()
}
}
}
fun readUserDataFromSharedPreferences(context: Context): Week {
val sharedPreferences: SharedPreferences = context.getSharedPreferences("Week", Context.MODE_PRIVATE)
val id = sharedPreferences.getInt("id",0)
val useId =sharedPreferences.getInt("userId",0)
val title = sharedPreferences.getString("title","")
val description = sharedPreferences.getString("description","")
val starttime = sharedPreferences.getString("startTime","")
val finaltime = sharedPreferences.getString("finalTime","")
val aming = sharedPreferences.getInt("aming",0)
val accomplish = sharedPreferences.getInt("accomplish",0)
return Week(id,useId,title?:"",description?:"",starttime?:"",finaltime?:"",aming,accomplish)
}
fun newWeek(context: Context):Week{
val sharedPreferences: SharedPreferences = context.getSharedPreferences("Week", Context.MODE_PRIVATE)
val id = sharedPreferences.getInt("id",0)
val useId =sharedPreferences.getInt("userId",0)
val accomplish = sharedPreferences.getInt("accomplish",0)
val title = binding.textView.text.toString()
val start = binding.textStart.text.toString()
val final = binding.textFinal.text.toString()
val description = binding.editTextTextMultiLine.text.toString()
val aming = binding.textAming.text.toString().toInt()
return Week(id,useId,title,description,start,final,aming,accomplish)
}
fun requireid(context: Context): Int{
val sharedPreferences: SharedPreferences = context.getSharedPreferences("Week", Context.MODE_PRIVATE)
val id = sharedPreferences.getInt("id",0)
return id;
}
override fun onDestroy() {
super.onDestroy()
_binding=null;
}
}