Skip to content

Commit

Permalink
fix: selection of selected profile
Browse files Browse the repository at this point in the history
  • Loading branch information
oyeraghib committed Mar 10, 2025
1 parent 5aa5c01 commit 600a1a3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,84 +1,60 @@
/***************************************************************************************
* Copyright (c) 2025 Mohd Raghib <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 3 of the License, or (at your option) any later *
* version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/

package com.ichi2.anki.dialogs.switchProfiles

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.ichi2.anki.R

class ProfileAdapter(
private val profiles: List<String>,
private val onClick: (String) -> Unit,
private val context: Context,
) : RecyclerView.Adapter<ProfileAdapter.ProfileViewHolder>() {
private var selectedPosition = 0 // Default: First profile selected

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): ProfileViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.list_items_switch_profiles, parent, false)
val view =
LayoutInflater
.from(parent.context)
.inflate(R.layout.list_items_switch_profiles, parent, false)
return ProfileViewHolder(view)
}

override fun onBindViewHolder(
holder: ProfileViewHolder,
position: Int,
) {
val profileName = profiles[position]
holder.bind(profileName, onClick, context, position == selectedPosition)

// Update selection state
holder.profileText.text = profiles[position]

// Highlight only the TextView instead of the entire item
holder.profileText.setBackgroundResource(
if (position == selectedPosition) {
R.drawable.selected_profile_background
} else {
android.R.color.transparent
},
)

// Handle click event
holder.itemView.setOnClickListener {
val previousSelected = selectedPosition
selectedPosition = position
notifyItemChanged(previousSelected) // Refresh old selection
notifyItemChanged(selectedPosition) // Refresh new selection

onClick(profileName)
notifyItemChanged(previousSelected) // Remove highlight from old selection
notifyItemChanged(selectedPosition) // Highlight the new selection

onClick(profiles[position])
}
}

override fun getItemCount(): Int = profiles.size

class ProfileViewHolder(
// Inner class ViewHolder
inner class ProfileViewHolder(
itemView: View,
) : RecyclerView.ViewHolder(itemView) {
private val profileText: TextView = itemView.findViewById(R.id.profile_name)

fun bind(
profileName: String,
onClick: (String) -> Unit,
context: Context,
isSelected: Boolean,
) {
profileText.text = profileName
// Highlight selected profile
itemView.setBackgroundColor(
ContextCompat.getColor(context, if (isSelected) R.color.material_blue_grey_100 else R.color.white),
)
itemView.setOnClickListener {
onClick(profileName)
}
}
val profileText: TextView = itemView.findViewById(R.id.profile_name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.ichi2.anki.dialogs.switchProfiles

import ProfileAdapter
import android.os.Bundle
import android.view.Gravity
import android.view.View
Expand Down Expand Up @@ -67,7 +68,7 @@ class SwitchProfilesDialog : DialogFragment() {
)

showThemedToast(requireContext(), "Profile selected: $selectedProfile", true)
}, requireContext())
})

// Handle button clicks (TODO: Implement actual functionality)
btnOpen.setOnClickListener { dismiss() } // Open selected profile
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/material_light_blue_100" />
<corners android:radius="8dp"/>
<padding android:left="8dp" android:right="8dp" android:top="4dp" android:bottom="4dp"/>
</shape>
5 changes: 2 additions & 3 deletions AnkiDroid/src/main/res/layout/list_items_switch_profiles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
android:textColor="?attr/colorPrimary"
android:paddingStart="8dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
tools:text="User Profile 1" />
tools:text="User Profile 1"
android:paddingVertical="12dp"/>

<View
android:layout_width="match_parent"
Expand Down

0 comments on commit 600a1a3

Please sign in to comment.