Skip to content

Instantly share code, notes, and snippets.

View douglasiacovelli's full-sized avatar

Douglas Iacovelli douglasiacovelli

  • @contratadome
  • Bertioga
View GitHub Profile
class TextInput @JvmOverloads constructor(context: Context, attrSet: AttributeSet? = null, defStyleAttr: Int = 0)
: FrameLayout(context, attrSet, defStyleAttr), FieldInput {
override var key: String = ""
private val textInput: TextInputEditText
init {
val view = inflate(context, R.layout.text_input, this)
textInput = view.findViewById(R.id.editText)
interface FormSection {
fun onUserSubmitSection()
}
class FormFragment : Fragment(), FormSection {
private val section by lazy { arguments!!.getParcelable<Section>(SECTION) }
private val fieldInputs = mutableListOf<FieldInput>()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.form_section_fragment, container, false)
return view
}
interface SectionSubmittedListener {
fun onSectionSubmittedWithValidAnswers(answers: List<Answer>)
}
class MainActivity : AppCompatActivity(), SectionSubmittedListener {
private val viewPager by lazy { findViewById<ViewPager>(R.id.viewPager) }
//Added code
private val nextButton by lazy { findViewById<Button>(R.id.nextButton) }
private val allAnswers = mutableListOf<Answer>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
interface WorkflowService {
@POST("/my_endpoint_to_receive_data")
fun submit(@Body data: FormBody): Single<MyResponse>
}
class WorkflowRepository(private val service: WorkflowService) {
override fun submit(answers: List<Answer>): Single<MyResponse> {
val formBody = FormBody.Builder()
answers.forEach {
formBody.add(it.key, it.value)
}
return service.submit(formBody.build())
}
}
{
"sections": [
{
"title": "What's your name",
"type": "form",
"fields": [
{
"type": "text",
"key": "first_name",
"hint": "Type your first name"
sealed class ResultWrapper<out T> {
data class Success<out T>(val value: T): ResultWrapper<T>()
data class GenericError(val code: Int? = null, val error: ErrorResponse? = null): ResultWrapper<Nothing>()
object NetworkError: ResultWrapper<Nothing>()
}
suspend fun <T> safeApiCall(dispatcher: CoroutineDispatcher, apiCall: suspend () -> T): ResultWrapper<T> {
return withContext(dispatcher) {
try {
ResultWrapper.Success(apiCall.invoke())
} catch (throwable: Throwable) {
when (throwable) {
is IOException -> ResultWrapper.NetworkError
is HttpException -> {
val code = throwable.code()
val errorResponse = convertErrorBody(throwable)