Created
May 7, 2022 16:55
-
-
Save ultramilk155/62933727cfa55d45509d2a8ce4505f34 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
package com.rr.elearning.presentation.contain | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.os.Handler | |
import android.os.Looper | |
import androidx.viewpager.widget.ViewPager | |
import com.google.firebase.database.* | |
import com.google.gson.Gson | |
import com.rr.elearning.adapter.PagesAdapter | |
import com.rr.elearning.databinding.ActivityContentBinding | |
import com.rr.elearning.model.Content | |
import com.rr.elearning.model.Material | |
import com.rr.elearning.model.Page | |
import com.rr.elearning.presentation.contain.ContentActivity.Companion.EXTRA_POSITION | |
import com.rr.elearning.presentation.main.MainActivity | |
import com.rr.elearning.repository.Repository | |
import com.rr.elearning.utils.* | |
import org.jetbrains.anko.startActivity | |
import org.jetbrains.anko.toast | |
import java.text.ParsePosition | |
class ContentActivity : AppCompatActivity() { | |
companion object { | |
const val EXTRA_MATERIAL = "extra_material" | |
const val EXTRA_POSITION = "extra_position" | |
} | |
private lateinit var contentBinding: ActivityContentBinding | |
private lateinit var pagesAdapter: PagesAdapter | |
private lateinit var contentDatabase: DatabaseReference | |
private var currentPosition = 0 | |
private var materialPosition = 0 | |
private val listenerContent = object : ValueEventListener { | |
override fun onDataChange(snapshot: DataSnapshot) { | |
hideLoading() | |
if (snapshot.value != null) { | |
showData() | |
val json = Gson().toJson(snapshot.value) | |
val content = Gson().fromJson(json, Content::class.java) | |
pagesAdapter.pages = content?.pages as MutableList<Page> | |
} else { | |
showEmptyData() | |
} | |
} | |
override fun onCancelled(error: DatabaseError) { | |
hideLoading() | |
showDialogError(this@ContentActivity, error.message) | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
contentBinding = ActivityContentBinding.inflate(layoutInflater) | |
setContentView(contentBinding.root) | |
//init | |
pagesAdapter= PagesAdapter(this) | |
contentDatabase = FirebaseDatabase.getInstance().getReference("contents") | |
getDataIntent() | |
onAction() | |
viewPagerCurrentPosition() | |
} | |
private fun viewPagerCurrentPosition() { | |
contentBinding.vpContent.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{ | |
override fun onPageScrolled( | |
position: Int, | |
positionOffset: Float, | |
positionOffsetPixels: Int | |
) { | |
val totalIndex = pagesAdapter.count | |
currentPosition = position | |
val textIndex = "${currentPosition + 1}/ $totalIndex" | |
contentBinding.tvIndexContent.text=textIndex | |
if (currentPosition==0){ | |
contentBinding.btnPrevContent.invisible() | |
contentBinding.btnPrevContent.disabled() | |
}else{ | |
contentBinding.btnPrevContent.visible() | |
contentBinding.btnPrevContent.enabled() | |
} | |
} | |
override fun onPageSelected(position: Int) { | |
} | |
override fun onPageScrollStateChanged(state: Int) { | |
} | |
}) | |
} | |
private fun getDataIntent() { | |
if (intent !=null) { | |
materialPosition = intent.getIntExtra(EXTRA_POSITION, 0) | |
val material = intent.getParcelableExtra<Material>(EXTRA_MATERIAL) | |
contentBinding.tvTittleContent.text = material?.titleMaterial | |
material?.let { getDataContent(material) } | |
} | |
} | |
private fun getDataContent(material: Material) { | |
showLoading() | |
contentDatabase | |
.child(material.idMaterial.toString()) | |
.addValueEventListener(listenerContent) | |
contentBinding.vpContent.adapter = pagesAdapter | |
contentBinding.vpContent.setPagingEnabled(false) | |
//Init untuk tampilan awal index | |
val textIndex = "${currentPosition + 1} / ${pagesAdapter.count}" | |
contentBinding.tvIndexContent.text = textIndex | |
} | |
private fun showLoading() { | |
contentBinding.swipeContent.isRefreshing = true | |
} | |
private fun hideLoading() { | |
contentBinding.swipeContent.isRefreshing = false | |
} | |
private fun onAction() { | |
contentBinding.apply { | |
btnCloseContent.setOnClickListener { | |
finish() | |
} | |
btnNextContent.setOnClickListener { | |
if (currentPosition < pagesAdapter.count -1){ | |
contentBinding.vpContent.currentItem+=1 | |
}else{ | |
startActivity<MainActivity>( | |
MainActivity.EXTRA_POSITION to materialPosition +1 | |
) | |
finish() | |
} | |
} | |
btnPrevContent.setOnClickListener { | |
contentBinding.vpContent.currentItem-=1 | |
} | |
swipeContent.setOnRefreshListener { | |
getDataIntent() | |
} | |
} | |
} | |
private fun showEmptyData() { | |
contentBinding.apply { | |
ivEmptyDataContent.visible() | |
vpContent.gone() | |
} | |
} | |
private fun showData() { | |
contentBinding.apply { | |
ivEmptyDataContent.gone() | |
vpContent.visible() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment