Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper is able to cancel accepted request #137

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
})
}


}
Original file line number Diff line number Diff line change
@@ -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<String>()
val firstName = MutableLiveData<String>()
Expand All @@ -24,13 +30,22 @@ class HelperDetailViewModel(private val api: Api) : ViewModel() {
val city = MutableLiveData<String>()
val requestArticles = MutableLiveData<List<HelpRequestArticle>>()
val requestIsAccepted = MutableLiveData<Boolean>()
val buttonText = MutableLiveData<String>()
val idOfRequest = MutableLiveData<Long>()

val progress = MutableLiveData<Progress>(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 }
Expand All @@ -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(
Expand All @@ -74,6 +119,7 @@ class HelperDetailViewModel(private val api: Api) : ViewModel() {
additionalRequest.value = it.additionalRequest
idOfRequest.value = it.id
requestIsAccepted.value = (it.helpListId != null)

}
)

Expand All @@ -84,4 +130,7 @@ class HelperDetailViewModel(private val api: Api) : ViewModel() {
super.onCleared()
compositeDisposable.clear()
}

// added comment for checking possible CI issues

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
})
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/res/layout/fragment_helper_request_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_transcript_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<string name="helper_request_detail_screen_title">"Zu Einkaufen"</string>
<string name="helper_request_detail_button_accepted">"Anfrage bereits angenommen"</string>
<string name="helper_request_detail_button_accept">"Mach ich!"</string>
<string name="helper_request_detail_button_cancel">"Kann ich leider nicht machen "</string>
<string name="delivery_screen_title">"Abgabe!"</string>
<string name="delivery_dialog_deliver_title">"Abgeben"</string>
<string name="delivery_dialog_deliver_description">"Bitte bestätige die Abgabe aller angenommen Aufträge"</string>
Expand Down Expand Up @@ -180,6 +181,8 @@
<string name="helper_request_detail_delivery_comment_header">"Lieferhinweis"</string>
<string name="helper_request_detail_empty_delivery_address_placeholder">"The delivery address is not available."</string>
<string name="helper_request_detail_empty_list_placeholder">"Die Einkaufsliste ist fehlerhaft oder enthält keine Einträge."</string>
<string name="helper_request_detail_request_accepted">Die Anfrage wurde angenommen!</string>
<string name="helper_request_detail_request_declined">Die Anfrage wurde abgelehnt.</string>
<string name="helper_request_overview_empty_accepted_requests_list_placeholder">"Bisher hast du noch keine Anfragen angenommen. Bitte tippe auf eine offene Anfrage um sie anzusehen und anzunehmen."</string>
<string name="helper_request_overview_empty_open_requests_list_placeholder">"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."</string>
<string name="seeker_article_input_description">"Bitte gib den Namen, Anzahl und Maßeinheit des Artikels an den du zur Liste hinzufügen möchtest."</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<string name="helper_request_detail_screen_title">"To collect"</string>
<string name="helper_request_detail_button_accepted">"Request already accepted"</string>
<string name="helper_request_detail_button_accept">"Will do!"</string>
<string name="helper_request_detail_button_cancel">"Have to cancel"</string>
<string name="delivery_screen_title">"Delivery!"</string>
<string name="delivery_dialog_deliver_title">"Deliver"</string>
<string name="delivery_dialog_deliver_description">"Confirm the delivery of all accepted requests"</string>
Expand Down Expand Up @@ -180,6 +181,8 @@ phone call"</string>
<string name="helper_request_detail_delivery_comment_header">"Delivery comment"</string>
<string name="helper_request_detail_empty_delivery_address_placeholder">"The delivery address is not available."</string>
<string name="helper_request_detail_empty_list_placeholder">"The shopping list is empty or failed to load."</string>
<string name="helper_request_detail_request_accepted">The request has been accepted!</string>
<string name="helper_request_detail_request_declined">The request has been declined.</string>
<string name="helper_request_overview_empty_accepted_requests_list_placeholder">"No accepted requests found. Please chose requests from the list below."</string>
<string name="helper_request_overview_empty_open_requests_list_placeholder">"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."</string>
<string name="seeker_article_input_description">"To add a new article to the list provide its name, amount and unit."</string>
Expand Down