Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/oppia/oppia-android into…
Browse files Browse the repository at this point in the history
… develop
  • Loading branch information
PrarabdhGarg committed Jan 30, 2020
2 parents dc7bbbb + 65550b0 commit 043184f
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 37 deletions.
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
android:screenOrientation="portrait" />
<activity
android:name=".profile.AdminAuthActivity"
android:screenOrientation="portrait"
android:theme="@style/OppiaThemeWithoutActionBar"
android:windowSoftInputMode="adjustResize" />
<activity
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/org/oppia/app/home/HomeActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.oppia.app.home

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
Expand All @@ -9,10 +10,20 @@ import org.oppia.app.profile.ProfileActivity
import org.oppia.app.topic.TopicActivity
import javax.inject.Inject

const val KEY_HOME_PROFILE_ID = "KEY_HOME_PROFILE_ID"

/** The central activity for all users entering the app. */
class HomeActivity : InjectableAppCompatActivity(), RouteToTopicListener {
@Inject lateinit var homeActivityPresenter: HomeActivityPresenter

companion object {
fun createHomeActivity(context: Context, profileId: Int?): Intent {
val intent = Intent(context, HomeActivity::class.java)
intent.putExtra(KEY_HOME_PROFILE_ID, profileId)
return intent
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityComponent.inject(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import org.oppia.app.activity.ActivityScope
import org.oppia.app.drawer.NavigationDrawerFragment
import javax.inject.Inject

const val TAG_HOME_FRAGMENT = "HOME_FRAGMENT"

/** The presenter for [HomeActivity]. */
@ActivityScope
class HomeActivityPresenter @Inject constructor(private val activity: AppCompatActivity) {
Expand All @@ -20,7 +22,8 @@ class HomeActivityPresenter @Inject constructor(private val activity: AppCompatA
if (getHomeFragment() == null) {
activity.supportFragmentManager.beginTransaction().add(
R.id.home_fragment_placeholder,
HomeFragment()
HomeFragment(),
TAG_HOME_FRAGMENT
).commitNow()
}
}
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/org/oppia/app/home/HomeFragmentPresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import org.oppia.app.home.topiclist.TopicListAdapter
import org.oppia.app.home.topiclist.TopicSummaryClickListener
import org.oppia.app.home.topiclist.TopicSummaryViewModel
import org.oppia.app.model.OngoingStoryList
import org.oppia.app.model.Profile
import org.oppia.app.model.ProfileId
import org.oppia.app.model.TopicList
import org.oppia.app.model.TopicSummary
import org.oppia.app.model.UserAppHistory
import org.oppia.domain.UserAppHistoryController
import org.oppia.domain.profile.ProfileManagementController
import org.oppia.domain.topic.TopicListController
import org.oppia.util.data.AsyncResult
import org.oppia.util.logging.Logger
Expand All @@ -32,6 +35,7 @@ import javax.inject.Inject
class HomeFragmentPresenter @Inject constructor(
private val activity: AppCompatActivity,
private val fragment: Fragment,
private val profileManagementController: ProfileManagementController,
private val userAppHistoryController: UserAppHistoryController,
private val topicListController: TopicListController,
private val logger: Logger
Expand All @@ -44,6 +48,10 @@ class HomeFragmentPresenter @Inject constructor(
private lateinit var allTopicsViewModel: AllTopicsViewModel
private lateinit var topicListAdapter: TopicListAdapter
private lateinit var binding: HomeFragmentBinding
private var internalProfileId: Int = -1
private lateinit var profileId: ProfileId
private lateinit var profileName: String

fun handleCreateView(inflater: LayoutInflater, container: ViewGroup?): View? {
binding = HomeFragmentBinding.inflate(inflater, container, /* attachToRoot= */ false)
// NB: Both the view model and lifecycle owner must be set in order to correctly bind LiveData elements to
Expand All @@ -57,6 +65,9 @@ class HomeFragmentPresenter @Inject constructor(
itemList.add(allTopicsViewModel)
topicListAdapter = TopicListAdapter(activity, itemList, promotedStoryList)

internalProfileId = activity.intent.getIntExtra(KEY_HOME_PROFILE_ID, -1)
profileId = ProfileId.newBuilder().setInternalId(internalProfileId).build()

val homeLayoutManager = GridLayoutManager(activity.applicationContext, 2)
homeLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
Expand All @@ -79,12 +90,35 @@ class HomeFragmentPresenter @Inject constructor(
}

userAppHistoryController.markUserOpenedApp()
subscribeToProfileLiveData()
subscribeToUserAppHistory()
subscribeToOngoingStoryList()
subscribeToTopicList()
return binding.root
}

private val profileLiveData: LiveData<Profile> by lazy {
getProfileData()
}

private fun getProfileData(): LiveData<Profile> {
return Transformations.map(profileManagementController.getProfile(profileId), ::processGetProfileResult)
}

private fun subscribeToProfileLiveData() {
profileLiveData.observe(activity, Observer<Profile> { result ->
profileName = result.name
setProfileName()
})
}

private fun processGetProfileResult(profileResult: AsyncResult<Profile>): Profile {
if (profileResult.isFailure()) {
logger.e("HomeFragment", "Failed to retrieve profile", profileResult.getErrorOrNull()!!)
}
return profileResult.getOrDefault(Profile.getDefaultInstance())
}

private val topicListSummaryResultLiveData: LiveData<AsyncResult<TopicList>> by lazy {
topicListController.getTopicList()
}
Expand All @@ -108,6 +142,7 @@ class HomeFragmentPresenter @Inject constructor(
getUserAppHistory().observe(fragment, Observer<UserAppHistory> { result ->
userAppHistoryViewModel = UserAppHistoryViewModel()
userAppHistoryViewModel.setAlreadyAppOpened(result.alreadyOpenedApp)
setProfileName()
itemList[0] = userAppHistoryViewModel
topicListAdapter.notifyItemChanged(0)
})
Expand All @@ -125,6 +160,12 @@ class HomeFragmentPresenter @Inject constructor(
return appHistoryResult.getOrDefault(UserAppHistory.getDefaultInstance())
}

private fun setProfileName() {
if (::userAppHistoryViewModel.isInitialized && ::profileName.isInitialized) {
userAppHistoryViewModel.profileName = "$profileName!"
}
}

private val ongoingStoryListSummaryResultLiveData: LiveData<AsyncResult<OngoingStoryList>> by lazy {
topicListController.getOngoingStoryList()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.ViewModel
/** [ViewModel] for user app usage history. */
class UserAppHistoryViewModel : HomeItemViewModel() {
var isAppAlreadyOpened = ObservableField<Boolean>(false)
var profileName : String = ""

fun setAlreadyAppOpened(alreadyOpenedApp: Boolean) = isAppAlreadyOpened.set(alreadyOpenedApp)
}
10 changes: 10 additions & 0 deletions app/src/main/java/org/oppia/app/profile/AdminAuthActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class AdminAuthActivity : InjectableAppCompatActivity() {
adminAuthFragmentPresenter.handleOnCreate()
}

override fun onSaveInstanceState(outState: Bundle) {
adminAuthFragmentPresenter.handleOnSavedInstanceState(outState)
super.onSaveInstanceState(outState)
}

override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
super.onRestoreInstanceState(savedInstanceState)
adminAuthFragmentPresenter.handleOnRestoreInstanceState(savedInstanceState!!)
}

override fun onSupportNavigateUp(): Boolean {
finish()
return false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.oppia.app.profile

import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -11,6 +12,8 @@ import org.oppia.app.databinding.AdminAuthActivityBinding
import org.oppia.app.viewmodel.ViewModelProvider
import javax.inject.Inject

const val KEY_ADMIN_AUTH_INPUT_ERROR_MESSAGE = "ADMIN_AUTH_INPUT_ERROR_MESSAGE"

/** The presenter for [AdminAuthActivity]. */
@ActivityScope
class AdminAuthActivityPresenter @Inject constructor(
Expand Down Expand Up @@ -61,6 +64,17 @@ class AdminAuthActivityPresenter @Inject constructor(
}
}

fun handleOnSavedInstanceState(bundle: Bundle) {
bundle.putString(KEY_ADMIN_AUTH_INPUT_ERROR_MESSAGE, authViewModel.errorMessage.get())
}

fun handleOnRestoreInstanceState(bundle: Bundle) {
val errorMessage = bundle.getString(KEY_ADMIN_AUTH_INPUT_ERROR_MESSAGE)
if (errorMessage != null && errorMessage.isNotEmpty()) {
authViewModel.errorMessage.set(errorMessage)
}
}

private fun getAdminAuthViewModel(): AdminAuthViewModel {
return viewModelProvider.getForActivity(activity, AdminAuthViewModel::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PinPasswordActivityPresenter @Inject constructor(
profileManagementController.loginToProfile(ProfileId.newBuilder().setInternalId(profileId).build())
.observe(activity, Observer {
if (it.isSuccess()) {
activity.startActivity(Intent(activity, HomeActivity::class.java))
activity.startActivity((HomeActivity.createHomeActivity(activity, profileId)))
}
})
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.oppia.app.profile

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -112,7 +111,7 @@ class ProfileChooserFragmentPresenter @Inject constructor(
if (model.profile.pin.isEmpty()) {
profileManagementController.loginToProfile(model.profile.id).observe(fragment, Observer {
if (it.isSuccess()) {
activity.startActivity(Intent(fragment.context, HomeActivity::class.java))
activity.startActivity((HomeActivity.createHomeActivity(activity, model.profile.id.internalId)))
}
})
} else {
Expand Down
155 changes: 155 additions & 0 deletions app/src/main/res/layout-land/admin_auth_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:profile="http://schemas.android.com/tools"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto" >

<data>
<variable
name="viewModel"
type="org.oppia.app.profile.AdminAuthViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/admin_auth_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/addProfileBackground">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/admin_auth_app_bar_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.appcompat.widget.Toolbar
android:id="@+id/admin_auth_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:fontFamily="sans-serif"
android:minHeight="?attr/actionBarSize"
android:textSize="20sp"
app:navigationIcon="@drawable/ic_close_white_24dp"
app:title="@string/admin_auth_heading"
app:titleTextColor="@color/white" />
</com.google.android.material.appbar.AppBarLayout>

<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/admin_auth_app_bar_layout">

<ScrollView
android:id="@+id/admin_auth_landscape_scrollView"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/heading_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:fontFamily="sans-serif-medium"
android:lines="2"
android:paddingStart="28dp"
android:paddingEnd="28dp"
android:text="@string/admin_auth_heading"
android:textColor="@color/oppiaPrimaryText"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/sub_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:fontFamily="sans-serif"
android:paddingStart="28dp"
android:paddingEnd="28dp"
android:text="@string/admin_auth_sub"
android:textColor="@color/black_54"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/heading_text" />

<LinearLayout
android:id="@+id/admin_auth_linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/sub_text">

<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10" />

<org.oppia.app.profile.ProfileInputView
android:id="@+id/input_pin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_weight="18"
android:addTextChangedListener="@{viewModel.inputPinTextWatcher}"
android:imeOptions="actionDone"
app:inputLength="5"
app:isPasswordInput="true"
app:label="@string/admin_auth_pin"
profile:error="@{viewModel.errorMessage}" />

<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10" />
</LinearLayout>

<Button
android:id="@+id/submit_button"
style="@style/StateButtonActive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:layout_marginEnd="28dp"
android:layout_marginBottom="36dp"
android:layout_weight="18"
android:background="@{viewModel.isSubmitButtonActive.get()? @drawable/state_button_primary_background :@drawable/start_button_transparent_background}"
android:gravity="center"
android:minHeight="48dp"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:text="@string/state_continue_submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.77"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/admin_auth_linearLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

<View
android:id="@+id/toolbar_shadow_view"
android:layout_width="match_parent"
android:layout_height="6dp"
android:background="@drawable/toolbar_drop_shadow" />
</FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

</layout>
Loading

0 comments on commit 043184f

Please sign in to comment.