MainViewModel.kt (2281B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2020 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.merchantpos 18 19 import android.app.Application 20 import androidx.lifecycle.AndroidViewModel 21 import androidx.lifecycle.viewModelScope 22 import io.ktor.client.statement.* 23 import io.ktor.http.HttpStatusCode 24 import net.taler.common.getDefaultHttpClient 25 import net.taler.merchantlib.MerchantApi 26 import net.taler.merchantpos.config.ConfigManager 27 import net.taler.merchantpos.history.HistoryManager 28 import net.taler.merchantpos.order.OrderManager 29 import net.taler.merchantpos.payment.PaymentManager 30 import net.taler.merchantpos.refund.RefundManager 31 32 class MainViewModel(app: Application) : AndroidViewModel(app) { 33 34 private val httpClient = getDefaultHttpClient(followRedirect = true) 35 private val api = MerchantApi(httpClient) 36 37 val orderManager = OrderManager(app) 38 val configManager = ConfigManager(app, viewModelScope, httpClient).apply { 39 addConfigurationReceiver(orderManager) 40 } 41 val paymentManager = PaymentManager(app, configManager, viewModelScope, api) 42 val historyManager = HistoryManager(configManager, viewModelScope, api) 43 val refundManager = RefundManager(configManager, viewModelScope, api) 44 45 init { 46 httpClient.responsePipeline.intercept(HttpResponsePipeline.Transform) { (info, body) -> 47 if (context.response.status == HttpStatusCode.Unauthorized && configManager.merchantConfig != null) { 48 configManager.forgetPassword() 49 configManager.notifySessionExpired() 50 } 51 proceedWith(subject) 52 } 53 } 54 55 override fun onCleared() { 56 httpClient.close() 57 } 58 59 }