Skip to content

Commit

Permalink
Fix part of #1633: Removed textview reference from RevisionCardViewmo…
Browse files Browse the repository at this point in the history
…del (#1981)

* Removed textview from RevisionCardViewmodel

- Removed textview reference from RevisionCardViewModel
- Removed explanationLiveData value from RevisionCardViewModel as the
  function that is used to get the charsequence was using view, now the
  code for the same is shifted to RevisionCardFragmentPresenter. So now
  RevisionCardViewModel expose RevisionCardLiveData and presenter
  observe it and then uses the HTML factory to get the char sequence and
  then update the textview
- Removed the usage of explanation livedata from
  revision_card_fragment.xml

* renamed setSubtopicIdAndBinding function and removed extra space & simplified view.text assignment

* Used named arguments 'imageCenterAlign' to have compiler verification

Co-authored-by: Ben Henning <[email protected]>

Co-authored-by: Ben Henning <[email protected]>
  • Loading branch information
alokbharti and BenHenning authored Oct 19, 2020
1 parent 19faa4f commit 1f8a133
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import org.oppia.android.app.fragment.FragmentScope
import org.oppia.android.app.model.EventLog
import org.oppia.android.app.viewmodel.ViewModelProvider
import org.oppia.android.databinding.RevisionCardFragmentBinding
import org.oppia.android.domain.oppialogger.OppiaLogger
import org.oppia.android.util.gcsresource.DefaultResourceBucketName
import org.oppia.android.util.parser.HtmlParser
import org.oppia.android.util.parser.TopicHtmlParserEntityType
import org.oppia.android.util.system.OppiaClock
import javax.inject.Inject

Expand All @@ -18,6 +22,9 @@ class RevisionCardFragmentPresenter @Inject constructor(
private val fragment: Fragment,
private val oppiaLogger: OppiaLogger,
private val oppiaClock: OppiaClock,
private val htmlParserFactory: HtmlParser.Factory,
@DefaultResourceBucketName private val resourceBucketName: String,
@TopicHtmlParserEntityType private val entityType: String,
private val viewModelProvider: ViewModelProvider<RevisionCardViewModel>
) {

Expand All @@ -36,13 +43,23 @@ class RevisionCardFragmentPresenter @Inject constructor(
val view = binding.revisionCardExplanationText
val viewModel = getReviewCardViewModel()

viewModel.setSubtopicIdAndBinding(topicId, subtopicId, view)
viewModel.setTopicAndSubtopicId(topicId, subtopicId)
logRevisionCardEvent(topicId, subtopicId)

binding.let {
it.viewModel = viewModel
it.lifecycleOwner = fragment
}

viewModel.revisionCardLiveData.observe(
fragment,
Observer {
view.text = htmlParserFactory.create(
resourceBucketName, entityType, topicId, imageCenterAlign = true
).parseOppiaHtml(it.pageContents.html, view)
}
)

return binding.root
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.oppia.android.app.topic.revisioncard

import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
Expand All @@ -10,73 +9,60 @@ import org.oppia.android.app.model.RevisionCard
import org.oppia.android.app.viewmodel.ObservableViewModel
import org.oppia.android.domain.topic.TopicController
import org.oppia.android.util.data.AsyncResult
import org.oppia.android.util.gcsresource.DefaultResourceBucketName
import org.oppia.android.util.logging.ConsoleLogger
import org.oppia.android.util.parser.HtmlParser
import org.oppia.android.util.parser.TopicHtmlParserEntityType
import javax.inject.Inject

// TODO(#1633): Fix ViewModel to not depend on View
/** [ObservableViewModel] for revision card, providing rich text and worked examples */
@FragmentScope
class RevisionCardViewModel @Inject constructor(
activity: AppCompatActivity,
private val topicController: TopicController,
private val logger: ConsoleLogger,
private val htmlParserFactory: HtmlParser.Factory,
@DefaultResourceBucketName private val resourceBucketName: String,
@TopicHtmlParserEntityType private val entityType: String
private val logger: ConsoleLogger
) : ObservableViewModel() {
private lateinit var topicId: String
private var subtopicId: Int = 0
private lateinit var view: TextView

private val returnToTopicClickListener: ReturnToTopicClickListener =
activity as ReturnToTopicClickListener

val explanationLiveData: LiveData<CharSequence> by lazy {
processExplanationLiveData()
val revisionCardLiveData: LiveData<RevisionCard> by lazy {
processRevisionCardLiveData()
}

fun clickReturnToTopic(@Suppress("UNUSED_PARAMETER") v: View) {
returnToTopicClickListener.onReturnToTopicClicked()
}

/** Sets the value of topicId, subtopicId and binding before anything else. */
fun setSubtopicIdAndBinding(
/** Sets the value of topicId, subtopicId before anything else. */
fun setTopicAndSubtopicId(
topicId: String,
subtopicId: Int,
view: TextView
subtopicId: Int
) {
this.topicId = topicId
this.subtopicId = subtopicId
this.view = view
}

private val revisionCardResultLiveData: LiveData<AsyncResult<RevisionCard>> by lazy {
topicController.getRevisionCard(topicId, subtopicId)
}

private fun processExplanationLiveData(): LiveData<CharSequence> {
return Transformations.map(revisionCardResultLiveData, ::processExplanationResult)
private fun processRevisionCardLiveData(): LiveData<RevisionCard> {
return Transformations.map(revisionCardResultLiveData, ::processRevisionCard)
}

private fun processExplanationResult(
private fun processRevisionCard(
revisionCardResult: AsyncResult<RevisionCard>
): CharSequence {
): RevisionCard {
if (revisionCardResult.isFailure()) {
logger.e(
"RevisionCardFragment",
"Failed to retrieve Revision Card",
revisionCardResult.getErrorOrNull()!!
)
}
val revisionCard = revisionCardResult.getOrDefault(

return revisionCardResult.getOrDefault(
RevisionCard.getDefaultInstance()
)
return htmlParserFactory.create(

resourceBucketName, entityType, topicId, /* imageCenterAlign= */ true
).parseOppiaHtml(revisionCard.pageContents.html, view)
}
}
1 change: 0 additions & 1 deletion app/src/main/res/layout-land/revision_card_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
android:layout_marginTop="24dp"
android:layout_marginEnd="52dp"
android:fontFamily="sans-serif"
android:text="@{viewModel.explanationLiveData}"
android:textColor="@color/oppiaPrimaryText"
android:textSize="16sp" />

Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/revision_card_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
android:layout_marginTop="36dp"
android:layout_marginEnd="28dp"
android:fontFamily="sans-serif"
android:text="@{viewModel.explanationLiveData}"
android:textColor="@color/oppiaPrimaryText"
android:textSize="16sp" />

Expand Down

0 comments on commit 1f8a133

Please sign in to comment.