Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.merkury.loans.views.activities
- import android.content.Intent
- import android.os.Build
- import android.os.Bundle
- import android.util.Log
- import android.view.View
- import android.widget.ImageView
- import android.widget.LinearLayout
- import android.widget.TextView
- import androidx.annotation.RequiresApi
- import androidx.appcompat.app.AppCompatActivity
- import com.beust.klaxon.json
- import com.google.gson.Gson
- import com.merkury.loans.R
- import com.merkury.loans.models.Language
- import com.merkury.loans.parser.JsonParser
- import com.merkury.loans.utils.extensions.*
- import io.reactivex.android.schedulers.AndroidSchedulers
- import io.reactivex.android.*
- import io.reactivex.disposables.Disposable
- import io.reactivex.rxkotlin.zipWith
- import io.reactivex.schedulers.Schedulers
- import java.net.URL
- import java.util.*
- import kotlin.concurrent.thread
- import kotlin.random.Random
- @Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
- class MainActivity : AppCompatActivity() {
- @RequiresApi(Build.VERSION_CODES.N)
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val lPrefs = getSharedPreferences("lang", 0);
- if (lPrefs.getString("language", "") == "") {
- LANG = Locale.getDefault().displayLanguage
- LANG = convertLang[LANG].toString()
- } else {
- LANG = lPrefs.getString("lang", "").toString()
- }
- if (lPrefs.getString("country", "") == "") {
- loadCountry()
- } else {
- COUNTRY = lPrefs.getString("country", "").toString()
- }
- Log.d("country", COUNTRY)
- Log.d("lang", LANG)
- if (LANG != "" && COUNTRY != "") {
- if (convertCountry[COUNTRY].toString() == LANG) {
- loadLanguageAndData()
- val changePage = Intent(this, StartActivity::class.java)
- startActivity(changePage)
- } else {
- val countriesLayout: LinearLayout = findViewById(R.id.countriesLayout)
- val text: TextView = findViewById(R.id.choouse)
- countriesLayout.visibility = View.VISIBLE
- text.visibility = View.VISIBLE
- val ru: ImageView = findViewById(R.id.countryru)
- val pl: ImageView = findViewById(R.id.countrypl)
- ru.setOnClickListener {
- COUNTRY = "RU"
- LANG = "ru"
- val lPrefs = getSharedPreferences("lang", 0);
- lPrefs.edit().putString("country", "RU").apply()
- lPrefs.edit().putBoolean("lang", false).apply()
- lPrefs.edit().putString("language", "ru").apply()
- loadLanguageAndData()
- val changePage = Intent(this, StartActivity::class.java)
- startActivity(changePage)
- }
- pl.setOnClickListener {
- COUNTRY = "PL"
- LANG = "pl"
- val lPrefs = getSharedPreferences("lang", 0);
- lPrefs.edit().putString("country", "PL").apply()
- lPrefs.edit().putString("lang", "pl").apply()
- lPrefs.edit().putBoolean("lang", false).apply()
- loadLanguageAndData()
- val changePage = Intent(this, StartActivity::class.java)
- startActivity(changePage)
- }
- }
- }
- }
- private fun loadLanguageAndData() {
- loadLanguageAndCountry()
- //start loading data
- loadData()
- }
- private fun loadCountry() {
- val lPrefs = getSharedPreferences("lang", 0);
- if (lPrefs.getString("country", "") != "") {
- return
- }
- val ip = CreateRequest("https://api.ipify.org/").subscribeOn(
- Schedulers.io()
- ).flatMap {
- CreateRequest("http://ip-api.com/json/$it?fields=countryCode")
- }.subscribe({
- COUNTRY = it.substring(16, 18)
- Log.d("country", COUNTRY)
- }, { Log.e("errorCountry", "", it) })
- }
- private fun loadLanguageAndCountry() {
- val lPrefs = getSharedPreferences("lang", 0);
- if (lPrefs.getBoolean("lang", false)) {
- Log.d("lang", LANG)
- val url = "$LANGUAGE_URL$LANG.json$LANGUAGE_KEY"
- var data: Language = Language()
- val o = CreateRequest(url).map { Gson().fromJson(it, Language::class.java) }
- .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
- val response = o.subscribe({
- data = it
- }, {
- Log.e("errorLanguageMap", "", it)
- })
- val edittor = lPrefs.edit()
- //save data
- Log.d("datalang", data.toString())
- edittor.putString("loans", data.loans)
- edittor.putString("credits", data.credits)
- edittor.putString("chat", data.chat)
- edittor.putString("calculator", data.calculator)
- edittor.putString("help", data.help)
- edittor.putString("apply", data.apply)
- Log.d("datalang", data.apply)
- edittor.putString("conditions", data.conditions)
- edittor.putString("sum", data.sum)
- edittor.putString("rate", data.rate)
- edittor.putString("credit history", data.credit_history)
- edittor.putString("from", data.from)
- edittor.putString("to", data.to)
- edittor.putString("points", data.points)
- edittor.putString("time", data.time)
- edittor.putString("interest rate", data.interest_rate)
- edittor.putString("pay", data.pay)
- edittor.putString("overpayment", data.overpayment)
- edittor.putString("day", data.day)
- edittor.putString("currency", data.currency)
- edittor.putString("text1", data.text1)
- edittor.putString("text2", data.text2)
- edittor.putString("text3", data.text3)
- edittor.putString("text4", data.text4)
- edittor.putString("text5", data.text5)
- edittor.putString("text6", data.text6)
- edittor.putString("text7", data.text7)
- edittor.putBoolean("lang", true)
- edittor.apply()
- }
- }
- private fun loadData() {
- val tag = OFFERS_KEY
- var loansOffers = ""
- var creditsOffers = ""
- val mPrefs = getSharedPreferences(tag, 0)
- val editor = mPrefs.edit()
- //get data from firebase
- editor.putInt("sum_max", 0);
- editor.putInt("sum_min", 1000000000);
- while (true) {
- try {
- var jsonData: String = ""
- val o = CreateRequest(URL_FOR_FB_DATA + LANG + KEY_FOR_FB_DATA).map { it }
- .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
- val response = o.subscribe({
- jsonData = it
- }, {
- Log.e("errorLanguageMap", "", it)
- })
- Log.d("dataurl", URL_FOR_FB_DATA + LANG + KEY_FOR_FB_DATA)
- val data = JsonParser.parse(jsonData)
- //save data
- var sumMax: Int = 0
- var sumMin: Int = 100000000
- for (offer in data) {
- when (offer.type) {
- "bank" -> creditsOffers += offer.id + ","
- "mfo" -> loansOffers += offer.id + ","
- "service" -> loansOffers += offer.id + ","
- }
- thread {
- val b = getBitmapFromURL(offer.logo)
- if (b != null) {
- saveFile(this, b, offer.id)
- }
- }
- val sum_max: Int = offer.sumTo.filter { it.isDigit() }.toInt()
- Log.d("summa", sum_max.toString() + " " + offer.sumTo)
- val sum_min: Int = offer.sumFrom.filter { it.isDigit() }.toInt()
- if (sumMax < sum_max)
- sumMax = sum_max
- if (sumMin > sum_min)
- sumMin = sum_min
- editor.putString(offer.id + "_name", offer.name)
- editor.putString(offer.id + "_type", offer.type)
- editor.putString(offer.id + "_cpa", offer.cpa)
- editor.putString(offer.id + "_logo", offer.logo)
- editor.putString(offer.id + "_goals", offer.goals)
- editor.putString(offer.id + "_sum_from", offer.sumFrom)
- editor.putString(offer.id + "_sum_to", offer.sumTo)
- editor.putString(offer.id + "_percent_from", offer.percentFrom)
- editor.putString(offer.id + "_percent_to", offer.percentTo)
- editor.putString(offer.id + "_age_from", offer.ageFrom)
- editor.putString(offer.id + "_age_to", offer.ageTo)
- editor.putString(offer.id + "_ki_from", offer.kiFrom)
- editor.putString(offer.id + "_rate", offer.rate)
- editor.putString(offer.id + "_sale", offer.sale)
- editor.putString(offer.id + "_link", offer.link)
- try {
- editor.putBoolean(
- offer.id + "_in_app",
- Math.abs(Random.nextInt()) % 100 < offer.inApp.toInt()
- )
- } catch (e: NumberFormatException) {
- }
- }
- editor.putInt("sum_min", sumMin)
- editor.putInt("sum_max", sumMax)
- editor.putString(LOANS_KEY, loansOffers)
- editor.putString(CREDITS_KEY, creditsOffers)
- editor.apply()
- break
- } catch (e: Exception) {
- e.printStackTrace()
- Thread.sleep(100)
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment