Skip to content

Commit

Permalink
Made some changes in the ViewModel and fixed the toast
Browse files Browse the repository at this point in the history
  • Loading branch information
aritra-tech committed Feb 24, 2023
1 parent 5207675 commit 08ab3fb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class AccountAdapter(private val context: Context, private val listner: (Account
"google drive" -> holder.itemView.findViewById<ImageView>(R.id.img_company).setImageResource(R.drawable.drive)
"netflix" -> holder.itemView.findViewById<ImageView>(R.id.img_company).setImageResource(R.drawable.netflix_logo)
"amazon prime" -> holder.itemView.findViewById<ImageView>(R.id.img_company).setImageResource(R.drawable.amazon_logo)
"spotify" -> holder.itemView.findViewById<ImageView>(R.id.img_company).setImageResource(R.drawable.amazon)
"spotify" -> holder.itemView.findViewById<ImageView>(R.id.img_company).setImageResource(R.drawable.spotify)
"discord" -> holder.itemView.findViewById<ImageView>(R.id.img_company).setImageResource(R.drawable.discord)
"others" -> holder.itemView.findViewById<ImageView>(R.id.img_company).setImageResource(R.drawable.general_account)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ class AccountAdapter(private val context: Context, private val listner: (Account
}

fun setData(account: List<Account>){
this.accountList = account
accountList = account
notifyDataSetChanged()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.view.children
import androidx.lifecycle.ViewModelProvider
Expand Down Expand Up @@ -53,6 +54,7 @@ class AddFragment : Fragment() {

requireActivity().findViewById<BottomNavigationView>(R.id.bottom_nav).visibility = View.GONE

init()

binding.accType.afterTextChanged{
selectedAccount = it
Expand Down Expand Up @@ -87,11 +89,11 @@ class AddFragment : Fragment() {
val password = binding.password.text.toString()

if (company.isEmpty() || email.isEmpty() || password.isEmpty()){
Snackbar.make(binding.root, "Please fill all the details", Snackbar.LENGTH_SHORT).show()
Snackbar.make(it, "Please fill all the details", Snackbar.LENGTH_SHORT).show()
return@setOnClickListener
}
else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
Snackbar.make(binding.root, "Please check your Email Id", Snackbar.LENGTH_SHORT).show()
Snackbar.make(it, "Please check your Email Id", Snackbar.LENGTH_SHORT).show()
return@setOnClickListener
}
insertDataToDB()
Expand All @@ -111,6 +113,23 @@ class AddFragment : Fragment() {

}

private fun init() {
myViewModel.saveStatus.observe(viewLifecycleOwner){

if (it == 2){
Toast.makeText(requireContext(), "Successful Saved", Toast.LENGTH_SHORT).show()
// Moving into HomeFragment after saving
val frag = HomeFragment()
val trans = fragmentManager?.beginTransaction()
trans?.replace(R.id.fragment,frag)?.commit()

}else if (it == 6){
Toast.makeText(requireContext(), "Failed to save", Toast.LENGTH_SHORT).show()
}
}

}

private fun setImageOnAccountNameChange(imageID: Int){
binding.accountLogo.apply {
setImageResource(imageID)
Expand Down Expand Up @@ -194,16 +213,10 @@ class AddFragment : Fragment() {

val account = Account(0,company, email, category,userName, password)

// viewModel.addAccount(account)
lifecycleScope.launch {
myViewModel.saveData(viewModel,account)
}

Snackbar.make(binding.root, "Successful Saved", Snackbar.LENGTH_SHORT).show()
// Moving into HomeFragment after saving
val frag = HomeFragment()
val trans = fragmentManager?.beginTransaction()
trans?.replace(R.id.fragment,frag)?.commit()
}

private fun AutoCompleteTextView.afterTextChanged(afterTextChanged: (String) -> Unit){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class EditFragment : Fragment() {
.getInstance(requireActivity().application)
)[AddEditViewModel::class.java]

initObservers()

// Fetching data from adapter
val id = arguments?.getInt("id")
Expand Down Expand Up @@ -118,6 +119,23 @@ class EditFragment : Fragment() {
return binding.root
}

private fun initObservers() {
editViewModel.updateStatus.observe(viewLifecycleOwner){

if (it == 1){
Toast.makeText(requireContext(),"Successfully Updated",Toast.LENGTH_SHORT).show()
val frag = HomeFragment()
val trans = fragmentManager?.beginTransaction()
trans?.replace(R.id.fragment,frag)?.commit()
}
else if (it == 5){
Toast.makeText(requireContext(),"Failed to edit",Toast.LENGTH_SHORT).show()
}

}

}

private fun setImageOnAccountNameChange(imageID: Int) {
binding.accountLogo.apply {
setImageResource(imageID)
Expand Down Expand Up @@ -214,26 +232,6 @@ class EditFragment : Fragment() {
editViewModel.updateData(accountViewModel,account)
}
}

// accountViewModel.editAccount(updateAccount)

editViewModel.updateStatus.observe(viewLifecycleOwner){

if (it == 1){
Toast.makeText(requireContext(),"Successfully Updated",Toast.LENGTH_LONG).show()
}
else if (it == 5){
Toast.makeText(requireContext(),"Failed to edit",Toast.LENGTH_LONG).show()
}

}
val frag = HomeFragment()
val trans = fragmentManager?.beginTransaction()
trans?.replace(R.id.fragment,frag)?.commit()

}



}

0 comments on commit 08ab3fb

Please sign in to comment.