Skip to content

Instantly share code, notes, and snippets.

@Ochornma
Created April 25, 2021 18:31
Show Gist options
  • Select an option

  • Save Ochornma/10107a10754ec324cabe3aea1cc25a61 to your computer and use it in GitHub Desktop.

Select an option

Save Ochornma/10107a10754ec324cabe3aea1cc25a61 to your computer and use it in GitHub Desktop.
class VerifyBVNFragment : ScopedFragment(), KodeinAware, VerifyAdapter.OnItemClickListener {
companion object {
fun newInstance() = VerifyBVNFragment()
}
override val kodein by closestKodein()
private val viewModelFactory: VerifyBVNFactory by instance()
private var _binding: VerifyBVNFragmentBinding? = null
private val binding get() = _binding!!
private lateinit var viewModel: VerifyBVNViewModel
private lateinit var adapter: VerifyAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = VerifyBVNFragmentBinding.inflate(inflater, container, false)
binding.spinKit.visibility = View.VISIBLE
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProvider(this, viewModelFactory).get(VerifyBVNViewModel::class.java)
bindUI()
}
fun bindUI(){
val layoutManager = LinearLayoutManager(binding.root.context)
layoutManager.reverseLayout = true
layoutManager.stackFromEnd = true
binding.recyclerView.layoutManager = layoutManager
viewModel.getUnverifiedBVN { response ->
if (response.isSuccessful){
binding.spinKit.visibility = View.GONE
if (response.body()?.data.isNullOrEmpty()){
binding.nobvn.visibility = View.VISIBLE
binding.nobvn.text = getString(R.string.no_type_)
}else{
binding.nobvn.visibility = View.GONE
adapter = VerifyAdapter(response.body()?.data!!, this)
binding.recyclerView.adapter = adapter
adapter.notifyDataSetChanged()
}
}else{
binding.spinKit.visibility = View.GONE
if (response.code() == 401){
toastMessage( "Session Expired")
Navigation.findNavController(binding.root).navigate(R.id.login)
}else {
Toast.makeText(requireContext(), "failed fetching unverified BVN", Toast.LENGTH_SHORT).show()
}
}
}
}
private fun toastMessage(message: String){
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
}
fun verifyBVNAlert(id: String){
val sPref = binding.root.context.getSharedPreferences("identifier", Context.MODE_PRIVATE)
MaterialAlertDialogBuilder(requireContext(),
R.style.ThemeOverlay_MaterialComponents_MaterialAlertDialog_Background_Accept)
.setTitle(getString(R.string.approve))
.setMessage(getString(R.string.approve_bvn))
.setPositiveButton("YES") { d, _ ->
val adminId = sPref.getInt(Constants.ADMIN_ID, 0).toString()
val adminIdRe: RequestBody = adminId.toRequestBody(adminId.toMediaTypeOrNull())
val userIdRe: RequestBody = id.toRequestBody(id.toMediaTypeOrNull())
viewModel.verifyBVN(userId = userIdRe, adminId = adminIdRe){ res ->
if (!res.isSuccessful){
if (res.code() == 401){
toastMessage( "Session Expired.")
Navigation.findNavController(binding.root).navigate(R.id.login)
}else {
Toast.makeText(requireContext(), "Failed to verify BVN", Toast.LENGTH_SHORT).show()
}
}else{
Toast.makeText(requireContext(), res.body()?.Response_message, Toast.LENGTH_SHORT).show()
}
}
d.dismiss()
}
.setNegativeButton("NO"){d, _ ->
d.dismiss()
}
.show()
}
override fun onItemClick(id: String) {
verifyBVNAlert(id)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment