Instantly share code, notes, and snippets.
Last active
September 14, 2022 10:20
-
Save kyodgorbek/84f77dfd49a21066339053d5c6118883 to your computer and use it in GitHub Desktop.
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
@Module | |
@InstallIn(SingletonComponent::class) | |
class ServerModule { | |
@Provides | |
@Singleton | |
fun provideRetrofit(): Retrofit { | |
return Retrofit.Builder() | |
.baseUrl(baseUrl) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
} | |
@Provides | |
@Singleton | |
fun provideApiService(retrofit: Retrofit): BackendApi = retrofit.create(BackendApi::class.java) | |
@Provides | |
@Singleton | |
fun getClient(sharedPref: SharedPref): OkHttpClient = OkHttpClient.Builder() | |
.connectTimeout( 60, TimeUnit.SECONDS) | |
.readTimeout(60, TimeUnit.SECONDS) | |
// .addInterceptor(ChuckInterceptor(context)) | |
.addInterceptor(HttpLoggingInterceptor().apply { | |
level = HttpLoggingInterceptor.Level.BODY | |
}) | |
.addInterceptor(Interceptor { chain -> | |
val builder = chain.request().newBuilder() | |
if (sharedPref.token != ""){ | |
builder.header("Authorization", "Bearer ${sharedPref.token}") | |
} | |
chain.proceed(builder.build()) | |
}) | |
.build() | |
} |
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
import android.app.Activity | |
import android.content.Context | |
import android.content.SharedPreferences | |
import android.util.Log | |
import android.widget.Toast | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.lifecycle.MutableLiveData | |
import com.chargeatfriends.android.model.Booking | |
import com.chargeatfriends.android.model.ChargePoint | |
import com.chargeatfriends.android.model.CustomerTokenEntity | |
import com.chargeatfriends.android.model.stripe.BackendApiFactory | |
import com.chargeatfriends.android.model.stripe.CafEphemeralKeyProvider | |
import com.chargeatfriends.android.model.stripe.Settings | |
import com.chargeatfriends.android.network.baseUrl | |
import com.google.android.gms.tasks.OnCompleteListener | |
import com.google.firebase.messaging.FirebaseMessaging | |
import com.stripe.android.* | |
import com.stripe.android.model.PaymentMethod | |
import dagger.Module | |
import dagger.hilt.android.qualifiers.ApplicationContext | |
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.withContext | |
import java.util.* | |
import javax.inject.Inject | |
class SharedPref @Inject constructor(@ApplicationContext val context: Context){ | |
var mySharedPref: SharedPreferences = context.getSharedPreferences("sharedPref", Context.MODE_PRIVATE) | |
private val backendApi = BackendApiFactory(baseUrl).create() | |
lateinit var FriendGuid: UUID | |
internal var loginToken = "" | |
var LdToken = MutableLiveData<String>() | |
var isAdhocBooking: Boolean = false | |
lateinit var pnsToken: String | |
var token: String | |
set(value) = mySharedPref.edit().putString("token",value).apply() | |
get() = mySharedPref.getString("token", "")!! | |
fun setPreferences(activity: Activity) { | |
mySharedPref = activity.getPreferences(Context.MODE_PRIVATE) | |
if (mySharedPref.contains("FriendGuid")) { | |
FriendGuid = UUID.fromString(mySharedPref.getString("FriendGuid", "")) | |
} | |
getMessageToken() | |
} | |
private fun setPreference(key: String, value: String) { | |
with(mySharedPref.edit()) { | |
putString(key, value) | |
apply() | |
} | |
} | |
private fun getPreferenceString(key: String): String { | |
return mySharedPref.getString(key, "") ?: "" | |
} | |
fun saveLanguage(key:String, value:String){ | |
val editor = mySharedPref.edit() | |
editor.putString(key, value) | |
editor.apply() | |
} | |
fun getLanguage(key: String, value: String): String? { | |
return mySharedPref.getString(key, value) | |
} | |
fun saveString(key: String, value: String) { | |
val editor = mySharedPref.edit() | |
editor.putString(key, value) | |
editor.apply() | |
} | |
fun getString(key: String, value: String): String? { | |
return mySharedPref.getString(key, value) | |
} | |
fun saveFirebaseToken(tokenKey: String, firebaseToken: String) { | |
mySharedPref.edit().putString(tokenKey, firebaseToken).apply() | |
} | |
fun getFirebaseToken(keyToken: String): String { | |
return mySharedPref.getString(keyToken, "")!! | |
} | |
fun saveBoolean(key: String, value: Boolean) { | |
val editor = mySharedPref.edit() | |
editor.putBoolean(key, value) | |
editor.apply() | |
} | |
fun getBoolean(key: String): Boolean { | |
return mySharedPref.getBoolean(key, false) | |
} | |
fun setFriend(Guid: UUID) { | |
FriendGuid = Guid | |
setPreference("FriendGuid", Guid.toString()) | |
} | |
fun getFriend(key: String, data: String): String { | |
return mySharedPref.getString(key, data)!! | |
} | |
fun setStoreValue(key: String, name: String) { | |
setPreference(key, name) | |
} | |
fun getStoredValue(key: String): String{ | |
return mySharedPref.getString(key, "") ?: "" | |
} | |
// | |
// fun setMessageToken(key: String, messageToken: String) { | |
// mySharedPref.edit().putString(key, messageToken).apply() | |
// } | |
// fun getMessageToken(key: String, data: String): String { | |
// return mySharedPref.getString(key, data)!! | |
// } | |
fun setMessageToken(token: String?) { | |
pnsToken = token as String | |
setPreference("PnsToken", pnsToken) | |
if (getLoginToken() != "") { | |
sendRegistrationToServer() | |
} | |
} | |
fun getMessageToken(): String { | |
if (!this::pnsToken.isInitialized) { | |
pnsToken = getPreferenceString("PnsToken") as String | |
} | |
//if (pnsToken == "") { | |
getFirebaseTokens() | |
//} | |
return pnsToken | |
} | |
fun setActivePaymentMethod(pm: PaymentMethod) { | |
paymentMethod = pm | |
setPreference("pmId", pm.id ?: "") | |
} | |
fun setLoginToken(token: String) { | |
loginToken = token | |
LdToken.value = token | |
setPreference("Token", token) | |
if ((token != "") and (getMessageToken() != "")) { | |
sendRegistrationToServer() | |
} | |
} | |
fun getLoginToken(): String { | |
if (loginToken == "") { | |
loginToken = getPreferenceString("Token") | |
} | |
return loginToken | |
} | |
fun sendRegistrationToServer(token: String?) { | |
setMessageToken(token as String) | |
if (this::pnsToken.isInitialized and (loginToken != "")) { | |
sendRegistrationToServer() | |
} | |
} | |
//sendMessageToken | |
fun sendRegistrationToServer() { | |
// TODO: Implement this method to send token to your app server. | |
CoroutineScope(Dispatchers.IO).launch { | |
val response = kotlin.runCatching { | |
backendApi.setNotificationToken( | |
getLoginToken(), | |
pnsToken | |
) | |
} | |
withContext(Dispatchers.Main) { | |
response.fold( | |
onSuccess = { | |
Log.d("submitRegistration-Success", it.toString()) | |
}, | |
onFailure = { | |
Log.d("submitRegistration-Fail", it.message.orEmpty()) | |
} | |
) | |
} | |
} | |
} | |
//GetFirebaseToken | |
fun getFirebaseTokens() { | |
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task -> | |
if (!task.isSuccessful) { | |
Log.w("@@@@@@@", "Fetching FCM registration token failed", task.exception) | |
Log.d("TAG","FCM failed: ${(task.exception?.message)}") | |
} | |
// Get new FCM registration token | |
//val token = task.result | |
// setMessageToken(task.result) | |
// pnsToken = token.toString() | |
// sendRegistrationToServer() | |
// Log and toast | |
//showToast("registered FCM Token:"+pnsToken.substring(0,8)) | |
Log.d("TAG", "registered FCM Token: " + pnsToken) | |
}) | |
} | |
//InitCustomerToken | |
private lateinit var ApplicationContext: Context | |
lateinit var ChargePointGuid: UUID | |
var QrContent: String? = "" | |
val TAG = "appCommon: " | |
@Volatile | |
var Bookings = MutableLiveData<List<Booking>>() | |
var CpBookings = MutableLiveData<List<Booking>>() | |
val MWSt = 0.19 | |
lateinit var CustomerToken: CustomerTokenEntity | |
lateinit var CurrentChargePoint: ChargePoint | |
lateinit var CurrentBooking: Booking | |
var defaultChargeTime: Long = 4 * 3600 | |
//<editor-fold desc="verification"> | |
lateinit var RemoteBookingGuid: UUID | |
lateinit var RemoteBooking: Booking | |
var RemoteBookingState: Int = 0 | |
var RemotBookkingIsCpo: Boolean = true | |
//</editor-fold> | |
lateinit var paymentMethod: PaymentMethod | |
lateinit var paymentSession: PaymentSession | |
lateinit var customerSession: CustomerSession | |
// private val backendApi = BackendApiFactory(baseUrl).create() | |
private var MlChargePoints = MutableLiveData<List<ChargePoint>>() | |
fun isChargePointInitialized(): Boolean{ | |
return this::CurrentChargePoint.isInitialized; | |
} | |
fun GetActiveChargePoint(): ChargePoint? { | |
if (this::CurrentChargePoint.isInitialized) { | |
return CurrentChargePoint | |
} | |
return null | |
} | |
fun initCustomerSession(activity: AppCompatActivity) { | |
if ((getLoginToken() != "") and !this::customerSession.isInitialized and !initializingCustomerSession) { | |
CustomerSession.initCustomerSession(activity, CafEphemeralKeyProvider(activity)) | |
// val paymentMethodType: PaymentMethod.Type = | |
CustomerSession.getInstance().getPaymentMethods( | |
PaymentMethod.Type.Card, | |
PaymentMethodsRetrievalListener() | |
) | |
if (!this::paymentSession.isInitialized) { | |
paymentSession = PaymentSession( | |
activity, | |
PaymentSessionConfig.Builder() | |
.setShippingInfoRequired(false) | |
.setShippingMethodsRequired(false) | |
.build() | |
) | |
PaymentConfiguration.init(activity, (Settings(activity) as Settings).publishableKey) | |
setupPaymentSession() | |
} | |
} | |
initializingCustomerSession = false | |
} | |
fun getChargePoints(): List<ChargePoint> { | |
if (MlChargePoints.value == null) { | |
MlChargePoints.value = mutableListOf<ChargePoint>() | |
} | |
return MlChargePoints.value as List<ChargePoint> | |
} | |
fun setChargePoints(chargePoints: List<ChargePoint>?) { | |
MlChargePoints.value = chargePoints!! | |
} | |
fun setBooking(booking: Booking) { | |
CurrentBooking = booking | |
val cp = MlChargePoints.value?.find { cp -> cp.Guid == booking.CpGuid } | |
if (cp != null) { | |
CurrentChargePoint = cp | |
} | |
} | |
fun isBookingSet(): Boolean { | |
return this::CurrentBooking.isInitialized | |
} | |
fun isPaymentMethodInitialized(): Boolean { | |
return this::paymentMethod.isInitialized | |
} | |
fun isPaymentSessionInitialized(): Boolean { | |
return this::paymentSession.isInitialized | |
} | |
fun setupPaymentSession() { | |
if (this::customerSession.isInitialized) { | |
} | |
if (!this::paymentSession.isInitialized) { | |
} | |
paymentSession.init( | |
object : PaymentSession.PaymentSessionListener { | |
override fun onCommunicatingStateChanged(isCommunicating: Boolean) { | |
// update UI, such as hiding or showing a progress bar | |
} | |
override fun onError(errorCode: Int, errorMessage: String) { | |
// handle error | |
} | |
override fun onPaymentSessionDataChanged(data: PaymentSessionData) { | |
Log.d("appCommon - onPaymentSessionDataChanged ", data.toString() + " "); | |
// paymentMethod = data.paymentMethod as PaymentMethod | |
// use paymentMethod | |
} | |
} | |
) | |
//startPaymentFlowButton.isEnabled = true | |
} | |
private var initializingCustomerSession = false | |
private fun showToast(message: String){ | |
var toast = message | |
if(ApplicationContext!=null){ | |
Toast.makeText( | |
ApplicationContext, | |
toast, | |
Toast.LENGTH_SHORT | |
).show() | |
} | |
} | |
fun initKey(applicationContext: Context) { | |
ApplicationContext = applicationContext | |
if (LdToken.value != null) { | |
loginToken = LdToken.value.toString() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment