Simply TextActivity

This commit is contained in:
Lucas Lima 2020-06-28 12:12:56 -03:00
parent 229b30631c
commit b388f37497
No known key found for this signature in database
GPG key ID: 0259A3F43EC1027A
7 changed files with 85 additions and 60 deletions

View file

@ -132,7 +132,7 @@
</activity>
<activity
android:name="dev.lucasnlm.antimine.about.TextActivity"
android:name="dev.lucasnlm.antimine.text.TextActivity"
android:theme="@style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"

View file

@ -8,13 +8,12 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.about.views.AboutInfoFragment
import dev.lucasnlm.antimine.about.models.AboutEvent
import dev.lucasnlm.antimine.about.views.translators.TranslatorsFragment
import dev.lucasnlm.antimine.about.views.thirds.ThirdPartiesFragment
import dev.lucasnlm.antimine.about.viewmodel.AboutViewModel
import dev.lucasnlm.antimine.about.views.AboutInfoFragment
import dev.lucasnlm.antimine.about.views.thirds.ThirdPartiesFragment
import dev.lucasnlm.antimine.about.views.translators.TranslatorsFragment
class AboutActivity : AppCompatActivity(R.layout.activity_empty) {
private val aboutViewModel: AboutViewModel by viewModels()

View file

@ -1,6 +0,0 @@
package dev.lucasnlm.antimine.about
object Constants {
const val TEXT_TITLE = "third_title"
const val TEXT_PATH = "third_path"
}

View file

@ -1,41 +0,0 @@
package dev.lucasnlm.antimine.about
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import dev.lucasnlm.antimine.R
import kotlinx.android.synthetic.main.activity_text.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class TextActivity : AppCompatActivity(R.layout.activity_text) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = intent.getStringExtra(Constants.TEXT_TITLE)
GlobalScope.launch {
withContext(Dispatchers.Main) {
progressBar.visibility = View.VISIBLE
}
withContext(Dispatchers.IO) {
val rawPath = intent.getIntExtra(Constants.TEXT_PATH, -1)
if (rawPath > 0) {
val result = resources.openRawResource(rawPath)
.bufferedReader()
.readLines()
.joinToString("\n")
withContext(Dispatchers.Main) {
textView.text = result
progressBar.visibility = View.GONE
}
}
}
}
}
}

View file

@ -1,13 +1,11 @@
package dev.lucasnlm.antimine.about.views.thirds
import android.content.Intent
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.about.Constants
import dev.lucasnlm.antimine.about.TextActivity
import dev.lucasnlm.antimine.text.TextActivity
import dev.lucasnlm.antimine.about.models.ThirdParty
class ThirdPartyAdapter(
@ -28,11 +26,7 @@ class ThirdPartyAdapter(
holder.apply {
title.text = thirdParty.name
itemView.setOnClickListener { view ->
val intent = Intent(view.context, TextActivity::class.java).apply {
putExtra(Constants.TEXT_TITLE, thirdParty.name)
putExtra(Constants.TEXT_PATH, thirdParty.license)
}
val intent = TextActivity.getIntent(view.context, thirdParty.name, thirdParty.license)
view.context.startActivity(intent)
}
}

View file

@ -0,0 +1,57 @@
package dev.lucasnlm.antimine.text
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import androidx.activity.viewModels
import androidx.annotation.RawRes
import androidx.lifecycle.Observer
import dev.lucasnlm.antimine.R
import dev.lucasnlm.antimine.text.viewmodel.TextViewModel
import kotlinx.android.synthetic.main.activity_text.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class TextActivity : AppCompatActivity(R.layout.activity_text) {
private val viewModel: TextViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = intent.getStringExtra(TEXT_TITLE)
viewModel.text.observe(
this,
Observer { loadedText ->
textView.text = loadedText
progressBar.visibility = View.GONE
}
)
GlobalScope.launch {
withContext(Dispatchers.Main) {
progressBar.visibility = View.VISIBLE
}
withContext(Dispatchers.IO) {
viewModel.loadText(intent.getIntExtra(TEXT_PATH, -1))
}
}
}
companion object {
private const val TEXT_TITLE = "third_title"
private const val TEXT_PATH = "third_path"
fun getIntent(context: Context, title: String, @RawRes textRes: Int): Intent {
return Intent(context, TextActivity::class.java).apply {
putExtra(TEXT_TITLE, title)
putExtra(TEXT_PATH, textRes)
}
}
}
}

View file

@ -0,0 +1,22 @@
package dev.lucasnlm.antimine.text.viewmodel
import android.app.Application
import androidx.annotation.RawRes
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
class TextViewModel(
application: Application
) : AndroidViewModel(application) {
val text = MutableLiveData<String>()
suspend fun loadText(@RawRes rawFile: Int) {
val result = getApplication<Application>().resources.openRawResource(rawFile)
.bufferedReader()
.readLines()
.joinToString("\n")
text.postValue(result)
}
}