From 08ab3fb9666281fd35f3a98bd72734e81a7bfcbe Mon Sep 17 00:00:00 2001 From: Aritra Das Date: Sat, 25 Feb 2023 01:27:53 +0530 Subject: [PATCH] Made some changes in the ViewModel and fixed the toast --- .../uncrack/adapter/AccountAdapter.kt | 4 +- .../uncrack/ui/fragments/AddFragment.kt | 29 ++++++++++---- .../uncrack/ui/fragments/EditFragment.kt | 38 +++++++++---------- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/geekymusketeers/uncrack/adapter/AccountAdapter.kt b/app/src/main/java/com/geekymusketeers/uncrack/adapter/AccountAdapter.kt index b5f03b12..b327f7d9 100644 --- a/app/src/main/java/com/geekymusketeers/uncrack/adapter/AccountAdapter.kt +++ b/app/src/main/java/com/geekymusketeers/uncrack/adapter/AccountAdapter.kt @@ -63,7 +63,7 @@ class AccountAdapter(private val context: Context, private val listner: (Account "google drive" -> holder.itemView.findViewById(R.id.img_company).setImageResource(R.drawable.drive) "netflix" -> holder.itemView.findViewById(R.id.img_company).setImageResource(R.drawable.netflix_logo) "amazon prime" -> holder.itemView.findViewById(R.id.img_company).setImageResource(R.drawable.amazon_logo) - "spotify" -> holder.itemView.findViewById(R.id.img_company).setImageResource(R.drawable.amazon) + "spotify" -> holder.itemView.findViewById(R.id.img_company).setImageResource(R.drawable.spotify) "discord" -> holder.itemView.findViewById(R.id.img_company).setImageResource(R.drawable.discord) "others" -> holder.itemView.findViewById(R.id.img_company).setImageResource(R.drawable.general_account) } @@ -97,7 +97,7 @@ class AccountAdapter(private val context: Context, private val listner: (Account } fun setData(account: List){ - this.accountList = account + accountList = account notifyDataSetChanged() } } \ No newline at end of file diff --git a/app/src/main/java/com/geekymusketeers/uncrack/ui/fragments/AddFragment.kt b/app/src/main/java/com/geekymusketeers/uncrack/ui/fragments/AddFragment.kt index e9eb0f30..7df58dbb 100644 --- a/app/src/main/java/com/geekymusketeers/uncrack/ui/fragments/AddFragment.kt +++ b/app/src/main/java/com/geekymusketeers/uncrack/ui/fragments/AddFragment.kt @@ -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 @@ -53,6 +54,7 @@ class AddFragment : Fragment() { requireActivity().findViewById(R.id.bottom_nav).visibility = View.GONE + init() binding.accType.afterTextChanged{ selectedAccount = it @@ -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() @@ -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) @@ -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){ diff --git a/app/src/main/java/com/geekymusketeers/uncrack/ui/fragments/EditFragment.kt b/app/src/main/java/com/geekymusketeers/uncrack/ui/fragments/EditFragment.kt index 70819bb1..4660b805 100644 --- a/app/src/main/java/com/geekymusketeers/uncrack/ui/fragments/EditFragment.kt +++ b/app/src/main/java/com/geekymusketeers/uncrack/ui/fragments/EditFragment.kt @@ -51,6 +51,7 @@ class EditFragment : Fragment() { .getInstance(requireActivity().application) )[AddEditViewModel::class.java] + initObservers() // Fetching data from adapter val id = arguments?.getInt("id") @@ -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) @@ -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() - } - - - }