-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement chargeprice feedback dialog
- Loading branch information
1 parent
a6117d3
commit a22b347
Showing
11 changed files
with
563 additions
and
7 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
app/src/main/java/net/vonforst/evmap/fragment/ChargepriceFeedbackFragment.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,74 @@ | ||
package net.vonforst.evmap.fragment | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ArrayAdapter | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.navigation.fragment.findNavController | ||
import androidx.navigation.fragment.navArgs | ||
import androidx.navigation.ui.setupWithNavController | ||
import net.vonforst.evmap.MapsActivity | ||
import net.vonforst.evmap.R | ||
import net.vonforst.evmap.databinding.FragmentChargepriceFeedbackBinding | ||
import net.vonforst.evmap.viewmodel.ChargepriceFeedbackViewModel | ||
import net.vonforst.evmap.viewmodel.viewModelFactory | ||
|
||
class ChargepriceFeedbackFragment : Fragment() { | ||
|
||
private lateinit var binding: FragmentChargepriceFeedbackBinding | ||
private val vm: ChargepriceFeedbackViewModel by viewModels(factoryProducer = { | ||
viewModelFactory { | ||
ChargepriceFeedbackViewModel( | ||
requireActivity().application, | ||
getString(R.string.chargeprice_key), | ||
getString(R.string.chargeprice_api_url) | ||
) | ||
} | ||
}) | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val fragmentArgs: ChargepriceFeedbackFragmentArgs by navArgs() | ||
vm.feedbackType.value = fragmentArgs.feedbackType | ||
vm.charger.value = fragmentArgs.charger | ||
vm.vehicle.value = fragmentArgs.vehicle | ||
vm.chargePrices.value = fragmentArgs.chargePrices?.toList() | ||
vm.batteryRange.value = fragmentArgs.batteryRange?.toList() | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
binding = DataBindingUtil.inflate( | ||
inflater, | ||
R.layout.fragment_chargeprice_feedback, container, false | ||
) | ||
binding.lifecycleOwner = this | ||
binding.vm = vm | ||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
binding.toolbar.setupWithNavController( | ||
findNavController(), | ||
(requireActivity() as MapsActivity).appBarConfiguration | ||
) | ||
binding.tariffSpinner.setAdapter( | ||
ArrayAdapter<String>( | ||
requireContext(), | ||
R.layout.item_simple_multiline, | ||
R.id.text, | ||
mutableListOf() | ||
) | ||
) | ||
} | ||
} |
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
129 changes: 129 additions & 0 deletions
129
app/src/main/java/net/vonforst/evmap/viewmodel/ChargepriceFeedbackViewModel.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,129 @@ | ||
package net.vonforst.evmap.viewmodel | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.* | ||
import kotlinx.coroutines.launch | ||
import net.vonforst.evmap.R | ||
import net.vonforst.evmap.api.chargeprice.* | ||
import net.vonforst.evmap.model.ChargeLocation | ||
import net.vonforst.evmap.model.Chargepoint | ||
import net.vonforst.evmap.storage.PreferenceDataSource | ||
import net.vonforst.evmap.ui.currency | ||
import java.io.IOException | ||
|
||
enum class ChargepriceFeedbackType { | ||
MISSING_PRICE, WRONG_PRICE, MISSING_VEHICLE | ||
} | ||
|
||
class ChargepriceFeedbackViewModel( | ||
application: Application, | ||
chargepriceApiKey: String, | ||
chargepriceApiUrl: String | ||
) : | ||
AndroidViewModel(application) { | ||
private var api = ChargepriceApi.create(chargepriceApiKey, chargepriceApiUrl) | ||
private var prefs = PreferenceDataSource(application) | ||
|
||
// data supplied through fragment args | ||
val feedbackType = MutableLiveData<ChargepriceFeedbackType>() | ||
val charger = MutableLiveData<ChargeLocation>() | ||
val chargepoint = MutableLiveData<Chargepoint>() | ||
val vehicle = MutableLiveData<ChargepriceCar>() | ||
val chargePrices = MutableLiveData<List<ChargePrice>>() | ||
val batteryRange = MutableLiveData<List<Float>>() | ||
|
||
// data input by user | ||
val tariff = MutableLiveData<String>() | ||
val price = MutableLiveData<String>() | ||
val notes = MutableLiveData<String>() | ||
val email = MutableLiveData<String>() | ||
|
||
val loading = MutableLiveData<Boolean>().apply { value = false } | ||
|
||
val chargePricesStrings = chargePrices.map { | ||
it.map { | ||
val name = if (!it.tariffName.lowercase().startsWith(it.provider.lowercase())) { | ||
"${it.provider} ${it.tariffName}" | ||
} else it.tariffName | ||
val price = application.getString( | ||
R.string.charge_price_format, | ||
it.chargepointPrices[0].price, | ||
currency(it.currency) | ||
) | ||
"$name: $price" | ||
}.toList() | ||
} | ||
|
||
private val feedback = MediatorLiveData<ChargepriceUserFeedback>().apply { | ||
listOf( | ||
feedbackType, | ||
charger, | ||
chargepoint, | ||
vehicle, | ||
chargePrices, | ||
tariff, | ||
price, | ||
notes, | ||
).forEach { | ||
addSource(it) { | ||
try { | ||
value = when (feedbackType.value!!) { | ||
ChargepriceFeedbackType.MISSING_PRICE -> { | ||
ChargepriceMissingPriceFeedback( | ||
tariff.value ?: "", | ||
charger.value?.network?.take(200) ?: "", | ||
price.value ?: "", | ||
charger.value?.let { ChargepriceApi.getPoiUrl(it) } ?: "", | ||
notes.value ?: "", | ||
email.value ?: "", | ||
getChargepriceContext(), | ||
ChargepriceApi.getChargepriceLanguage() | ||
) | ||
} | ||
ChargepriceFeedbackType.WRONG_PRICE -> { | ||
ChargepriceWrongPriceFeedback( | ||
"", // TODO: dropdown value | ||
charger.value?.network?.take(200) ?: "", | ||
"", // TODO: dropdown value | ||
price.value ?: "", | ||
charger.value?.let { ChargepriceApi.getPoiUrl(it) } ?: "", | ||
notes.value ?: "", | ||
email.value ?: "", | ||
getChargepriceContext(), | ||
ChargepriceApi.getChargepriceLanguage() | ||
) | ||
} | ||
ChargepriceFeedbackType.MISSING_VEHICLE -> { | ||
TODO() | ||
} | ||
} | ||
} catch (e: IllegalArgumentException) { | ||
value = null | ||
} | ||
} | ||
} | ||
} | ||
|
||
val formValid = feedback.map { it != null } | ||
|
||
fun sendFeedback() { | ||
val feedback = feedback.value ?: return | ||
viewModelScope.launch { | ||
loading.value = true | ||
try { | ||
api.userFeedback(feedback) | ||
} catch (e: IOException) { | ||
|
||
} | ||
loading.value = false | ||
} | ||
} | ||
|
||
private fun getChargepriceContext(): String { | ||
val result = StringBuilder() | ||
vehicle.value?.let { result.append("Vehicle: ${it.brand} ${it.name}\n") } | ||
batteryRange.value?.let { result.append("Battery SOC: ${it[0]} to ${it[1]}\n") } | ||
return result.toString() | ||
} | ||
} |
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.