Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Try to add real saving
Browse files Browse the repository at this point in the history
  • Loading branch information
sblatz committed May 6, 2019
1 parent c0a226c commit f3494a2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import org.mozilla.fenix.customtabs.CustomTabsIntegration
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.share
import org.mozilla.fenix.ext.urlToHost
import org.mozilla.fenix.ext.urlToTrimmedHost
import org.mozilla.fenix.home.sessioncontrol.Tab
import org.mozilla.fenix.lib.Do
import org.mozilla.fenix.mvi.ActionBusFactory
Expand Down Expand Up @@ -606,7 +606,7 @@ class BrowserFragment : Fragment(), BackHandler, CoroutineScope,

private fun showSaveToCollection() {
getSessionById()?.let {
val tabs = Tab(it.id, it.url, it.url.urlToHost(), it.title)
val tabs = Tab(it.id, it.url, it.url.urlToTrimmedHost(), it.title)
val viewModel = activity?.run {
ViewModelProviders.of(this).get(CreateCollectionViewModel::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ import kotlinx.android.synthetic.main.fragment_create_collection.view.*
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.ext.getRootView
import org.mozilla.fenix.home.sessioncontrol.TabCollection
import org.mozilla.fenix.mvi.ActionBusFactory
import org.mozilla.fenix.mvi.getAutoDisposeObservable
import org.mozilla.fenix.mvi.getManagedEmitter
import java.util.Random

class CreateCollectionFragment : DialogFragment() {
// Temporary callback. In the future we will just directly add the collection to the core session manager.
var onCollectionSaved: ((TabCollection) -> Unit)? = null
private lateinit var collectionCreationComponent: CollectionCreationComponent

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -91,6 +95,8 @@ class CreateCollectionFragment : DialogFragment() {
is CollectionCreationAction.SaveCollectionName -> {
showSavedSnackbar(it.tabs.size)
dismiss()
val newCollection = TabCollection(Random().nextInt(), it.name, it.tabs.toMutableList())
onCollectionSaved?.invoke(newCollection)
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/org/mozilla/fenix/ext/String.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ fun String.replace(pairs: Map<String, String>): String {
}

fun String?.urlToHost(): String {
return try {
val url = URL(this)
url.host
} catch (e: MalformedURLException) {
""
}
}

fun String?.urlToTrimmedHost(): String {
return try {
val url = URL(this)
val firstIndex = url.host.indexOfFirst { it == '.' } + 1
Expand Down
20 changes: 13 additions & 7 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.allowUndo
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.share
import org.mozilla.fenix.ext.urlToHost
import org.mozilla.fenix.ext.urlToTrimmedHost
import org.mozilla.fenix.home.sessioncontrol.Mode
import org.mozilla.fenix.home.sessioncontrol.SessionControlAction
import org.mozilla.fenix.home.sessioncontrol.SessionControlChange
Expand Down Expand Up @@ -260,7 +260,7 @@ class HomeFragment : Fragment(), CoroutineScope {
storedCollections.find { it.id == action.collection.id }?.apply { expanded = false }
}
is CollectionAction.Delete -> {
storedCollections.find { it.id == action.collection.id }?.let { storedCollections.clear() }
storedCollections.find { it.id == action.collection.id }?.let { storedCollections.remove(it) }
}
is CollectionAction.AddTab -> {
ItsNotBrokenSnack(context!!).showSnackbar(issueNumber = "1575")
Expand Down Expand Up @@ -370,7 +370,7 @@ class HomeFragment : Fragment(), CoroutineScope {
org.mozilla.fenix.home.sessioncontrol.Tab(
it.id,
it.url,
it.url.urlToHost(),
it.url.urlToTrimmedHost(),
it.title,
selected,
it.thumbnail
Expand Down Expand Up @@ -408,7 +408,7 @@ class HomeFragment : Fragment(), CoroutineScope {
org.mozilla.fenix.home.sessioncontrol.Tab(
it.id,
it.url,
it.url.urlToHost(),
it.url.urlToTrimmedHost(),
it.title,
selected,
it.thumbnail
Expand All @@ -420,7 +420,7 @@ class HomeFragment : Fragment(), CoroutineScope {

private fun showCollectionCreationFragment(selectedTabId: String?) {
val tabs = requireComponents.core.sessionManager.sessions
.map { Tab(it.id, it.url, it.url.urlToHost(), it.title) }
.map { Tab(it.id, it.url, it.url.urlToTrimmedHost(), it.title) }

val viewModel = activity?.run {
ViewModelProviders.of(this).get(CreateCollectionViewModel::class.java)
Expand All @@ -431,11 +431,17 @@ class HomeFragment : Fragment(), CoroutineScope {
viewModel?.selectedTabs = selectedSet
viewModel?.saveCollectionStep = SaveCollectionStep.SelectTabs

CreateCollectionFragment()
.show(
CreateCollectionFragment().also {
it.onCollectionSaved = {
storedCollections.add(it)
emitCollectionChange()
}

it.show(
requireActivity().supportFragmentManager,
CreateCollectionFragment.createCollectionTag
)
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ class CollectionViewHolder(
view.collection_description.text = titleList

if (collection.expanded) {
(view.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = EXPANDED_MARGIN
(view.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = 0
collection_title.setPadding(0, 0, 0, EXPANDED_PADDING)
view.background = ContextCompat.getDrawable(view.context, R.drawable.rounded_top_corners)
view.collection_description.visibility = View.GONE
view.expand_button.setImageDrawable(ContextCompat.getDrawable(view.context, R.drawable.ic_chevron_up))
Expand Down Expand Up @@ -161,7 +162,7 @@ class CollectionViewHolder(

companion object {
const val MAX_COLOR_INDEX = 4
const val EXPANDED_MARGIN = 8
const val EXPANDED_PADDING = 60
const val COLLAPSED_MARGIN = 12
const val LAYOUT_ID = R.layout.collection_home_list_row
const val maxTitleLength = 20
Expand Down

0 comments on commit f3494a2

Please sign in to comment.