forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
33 additions
and
51 deletions.
There are no files selected for viewing
70 changes: 23 additions & 47 deletions
70
AnkiDroid/src/main/java/com/ichi2/anki/dialogs/switchProfiles/ProfileAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
AnkiDroid/src/main/res/drawable/selected_profile_background.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters