-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated activity to retain state on configuration change
- Loading branch information
Showing
8 changed files
with
150 additions
and
112 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
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
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
33 changes: 33 additions & 0 deletions
33
...main/java/org/openobservatory/ooniprobe/activity/customwebsites/CustomWebsiteViewModel.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,33 @@ | ||
package org.openobservatory.ooniprobe.activity.customwebsites | ||
|
||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
|
||
|
||
class CustomWebsiteViewModel : ViewModel() { | ||
|
||
val urls = MutableLiveData<MutableList<String>>() | ||
|
||
fun addUrl(url: String) { | ||
val currentUrls = urls.value ?: ArrayList() | ||
currentUrls.add(url) | ||
urls.value = currentUrls | ||
} | ||
|
||
fun onItemRemoved(position: Int) { | ||
val currentList = urls.value ?: mutableListOf() | ||
if (position < currentList.size) { | ||
currentList.removeAt(position) | ||
urls.value = currentList | ||
} | ||
} | ||
|
||
fun updateUrlAt(position: Int, newUrl: String) { | ||
val currentList = urls.value ?: mutableListOf() | ||
if (position < currentList.size) { | ||
currentList[position] = newUrl | ||
urls.value = currentList | ||
} | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...observatory/ooniprobe/activity/customwebsites/adapter/CustomWebsiteRecyclerViewAdapter.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,54 @@ | ||
package org.openobservatory.ooniprobe.activity.customwebsites.adapter | ||
|
||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.core.widget.addTextChangedListener | ||
import androidx.recyclerview.widget.RecyclerView | ||
import org.openobservatory.ooniprobe.activity.customwebsites.CustomWebsiteViewModel | ||
import org.openobservatory.ooniprobe.databinding.EdittextUrlBinding | ||
|
||
|
||
class CustomWebsiteRecyclerViewAdapter( | ||
private val onItemRemovedListener: ItemRemovedListener, | ||
private val viewModel: CustomWebsiteViewModel, | ||
var items: MutableList<String> = mutableListOf() | ||
) : RecyclerView.Adapter<CustomWebsiteRecyclerViewAdapter.ViewHolder>() { | ||
|
||
override fun onCreateViewHolder( | ||
parent: ViewGroup, viewType: Int | ||
): ViewHolder { | ||
return ViewHolder( | ||
EdittextUrlBinding.inflate( | ||
LayoutInflater.from(parent.context), | ||
parent, | ||
false | ||
) | ||
) | ||
} | ||
|
||
override fun onBindViewHolder( | ||
holder: ViewHolder, position: Int | ||
) { | ||
holder.binding.editText.setText(items[position]) | ||
holder.binding.delete.visibility = if (items.size > 1) { | ||
View.VISIBLE | ||
} else { | ||
View.INVISIBLE | ||
} | ||
holder.binding.delete.setOnClickListener { | ||
onItemRemovedListener.onItemRemoved(holder.adapterPosition) | ||
} | ||
holder.binding.editText.addTextChangedListener { | ||
viewModel.updateUrlAt(position, it.toString()) | ||
} | ||
} | ||
|
||
override fun getItemCount(): Int = items.size | ||
|
||
class ViewHolder(val binding: EdittextUrlBinding) : RecyclerView.ViewHolder(binding.root) | ||
} | ||
|
||
interface ItemRemovedListener { | ||
fun onItemRemoved(position: Int) | ||
} |
75 changes: 0 additions & 75 deletions
75
app/src/main/java/org/openobservatory/ooniprobe/adapters/CustomWebsiteRecyclerViewAdapter.kt
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
app/src/main/java/org/openobservatory/ooniprobe/di/ActivityComponent.java
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
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