EarnApp #6

MainActivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.gms.ads.*
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
import com.neco_desarrollo.earnapp.adapters.CategoryAdapter
import com.neco_desarrollo.earnapp.adapters.ContentManager
import com.neco_desarrollo.earnapp.databinding.ActivityMainBinding
import kotlin.random.Random

class MainActivity : AppCompatActivity(), CategoryAdapter.Listener {
private lateinit var binding: ActivityMainBinding
private var adapter: CategoryAdapter? = null
private var interAd: InterstitialAd? = null
private var timer: CountDownTimer? = null
private var posM: Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initAdMob()
(application as AppMainState).showAdIfAvailable(this){}
initRcView()
binding.imageBg.setOnClickListener {
getResult()
}

}

private fun initRcView() = with(binding){
adapter = CategoryAdapter(this@MainActivity)
rcViewCat.layoutManager = LinearLayoutManager(
this@MainActivity,
LinearLayoutManager.HORIZONTAL,
false)
rcViewCat.adapter = adapter
adapter?.submitList(ContentManager.list)
}

override fun onResume() {
super.onResume()
binding.adView.resume()
loadInterAd()
}

override fun onPause() {
super.onPause()
binding.adView.pause()
}

override fun onDestroy() {
super.onDestroy()
binding.adView.destroy()
}
private fun getResult(){
var counter = 0
timer?.cancel()
timer = object : CountDownTimer(5000, 100){
override fun onTick(p0: Long) {
counter++
if(counter > 3)counter = 0
binding.imageBg.setImageResource(MainConst.imageList[counter])
}
override fun onFinish() {
getMessage()
}
}.start()
}

private fun initAdMob(){
MobileAds.initialize(this)
val adRequest = AdRequest.Builder().build()
binding.adView.loadAd(adRequest)
}

private fun loadInterAd(){
val adRequest = AdRequest.Builder().build()
InterstitialAd.load(this,
"ca-app-pub-3940256099942544/1033173712", adRequest,
object : InterstitialAdLoadCallback(){
override fun onAdFailedToLoad(p0: LoadAdError) {
interAd = null
}

override fun onAdLoaded(ad: InterstitialAd) {
interAd = ad
}
})
}

private fun showInterAd(){
if(interAd != null){
interAd?.fullScreenContentCallback =
object : FullScreenContentCallback(){
override fun onAdDismissedFullScreenContent() {
showContent()
interAd = null
loadInterAd()
}

override fun onAdFailedToShowFullScreenContent(p0: AdError) {
showContent()
interAd = null
loadInterAd()
}

override fun onAdShowedFullScreenContent() {
interAd = null
loadInterAd()
}
}

interAd?.show(this)
} else {
showContent()
}
}

private fun showContent(){
Toast.makeText(this, "Запуск контента", Toast.LENGTH_LONG).show()
}
private fun getMessage() = with(binding){
val currentArray = resources.getStringArray(MainConst.arrayList[posM])
val message = currentArray[Random.nextInt(currentArray.size)]
val messageList = message.split("|")
tvMessage.text = messageList[0]
tvName.text = messageList[1]
imageBg.setImageResource(MainConst.imageList[Random.nextInt(4)])
}

override fun onClick(pos: Int) {
posM = pos
getResult()
}
}

MainConst

object MainConst {
val imageList = listOf(
R.drawable.forest_bg,
R.drawable.sunset_bg,
R.drawable.sky_bg,
R.drawable.tree_bg,
)
val arrayList = listOf(R.array.philosophy_array)
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcViewCat"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/adView" />

<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="10dp"
app:cardCornerRadius="20dp"
app:cardElevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rcViewCat">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageBg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/forest_bg" />

<TextView
android:id="@+id/tvMessage"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#56000000"
android:gravity="center"
android:padding="40dp"
android:textColor="@color/white"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toTopOf="@+id/tvName"
app:layout_constraintEnd_toEndOf="@+id/imageBg"
app:layout_constraintStart_toStartOf="@+id/imageBg"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tvName"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#BAFF3636"
android:gravity="center"
android:padding="10dp"
android:textColor="@color/white"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline8" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.9" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

f_array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="philosophy_array">
<item>Добавте ваши фразы в массив|NECO</item>
</string-array>
</resources>

Добавить комментарий

Ваш адрес email не будет опубликован.