Skip to content

Commit

Permalink
Fix admin profile creation error
Browse files Browse the repository at this point in the history
  • Loading branch information
adhiamboperes committed Aug 27, 2024
1 parent 4c578e1 commit 99913bb
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ class OnboardingFragmentPresenter @Inject constructor(

private fun createDefaultProfile() {
profileManagementController.addProfile(
name = "Admin", // TODO(#4938): Refactor to empty name once proper admin profile creation flow
// is implemented.
name = "",
pin = "",
avatarImagePath = null,
allowDownloadAccess = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import org.oppia.android.databinding.OnboardingProfileTypeFragmentBinding
import org.oppia.android.util.extensions.putProtoExtra
import org.oppia.android.util.profile.CurrentUserProfileIdIntentDecorator.decorateWithUserProfileId
import javax.inject.Inject
import org.oppia.android.app.model.ProfileChooserActivityParams

/** Argument key for [CreateProfileActivity] intent parameters. */
const val CREATE_PROFILE_PARAMS_KEY = "CreateProfileActivity.params"

/** Argument key for [ProfileChooserActivity] intent parameters. */
const val PROFILE_CHOOSER_PARAMS_KEY = "ProfileChooserActivity.params"

/** The presenter for [OnboardingProfileTypeFragment]. */
class OnboardingProfileTypeFragmentPresenter @Inject constructor(
private val fragment: Fragment,
Expand Down Expand Up @@ -55,7 +59,15 @@ class OnboardingProfileTypeFragmentPresenter @Inject constructor(

profileTypeSupervisorNavigationCard.setOnClickListener {
val intent = ProfileChooserActivity.createProfileChooserActivity(activity)
// TODO(#4938): Add profileId and ProfileType to intent extras.
intent.apply {
decorateWithUserProfileId(profileId)
putProtoExtra(
PROFILE_CHOOSER_PARAMS_KEY,
ProfileChooserActivityParams.newBuilder()
.setProfileType(ProfileType.SUPERVISOR)
.build()
)
}
fragment.startActivity(intent)
// Clear back stack so that user cannot go back to the onboarding flow.
fragment.activity?.finishAffinity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import org.oppia.android.app.activity.InjectableSystemLocalizedAppCompatActivity
import org.oppia.android.app.model.ScreenName.PROFILE_CHOOSER_ACTIVITY
import org.oppia.android.util.logging.CurrentAppScreenNameIntentDecorator.decorateWithScreenName
import javax.inject.Inject
import org.oppia.android.app.model.CreateProfileActivityParams
import org.oppia.android.app.model.ProfileChooserActivityParams
import org.oppia.android.app.onboarding.CREATE_PROFILE_PARAMS_KEY
import org.oppia.android.app.onboarding.PROFILE_CHOOSER_PARAMS_KEY
import org.oppia.android.util.extensions.getProtoExtra
import org.oppia.android.util.profile.CurrentUserProfileIdIntentDecorator.extractCurrentUserProfileId

/** Activity that controls profile creation and selection. */
class ProfileChooserActivity : InjectableSystemLocalizedAppCompatActivity() {
Expand All @@ -26,6 +32,14 @@ class ProfileChooserActivity : InjectableSystemLocalizedAppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(activityComponent as ActivityComponentImpl).inject(this)
profileChooserActivityPresenter.handleOnCreate()

val profileType = intent.getProtoExtra(
PROFILE_CHOOSER_PARAMS_KEY,
ProfileChooserActivityParams.getDefaultInstance()
).profileType

val profileId = intent.extractCurrentUserProfileId()

profileChooserActivityPresenter.handleOnCreate(profileId, profileType)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,42 @@ import org.oppia.android.app.activity.ActivityScope
import org.oppia.android.app.testing.ProfileChooserFragmentTestActivity
import org.oppia.android.domain.profile.ProfileManagementController
import javax.inject.Inject
import org.oppia.android.app.model.ProfileId
import org.oppia.android.app.model.ProfileType
import org.oppia.android.util.platformparameter.EnableOnboardingFlowV2
import org.oppia.android.util.platformparameter.PlatformParameterValue

/** The presenter for [ProfileChooserActivity]. */
@ActivityScope
class ProfileChooserActivityPresenter @Inject constructor(
private val activity: AppCompatActivity,
private val profileManagementController: ProfileManagementController
private val profileManagementController: ProfileManagementController,
@EnableOnboardingFlowV2
private val enableOnboardingFlowV2: PlatformParameterValue<Boolean>
) {
/** Adds [ProfileChooserFragment] to view. */
fun handleOnCreate() {
// TODO(#482): Ensures that an admin profile is present. Remove when there is proper admin account creation.
profileManagementController.addProfile(
name = "Admin",
pin = "",
avatarImagePath = null,
allowDownloadAccess = true,
colorRgb = -10710042,
isAdmin = true
)
fun handleOnCreate(profileId: ProfileId, profileType: ProfileType) {
if (enableOnboardingFlowV2.value) {
profileManagementController.updateNewProfileDetails(
profileId = profileId,
profileType = profileType,
newName = "Admin",
avatarImagePath = null,
colorRgb = -10710042,
isAdmin = true
)
} else {
// TODO(#482): Ensures that an admin profile is present. Remove when there is proper admin account creation.
profileManagementController.addProfile(
name = "Admin",
pin = "",
avatarImagePath = null,
allowDownloadAccess = true,
colorRgb = -10710042,
isAdmin = true
)
}

activity.setContentView(R.layout.profile_chooser_activity)
if (getProfileChooserFragment() == null) {
activity.supportFragmentManager.beginTransaction().add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import org.oppia.android.util.platformparameter.PlatformParameterValue
import org.oppia.android.util.profile.CurrentUserProfileIdIntentDecorator.decorateWithUserProfileId
import org.oppia.android.util.statusbar.StatusBarColor
import javax.inject.Inject
import org.oppia.android.app.model.ProfileType

private val COLORS_LIST = listOf(
R.color.component_color_avatar_background_1_color,
Expand Down Expand Up @@ -212,11 +213,9 @@ class ProfileChooserFragmentPresenter @Inject constructor(

/** Randomly selects a color for the new profile that is not already in use. */
private fun selectUniqueRandomColor(): Int {
val availableColors = COLORS_LIST.map {
return COLORS_LIST.map {
ContextCompat.getColor(context, it)
}.minus(chooserViewModel.usedColors.toSet())

return availableColors.random()
}.minus(chooserViewModel.usedColors).random()
}

private fun createRecyclerViewAdapter(): BindableAdapter<ProfileItemViewModel> {
Expand Down Expand Up @@ -273,18 +272,13 @@ class ProfileChooserFragmentPresenter @Inject constructor(
}

private fun ensureProfileOnboarded(profile: Profile) {
if (isAdminWithPin(profile.isAdmin, profile.pin) || profile.completedProfileOboarding) {
if (profile.profileType == ProfileType.SUPERVISOR || profile.completedProfileOboarding) {
loginToProfile(profile)
} else {
launchOnboardingScreen(profile.id, profile.name)
}
}

// TODO(#4938): Replace with proper admin profile migration.
private fun isAdminWithPin(isAdmin: Boolean, pin: String?): Boolean {
return isAdmin && !pin.isNullOrBlank()
}

private fun launchOnboardingScreen(profileId: ProfileId, profileName: String) {
val introActivityParams = IntroActivityParams.newBuilder()
.setProfileNickname(profileName)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/profile_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
style="@style/Subtitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:textAlignment="center"
android:textColor="@color/component_color_shared_primary_text_color"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,6 @@ class ProfileManagementController @Inject constructor(
avatarImageUri = imageUri
} else avatarColorRgb = colorRgb
}.build()

if (enableOnboardingFlowV2.value) {
this.profileType = computeProfileType(isAdmin, pin)
}
}.build()

val wasProfileEverAdded = it.profilesCount > 0
Expand Down Expand Up @@ -851,7 +847,7 @@ class ProfileManagementController @Inject constructor(
profileId: ProfileId,
profileType: ProfileType,
avatarImagePath: Uri?,
colorRgb: Int,
colorRgb: Int?,
newName: String,
isAdmin: Boolean
): DataProvider<Any?> {
Expand Down Expand Up @@ -881,7 +877,7 @@ class ProfileManagementController @Inject constructor(
ProfileAvatar.newBuilder().setAvatarImageUri(imageUri).build()
} else {
updatedProfile.avatar =
ProfileAvatar.newBuilder().setAvatarColorRgb(colorRgb).build()
colorRgb?.let { color -> ProfileAvatar.newBuilder().setAvatarColorRgb(color).build() }
}

if (profileType == ProfileType.PROFILE_TYPE_UNSPECIFIED) {
Expand Down
12 changes: 12 additions & 0 deletions model/src/main/proto/arguments.proto
Original file line number Diff line number Diff line change
Expand Up @@ -925,3 +925,15 @@ message OnboardingFragmentStateBundle {
// The current selected language.
OppiaLanguage selected_language = 1;
}

// Params required when creating a new ProfileChooserActivity.
message ProfileChooserActivityParams {
// The ProfileType of the new profile as implied by the user's selection.
ProfileType profile_type = 1;
}

// Arguments required when creating a new ProfileChooserFragment.
message ProfileChooserFragmentArguments {
// The ProfileType of the new profile as implied by the user's selection.
ProfileType profile_type = 1;
}

0 comments on commit 99913bb

Please sign in to comment.