-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
- Loading branch information
Showing
14 changed files
with
407 additions
and
127 deletions.
There are no files selected for viewing
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
3 changes: 3 additions & 0 deletions
3
app/src/main/java/org/ole/planet/myplanet/model/ServerAddressesModel.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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package org.ole.planet.myplanet.model | ||
|
||
data class ServerAddressesModel(val name: String, val url: String) |
75 changes: 75 additions & 0 deletions
75
app/src/main/java/org/ole/planet/myplanet/ui/sync/ServerAddressAdapter.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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.ole.planet.myplanet.ui.sync | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.core.content.ContextCompat | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.google.android.material.button.MaterialButton | ||
import org.ole.planet.myplanet.R | ||
import org.ole.planet.myplanet.model.ServerAddressesModel | ||
|
||
class ServerAddressAdapter( | ||
private var serverList: List<ServerAddressesModel>, | ||
private val onItemClick: (ServerAddressesModel) -> Unit, | ||
private val onClearDataDialog: (ServerAddressesModel, Int) -> Unit, // Add callback for clear data dialog | ||
private val urlWithoutProtocol: String? // Pass the urlWithoutProtocol to the adapter | ||
) : RecyclerView.Adapter<ServerAddressAdapter.ViewHolder>() { | ||
private var selectedPosition: Int = -1 | ||
private var lastSelectedPosition: Int = -1 | ||
|
||
fun updateList(newList: List<ServerAddressesModel>) { | ||
serverList = newList | ||
notifyDataSetChanged() | ||
} | ||
|
||
fun setSelectedPosition(position: Int) { | ||
lastSelectedPosition = selectedPosition | ||
selectedPosition = position | ||
notifyDataSetChanged() | ||
} | ||
|
||
fun revertSelection() { | ||
selectedPosition = lastSelectedPosition | ||
notifyDataSetChanged() | ||
} | ||
|
||
fun clearSelection() { | ||
selectedPosition = -1 | ||
notifyDataSetChanged() | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_server_address, parent, false) | ||
return ViewHolder(view) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
val serverAddress = serverList[position] | ||
holder.bind(serverAddress, position == selectedPosition) | ||
holder.itemView.setOnClickListener { | ||
if (!urlWithoutProtocol.isNullOrEmpty() && serverAddress.url.replace(Regex("^https?://"), "") != urlWithoutProtocol) { | ||
onClearDataDialog(serverAddress, position) | ||
} else { | ||
onItemClick(serverAddress) | ||
setSelectedPosition(position) | ||
} | ||
} | ||
} | ||
|
||
override fun getItemCount(): Int = serverList.size | ||
|
||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
private val button: MaterialButton = itemView.findViewById(R.id.btn_server_address) | ||
|
||
fun bind(serverAddress: ServerAddressesModel, isSelected: Boolean) { | ||
button.text = serverAddress.name | ||
button.isSelected = isSelected | ||
if (isSelected) { | ||
button.setBackgroundColor(ContextCompat.getColor(button.context, R.color.selected_color)) | ||
} else { | ||
button.setBackgroundColor(ContextCompat.getColor(button.context, android.R.color.transparent)) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.