diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7270cf02..a31569ee 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -132,7 +132,7 @@ 0) { - val result = resources.openRawResource(rawPath) - .bufferedReader() - .readLines() - .joinToString("\n") - - withContext(Dispatchers.Main) { - textView.text = result - progressBar.visibility = View.GONE - } - } - } - } - } -} diff --git a/app/src/main/java/dev/lucasnlm/antimine/about/views/thirds/ThirdPartyAdapter.kt b/app/src/main/java/dev/lucasnlm/antimine/about/views/thirds/ThirdPartyAdapter.kt index 5a04a52a..ac1b27e3 100644 --- a/app/src/main/java/dev/lucasnlm/antimine/about/views/thirds/ThirdPartyAdapter.kt +++ b/app/src/main/java/dev/lucasnlm/antimine/about/views/thirds/ThirdPartyAdapter.kt @@ -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) } } diff --git a/app/src/main/java/dev/lucasnlm/antimine/text/TextActivity.kt b/app/src/main/java/dev/lucasnlm/antimine/text/TextActivity.kt new file mode 100644 index 00000000..fece9884 --- /dev/null +++ b/app/src/main/java/dev/lucasnlm/antimine/text/TextActivity.kt @@ -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) + } + } + } +} diff --git a/app/src/main/java/dev/lucasnlm/antimine/text/viewmodel/TextViewModel.kt b/app/src/main/java/dev/lucasnlm/antimine/text/viewmodel/TextViewModel.kt new file mode 100644 index 00000000..61e57f6c --- /dev/null +++ b/app/src/main/java/dev/lucasnlm/antimine/text/viewmodel/TextViewModel.kt @@ -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() + + suspend fun loadText(@RawRes rawFile: Int) { + val result = getApplication().resources.openRawResource(rawFile) + .bufferedReader() + .readLines() + .joinToString("\n") + + text.postValue(result) + } +}