-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
android: impl. donation pending approval feature
- Loading branch information
Showing
15 changed files
with
291 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
dtb-android/app/src/main/java/app/roaim/dtbazar/ui/store_details/PendingDonationFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package app.roaim.dtbazar.ui.store_details | ||
|
||
import android.os.Bundle | ||
import android.view.* | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.core.graphics.drawable.RoundedBitmapDrawable | ||
import androidx.databinding.DataBindingComponent | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.Observer | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.navigation.fragment.navArgs | ||
import app.roaim.dtbazar.R | ||
import app.roaim.dtbazar.databinding.FragmentPendingDonationBinding | ||
import app.roaim.dtbazar.di.Injectable | ||
import app.roaim.dtbazar.model.Status | ||
import app.roaim.dtbazar.ui.home.HomeDonationAdapter | ||
import app.roaim.dtbazar.utils.* | ||
import javax.inject.Inject | ||
|
||
/** | ||
* A simple [Fragment] subclass. | ||
*/ | ||
class PendingDonationFragment : Fragment(), Injectable, Loggable { | ||
|
||
@Inject | ||
lateinit var glidePlaceHolder: RoundedBitmapDrawable | ||
|
||
@Inject | ||
lateinit var viewModelFactory: ViewModelProvider.Factory | ||
|
||
private val viewModel: PendingDonationViewModel by viewModels { viewModelFactory } | ||
|
||
private val navArgs by navArgs<PendingDonationFragmentArgs>() | ||
private var bindingComponent by autoCleared<DataBindingComponent>() | ||
private var binding by autoCleared<FragmentPendingDonationBinding>() | ||
private var adapter by autoCleared<HomeDonationAdapter>() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setHasOptionsMenu(true) | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View? { | ||
// Inflate the layout for this fragment | ||
bindingComponent = FragmentDataBindingComponent(this, glidePlaceHolder) | ||
binding = DataBindingUtil.inflate( | ||
inflater, | ||
R.layout.fragment_pending_donation, | ||
container, | ||
false, | ||
bindingComponent | ||
) | ||
adapter = HomeDonationAdapter() | ||
binding.rvDonation.adapter = adapter | ||
adapter.setItemClickListener { donation, _, isLongClick -> | ||
log("ItemClick: $donation") | ||
AlertDialog.Builder(binding.root.context) | ||
.setMessage("Accept donation ${donation?.currency} ${donation?.amount?.formatted()} to ${donation?.foodName} from ${donation?.donorName}") | ||
.setPositiveButton("Yes") { _, _ -> | ||
viewModel.approve(donation) | ||
} | ||
.setNegativeButton("No", null) | ||
.show() | ||
} | ||
viewModel.setStoreId(navArgs.storeId) | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
viewModel.pendingDonation.observe(viewLifecycleOwner, Observer { | ||
log("PendingDonation: $it") | ||
if (it.status == Status.SUCCESS) adapter.submitList(it.data) | ||
}) | ||
viewModel.approve.observe(viewLifecycleOwner, Observer { | ||
log("ApproveDonation: $it") | ||
if (it.status == Status.SUCCESS) { | ||
val list = adapter.currentList.toMutableList() | ||
list.remove(it.data) | ||
adapter.submitList(list) | ||
Toast.makeText(requireContext(), "Approved", Toast.LENGTH_SHORT).show() | ||
} else if (it.status == Status.FAILED) { | ||
Toast.makeText(requireContext(), "Failed to Approve. ${it.msg}", Toast.LENGTH_LONG) | ||
.show() | ||
} | ||
}) | ||
} | ||
|
||
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { | ||
super.onCreateOptionsMenu(menu, inflater) | ||
val menuReload = menu.add("Reload") | ||
menuReload.setIcon(R.drawable.ic_refresh) | ||
menuReload.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM) | ||
menuReload.setOnMenuItemClickListener { | ||
viewModel.onRetry() | ||
true | ||
} | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
dtb-android/app/src/main/java/app/roaim/dtbazar/ui/store_details/PendingDonationViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package app.roaim.dtbazar.ui.store_details | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.switchMap | ||
import app.roaim.dtbazar.data.repository.DonationRepository | ||
import app.roaim.dtbazar.model.Donation | ||
import app.roaim.dtbazar.model.Result | ||
import app.roaim.dtbazar.ui.RetryCallback | ||
import javax.inject.Inject | ||
|
||
class PendingDonationViewModel @Inject constructor( | ||
private val donationRepository: DonationRepository | ||
) : | ||
ViewModel(), RetryCallback { | ||
|
||
private val _storeId = MutableLiveData<String>() | ||
|
||
private val _approve = MutableLiveData<Donation>() | ||
|
||
val pendingDonation: LiveData<Result<List<Donation>>> = | ||
_storeId.switchMap { donationRepository.getPendingDonations(it) } | ||
|
||
val approve: LiveData<Result<Donation>> = | ||
_approve.switchMap { donationRepository.approveDonation(it) } | ||
|
||
fun approve(donation: Donation?) { | ||
_approve.value = donation | ||
} | ||
|
||
fun setStoreId(storeId: String) { | ||
if (_storeId.value == storeId) return | ||
_storeId.value = storeId | ||
} | ||
|
||
override fun onRetry() { | ||
_storeId.value = _storeId.value | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.