Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix part of #136: Topic overview low fi part 2 - Topic Download/Delete [ON HOLD] #236

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.oppia.app.topic.overview

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import org.oppia.app.R

/**
* DialogFragment that confirms whether user wants to delete the topic or not.
*/
class TopicDeleteDialogFragment : DialogFragment() {
companion object {
/**
* This function is responsible for displaying content in DialogFragment.
*
* @return [TopicDeleteDialogFragment]: DialogFragment
*/
fun newInstance(): TopicDeleteDialogFragment {
return TopicDeleteDialogFragment()
}
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val topicDeleteListener: TopicDeleteListener = parentFragment as TopicOverviewFragment

return AlertDialog.Builder(activity as Context)
.setMessage(R.string.topic_delete_alert_dialog_description)
.setPositiveButton(R.string.topic_delete_alert_dialog_delete_button) { dialog, whichButton ->
topicDeleteListener.deleteTopic()
dismiss()
}
.setNegativeButton(R.string.topic_delete_alert_dialog_cancel_button) { dialog, whichButton ->
dismiss()
}
.create()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.oppia.app.topic.overview

/** Interface to confirm whether user wants to delete the topic. */
interface TopicDeleteListener {
fun deleteTopic()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.oppia.app.topic.overview

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import android.widget.CheckBox
import org.oppia.app.R

/**
* DialogFragment that indicates to the user they are on cellular when trying to download topic.
*/
class TopicDownloadDialogFragment : DialogFragment() {
companion object {
/**
* This function is responsible for displaying content in DialogFragment.
*
* @return [TopicDownloadDialogFragment]: DialogFragment
*/
fun newInstance(): TopicDownloadDialogFragment {
return TopicDownloadDialogFragment()
}
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val view = activity!!.layoutInflater.inflate(R.layout.topic_download_dialog, /* root= */null)
val checkBox = view.findViewById<CheckBox>(R.id.topic_download_dialog_checkbox)

val topicDownloadListener: TopicDownloadListener = parentFragment as TopicOverviewFragment

return AlertDialog.Builder(activity as Context)
.setTitle(R.string.topic_download_alert_dialog_title)
.setView(view)
.setMessage(R.string.topic_download_alert_dialog_description)
.setPositiveButton(R.string.topic_download_alert_dialog_download_button) { dialog, whichButton ->
topicDownloadListener.downloadTopicWhileOnCellular(checkBox.isChecked)
dismiss()
}
.setNegativeButton(R.string.topic_download_alert_dialog_cancel_button) { dialog, whichButton ->
topicDownloadListener.doNotDownloadTopicWhileOnCellular(checkBox.isChecked)
dismiss()
}
.create()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.oppia.app.topic.overview

/** Interface to check the preference regarding alert for [TopicDownloadDialogFragment]. */
interface TopicDownloadListener {
/**
* If saveUserChoice is true, download topic and save preference do not show dialog again.
* If saveUserChoice is false, download topic and do not save preference and show this dialog next time too.
*/
fun downloadTopicWhileOnCellular(saveUserChoice: Boolean)

/**
* If saveUserChoice is true, do not download topic on cellular network and save preference.
* If saveUserChoice is false, do not download topic and do not save preference.
*/
fun doNotDownloadTopicWhileOnCellular(saveUserChoice: Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.oppia.app.fragment.InjectableFragment
import javax.inject.Inject

/** Fragment that contains overview of Topic. */
class TopicOverviewFragment : InjectableFragment() {
class TopicOverviewFragment : InjectableFragment(), TopicDownloadListener, TopicDeleteListener {
@Inject
lateinit var topicOverviewFragmentPresenter: TopicOverviewFragmentPresenter

Expand All @@ -21,4 +21,16 @@ class TopicOverviewFragment : InjectableFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return topicOverviewFragmentPresenter.handleCreateView(inflater, container)
}

override fun downloadTopicWhileOnCellular(saveUserChoice: Boolean) {
topicOverviewFragmentPresenter.handleDownloadTopic(saveUserChoice)
}

override fun doNotDownloadTopicWhileOnCellular(saveUserChoice: Boolean) {
topicOverviewFragmentPresenter.handleDoNotDownloadTopic(saveUserChoice)
}

override fun deleteTopic() {
topicOverviewFragmentPresenter.handleDeleteTopic()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import org.oppia.util.data.AsyncResult
import org.oppia.util.logging.Logger
import javax.inject.Inject

private const val TAG_TOPIC_DELETE_DIALOG = "TOPIC_DELETE_DIALOG"
private const val TAG_TOPIC_DOWNLOAD_DIALOG = "TOPIC_DOWNLOAD_DIALOG"

/** The presenter for [TopicOverviewFragment]. */
@FragmentScope
class TopicOverviewFragmentPresenter @Inject constructor(
Expand All @@ -30,7 +33,7 @@ class TopicOverviewFragmentPresenter @Inject constructor(
) {
private val routeToTopicPlayListener = activity as RouteToTopicPlayListener

private val topicOverviewViewModel = getTopicOverviewViewModel()
private val topicOverviewViewModel = getTopicOverviewViewModel()

fun handleCreateView(inflater: LayoutInflater, container: ViewGroup?): View? {
val binding = TopicOverviewFragmentBinding.inflate(inflater, container, /* attachToRoot= */ false)
Expand All @@ -43,12 +46,48 @@ class TopicOverviewFragmentPresenter @Inject constructor(
return binding.root
}

private fun showTopicDownloadDialogFragment() {
val previousFragment = fragment.childFragmentManager.findFragmentByTag(TAG_TOPIC_DOWNLOAD_DIALOG)
if (previousFragment != null) {
fragment.childFragmentManager.beginTransaction().remove(previousFragment).commitNow()
}
val dialogFragment = TopicDownloadDialogFragment.newInstance()
dialogFragment.showNow(fragment.childFragmentManager, TAG_TOPIC_DOWNLOAD_DIALOG)
}

private fun showTopicDeleteDialogFragment() {
val previousFragment = fragment.childFragmentManager.findFragmentByTag(TAG_TOPIC_DELETE_DIALOG)
if (previousFragment != null) {
fragment.childFragmentManager.beginTransaction().remove(previousFragment).commitNow()
}
val dialogFragment = TopicDeleteDialogFragment.newInstance()
dialogFragment.showNow(fragment.childFragmentManager, TAG_TOPIC_DELETE_DIALOG)
}

fun seeMoreClicked(v: View) {
routeToTopicPlayListener.routeToTopicPlayFragment()
}

private fun getTopicOverviewViewModel(): TopicOverviewViewModel {
return viewModelProvider.getForFragment(fragment, TopicOverviewViewModel::class.java)
fun downloadStatusImageClicked(v: View) {
when (topicOverviewViewModel.downloadStatus.get()) {
STATUS_NOT_DOWNLOADED -> showTopicDownloadDialogFragment()
STATUS_DOWNLOADED -> showTopicDeleteDialogFragment()
/** STATUS_DOWNLOADING -> TODO(Rajat): Discuss with Ben regarding this case. */
}
}

fun handleDownloadTopic(saveUserChoice: Boolean) {
// TODO(Rajat): Save this preference and change icon only when download is finished.
topicOverviewViewModel.downloadStatus.set(STATUS_DOWNLOADED)
}

fun handleDoNotDownloadTopic(saveUserChoice: Boolean) {
// TODO(Rajat): Save this preference and do not download topic.
}

fun handleDeleteTopic() {
// TODO(Rajat): Delete topic from device.
topicOverviewViewModel.downloadStatus.set(STATUS_NOT_DOWNLOADED)
}

private val topicLiveData: LiveData<Topic> by lazy { getTopicList() }
Expand All @@ -74,4 +113,8 @@ class TopicOverviewFragmentPresenter @Inject constructor(
}
return topic.getOrDefault(Topic.getDefaultInstance())
}

private fun getTopicOverviewViewModel(): TopicOverviewViewModel {
return viewModelProvider.getForFragment(fragment, TopicOverviewViewModel::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ import org.oppia.app.model.Topic
import org.oppia.app.viewmodel.ObservableViewModel
import javax.inject.Inject

const val STATUS_NOT_DOWNLOADED = "STATUS_NOT_DOWNLOADED"
const val STATUS_DOWNLOADED = "STATUS_DOWNLOADED"
const val STATUS_DOWNLOADING = "STATUS_DOWNLOADING"

/** [ViewModel] for showing topic overview details. */
@FragmentScope
class TopicOverviewViewModel @Inject constructor() : ObservableViewModel() {
companion object {
@JvmStatic
@BindingAdapter("downloadDrawable")
fun setBackgroundResource(downloadStatus: ImageView, resource: Int) {
downloadStatus.setImageResource(resource)
fun setBackgroundResource(downloadImageView: ImageView, downloadStatus: String) {
when (downloadStatus) {
STATUS_NOT_DOWNLOADED -> downloadImageView.setImageResource(R.drawable.ic_file_download_primary_24dp)
STATUS_DOWNLOADED -> downloadImageView.setImageResource(R.drawable.ic_check_circle_primary_24dp)
STATUS_DOWNLOADING -> downloadImageView.setImageResource(R.drawable.ic_pause_circle_filled_black_24dp)
}
}
}

val topic = ObservableField<Topic>(Topic.getDefaultInstance())

var downloadStatus = ObservableField<Int>(R.drawable.ic_file_download_primary_24dp)
var downloadStatus = ObservableField<String>(STATUS_NOT_DOWNLOADED)
}
14 changes: 14 additions & 0 deletions app/src/main/res/layout/topic_download_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/cellular_data_dialog_padding">
rt4914 marked this conversation as resolved.
Show resolved Hide resolved
<CheckBox
android:id="@+id/topic_download_dialog_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:text="@string/cellular_data_alert_dialog_checkbox">
</CheckBox>
</RelativeLayout>
1 change: 1 addition & 0 deletions app/src/main/res/layout/topic_overview_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:onClick="@{presenter::downloadStatusImageClicked}"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/see_more_text_view"/>
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@
<string name="state_end_exploration_button">RETURN TO TOPIC</string>
<string name="state_learn_again_button">LEARN AGAIN</string>
<string name="topic_overview_see_more">See More</string>
<string name="topic_download_alert_dialog_download_button">DOWNLOAD NOW</string>
<string name="topic_download_alert_dialog_cancel_button">CANCEL</string>
<string name="topic_download_alert_dialog_title">Currently on Cellular Data</string>
<string name="topic_download_alert_dialog_description">You can fo to Settings to Download only when connected to Wi-Fi.</string>
<string name="topic_download_alert_dialog_checkbox">Don\'t show this message again</string>
<string name="topic_delete_alert_dialog_delete_button">DELETE</string>
<string name="topic_delete_alert_dialog_cancel_button">CANCEL</string>
<string name="topic_delete_alert_dialog_description">Delete this topic from the device?</string>
</resources>