diff --git a/app/src/main/java/app/nexd/android/ui/helper/detail/HelperDetailFragment.kt b/app/src/main/java/app/nexd/android/ui/helper/detail/HelperDetailFragment.kt index 63f3233..3014703 100644 --- a/app/src/main/java/app/nexd/android/ui/helper/detail/HelperDetailFragment.kt +++ b/app/src/main/java/app/nexd/android/ui/helper/detail/HelperDetailFragment.kt @@ -1,5 +1,6 @@ package app.nexd.android.ui.helper.detail +import android.graphics.Typeface import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -9,9 +10,12 @@ import androidx.lifecycle.Observer import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.navArgs import androidx.recyclerview.widget.LinearLayoutManager +import app.nexd.android.R import app.nexd.android.api.model.HelpRequestArticle import app.nexd.android.databinding.FragmentHelperRequestDetailBinding +import app.nexd.android.ui.common.DefaultSnackBar import app.nexd.android.ui.common.HelpRequestArticleBinder +import com.google.android.material.snackbar.Snackbar import mva2.adapter.ListSection import mva2.adapter.MultiViewAdapter import org.koin.androidx.viewmodel.ext.android.viewModel @@ -65,9 +69,53 @@ class HelperDetailFragment : Fragment() { viewModel.idOfRequest.observe(viewLifecycleOwner, Observer { idOfRequest -> binding.buttonAccept.setOnClickListener { - viewModel.acceptRequest(idOfRequest) - findNavController().popBackStack() + viewModel.acceptOrDeclineRequest(idOfRequest) + } + }) + + viewModel.progress.observe(viewLifecycleOwner, Observer { progress -> + when (progress) { + is HelperDetailViewModel.Progress.Idle -> { + // do nothing + } + is HelperDetailViewModel.Progress.Loading -> { + // TODO: Show loading circle + } + is HelperDetailViewModel.Progress.Finished -> { + DefaultSnackBar( + requireParentFragment().requireView(), + progress.message, + Snackbar.LENGTH_SHORT + ) + findNavController().popBackStack() + } + is HelperDetailViewModel.Progress.Error -> { + DefaultSnackBar( + requireParentFragment().requireView(), + progress.message, + Snackbar.LENGTH_SHORT + ) + findNavController().popBackStack() + } + } + + }) + viewModel.requestIsAccepted.observe(viewLifecycleOwner, Observer { requestIsAccepted -> + if (requestIsAccepted) { + binding.buttonAccept.apply { + text = getString(R.string.helper_request_detail_button_cancel) + setTextColor(resources.getColor(android.R.color.holo_red_dark, null)) + setTypeface(null, Typeface.BOLD) + } + } else { + binding.buttonAccept.apply { + text = getString(R.string.helper_request_detail_button_accept) + setTextColor(resources.getColor(android.R.color.black, null)) + setTypeface(null, Typeface.NORMAL) + } } }) } + + } diff --git a/app/src/main/java/app/nexd/android/ui/helper/detail/HelperDetailViewModel.kt b/app/src/main/java/app/nexd/android/ui/helper/detail/HelperDetailViewModel.kt index 7bb874f..70bf662 100644 --- a/app/src/main/java/app/nexd/android/ui/helper/detail/HelperDetailViewModel.kt +++ b/app/src/main/java/app/nexd/android/ui/helper/detail/HelperDetailViewModel.kt @@ -1,19 +1,25 @@ package app.nexd.android.ui.helper.detail -import android.util.Log +import androidx.annotation.StringRes import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import app.nexd.android.Api +import app.nexd.android.R import app.nexd.android.api.model.HelpList import app.nexd.android.api.model.HelpListCreateDto import app.nexd.android.api.model.HelpRequestArticle import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import io.reactivex.rxkotlin.subscribeBy -import io.reactivex.schedulers.Schedulers.io class HelperDetailViewModel(private val api: Api) : ViewModel() { + sealed class Progress { + object Idle : Progress() + object Loading : Progress() + class Finished(@StringRes val message: Int) : Progress() + class Error(val message: String) : Progress() + } val additionalRequest = MutableLiveData() val firstName = MutableLiveData() @@ -24,13 +30,22 @@ class HelperDetailViewModel(private val api: Api) : ViewModel() { val city = MutableLiveData() val requestArticles = MutableLiveData>() val requestIsAccepted = MutableLiveData() - val buttonText = MutableLiveData() val idOfRequest = MutableLiveData() + val progress = MutableLiveData(Progress.Idle) + private val compositeDisposable = CompositeDisposable() - fun acceptRequest(requestId: Long) { - api.helpListsControllerGetUserLists(null) + fun acceptOrDeclineRequest(requestId: Long) { + progress.value = Progress.Loading + requestIsAccepted.value?.let { + if (it) declineRequest(requestId) + else acceptRequest(requestId) + } + } + + private fun acceptRequest(requestId: Long) { + val disposable = api.helpListsControllerGetUserLists(null) .map { lists -> if (lists.any { it.status == HelpList.StatusEnum.ACTIVE }) lists.first { it.status == HelpList.StatusEnum.ACTIVE } @@ -50,16 +65,46 @@ class HelperDetailViewModel(private val api: Api) : ViewModel() { ) } } - .subscribeOn(io()) - .doOnError { - Log.e("Error", it.message.toString()) - } - .blockingSubscribe() + .observeOn(AndroidSchedulers.mainThread()) + .subscribeBy( + onError = { + progress.value = Progress.Error(it.message ?: "Unknown Error") + }, + onComplete = { + progress.value = + Progress.Finished(R.string.helper_request_detail_request_accepted) + } + ) + compositeDisposable.add(disposable) + + } + + private fun declineRequest(requestId: Long) { + val helpListToDecline = + api.helpRequestsControllerGetSingleRequest(requestId) + val disposable = helpListToDecline + .observeOn(AndroidSchedulers.mainThread()) + .flatMap { helpListId -> + api.helpListsControllerDeleteHelpRequestFromHelpList( + helpListId.helpListId, + requestId + ) + + }.observeOn(AndroidSchedulers.mainThread()) + .subscribeBy( + onError = { + progress.value = Progress.Error(it.message ?: "Unknown Error") + }, + onComplete = { + progress.value = + Progress.Finished(R.string.helper_request_detail_request_declined) + } + ) + compositeDisposable.add(disposable) } fun setInfo(requestId: Long) { val observable = api.helpRequestsControllerGetSingleRequest(requestId) - val disposable = observable .observeOn(AndroidSchedulers.mainThread()) .subscribeBy( @@ -74,6 +119,7 @@ class HelperDetailViewModel(private val api: Api) : ViewModel() { additionalRequest.value = it.additionalRequest idOfRequest.value = it.id requestIsAccepted.value = (it.helpListId != null) + } ) @@ -84,4 +130,7 @@ class HelperDetailViewModel(private val api: Api) : ViewModel() { super.onCleared() compositeDisposable.clear() } + + // added comment for checking possible CI issues + } \ No newline at end of file diff --git a/app/src/main/java/app/nexd/android/ui/helper/requestOverview/HelperOverviewFragment.kt b/app/src/main/java/app/nexd/android/ui/helper/requestOverview/HelperOverviewFragment.kt index c11c50a..a6bfb08 100644 --- a/app/src/main/java/app/nexd/android/ui/helper/requestOverview/HelperOverviewFragment.kt +++ b/app/src/main/java/app/nexd/android/ui/helper/requestOverview/HelperOverviewFragment.kt @@ -12,7 +12,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import app.nexd.android.R import app.nexd.android.api.model.HelpRequest import app.nexd.android.databinding.FragmentHelperRequestOverviewBinding -import app.nexd.android.ui.common.DefaultSnackbar +import app.nexd.android.ui.common.DefaultSnackBar import app.nexd.android.ui.common.HelpRequestBinder import app.nexd.android.ui.dialog.SelectTextDialog import app.nexd.android.ui.helper.requestOverview.HelperOverviewFragmentDirections.Companion.requestDetailAction @@ -83,7 +83,7 @@ class HelperOverviewFragment : Fragment() { } is Error -> { progressBar.visibility = View.GONE - DefaultSnackbar(view, progress.message, Snackbar.LENGTH_SHORT) + DefaultSnackBar(view, progress.message, Snackbar.LENGTH_SHORT) } } }) diff --git a/app/src/main/res/layout/fragment_helper_request_detail.xml b/app/src/main/res/layout/fragment_helper_request_detail.xml index ae43755..ce57dfc 100644 --- a/app/src/main/res/layout/fragment_helper_request_detail.xml +++ b/app/src/main/res/layout/fragment_helper_request_detail.xml @@ -128,8 +128,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/rounded_white" - android:enabled="@{!viewModel.requestIsAccepted}" - android:text="@{viewModel.requestIsAccepted? @string/helper_request_detail_button_accepted : @string/helper_request_detail_button_accept}" android:textAllCaps="false" android:textSize="20sp" app:layout_constraintBottom_toBottomOf="parent" diff --git a/app/src/main/res/layout/fragment_transcript_info.xml b/app/src/main/res/layout/fragment_transcript_info.xml index e9fce1b..4084895 100644 --- a/app/src/main/res/layout/fragment_transcript_info.xml +++ b/app/src/main/res/layout/fragment_transcript_info.xml @@ -192,7 +192,7 @@ android:autofillHints="postal-code" android:background="@drawable/rounded_white" android:error="@{viewModel.zipCodeError}" - android:inputType="number" + android:inputType="textPostalAddress" android:nextFocusDown="@id/editText_city_value" android:padding="10dp" android:text="@={viewModel.zipCode}" diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index acbdc46..39e60ba 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -57,6 +57,7 @@ "Zu Einkaufen" "Anfrage bereits angenommen" "Mach ich!" + "Kann ich leider nicht machen " "Abgabe!" "Abgeben" "Bitte bestätige die Abgabe aller angenommen Aufträge" @@ -180,6 +181,8 @@ "Lieferhinweis" "The delivery address is not available." "Die Einkaufsliste ist fehlerhaft oder enthält keine Einträge." + Die Anfrage wurde angenommen! + Die Anfrage wurde abgelehnt. "Bisher hast du noch keine Anfragen angenommen. Bitte tippe auf eine offene Anfrage um sie anzusehen und anzunehmen." "Für deine eingestellte Region sind derzeit keine offenen Anfragen vorhanden. Bitte such in einer anderen Region in deiner Nähe oder hilf uns in dem du Telefonanrufe übersetzt." "Bitte gib den Namen, Anzahl und Maßeinheit des Artikels an den du zur Liste hinzufügen möchtest." diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f08bfcc..672f3be 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -57,6 +57,7 @@ "To collect" "Request already accepted" "Will do!" + "Have to cancel" "Delivery!" "Deliver" "Confirm the delivery of all accepted requests" @@ -180,6 +181,8 @@ phone call" "Delivery comment" "The delivery address is not available." "The shopping list is empty or failed to load." + The request has been accepted! + The request has been declined. "No accepted requests found. Please chose requests from the list below." "Currently there are no open requests in your selected region. Please try to check other regions nearby for open requests or help us by translating phone calls." "To add a new article to the list provide its name, amount and unit."