|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.autofill.impl.importing.takeout.webflow |
| 18 | + |
| 19 | +import android.os.Bundle |
| 20 | +import android.view.LayoutInflater |
| 21 | +import android.view.View |
| 22 | +import android.view.View.GONE |
| 23 | +import android.view.View.VISIBLE |
| 24 | +import android.view.ViewGroup |
| 25 | +import androidx.appcompat.widget.Toolbar |
| 26 | +import androidx.fragment.app.Fragment |
| 27 | +import com.duckduckgo.anvil.annotations.InjectWith |
| 28 | +import com.duckduckgo.autofill.impl.R |
| 29 | +import com.duckduckgo.autofill.impl.databinding.FragmentImportBookmarksResultBinding |
| 30 | +import com.duckduckgo.di.scopes.FragmentScope |
| 31 | + |
| 32 | +@InjectWith(FragmentScope::class) |
| 33 | +class ImportFinishedFragment : Fragment() { |
| 34 | + private var binding: FragmentImportBookmarksResultBinding? = null |
| 35 | + private var onDoneCallback: (() -> Unit)? = null |
| 36 | + |
| 37 | + override fun onCreateView( |
| 38 | + inflater: LayoutInflater, |
| 39 | + container: ViewGroup?, |
| 40 | + savedInstanceState: Bundle?, |
| 41 | + ): View { |
| 42 | + binding = FragmentImportBookmarksResultBinding.inflate(inflater, container, false) |
| 43 | + return binding!!.root |
| 44 | + } |
| 45 | + |
| 46 | + override fun onViewCreated( |
| 47 | + view: View, |
| 48 | + savedInstanceState: Bundle?, |
| 49 | + ) { |
| 50 | + super.onViewCreated(view, savedInstanceState) |
| 51 | + setupUi() |
| 52 | + setupToolbar() |
| 53 | + } |
| 54 | + |
| 55 | + override fun onDestroyView() { |
| 56 | + super.onDestroyView() |
| 57 | + binding = null |
| 58 | + } |
| 59 | + |
| 60 | + private fun setupUi() { |
| 61 | + val success = arguments?.getBoolean(ARG_BOOKMARK_IMPORT_SUCCESS, false) ?: false |
| 62 | + |
| 63 | + if (success) { |
| 64 | + setupUiForSuccess() |
| 65 | + } else { |
| 66 | + setupUiForFailure() |
| 67 | + } |
| 68 | + |
| 69 | + binding?.doneButton?.setOnClickListener { |
| 70 | + onDoneCallback?.invoke() |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private fun setupUiForSuccess() { |
| 75 | + val bookmarkCount = arguments?.getInt(ARG_BOOKMARK_COUNT_SUCCESS, 0) ?: 0 |
| 76 | + |
| 77 | + binding?.run { |
| 78 | + bookmarksCountText.setPrimaryText(getString(R.string.importBookmarksFromGoogleSuccessBookmarksCount, bookmarkCount)) |
| 79 | + |
| 80 | + importResultTitle.text = getString(R.string.importBookmarksSuccessTitle) |
| 81 | + importResultSubtitle.text = getString(R.string.importBookmarksSuccessSubtitle) |
| 82 | + importResultSubtitle.visibility = VISIBLE |
| 83 | + |
| 84 | + bookmarksCountText.visibility = VISIBLE |
| 85 | + bookmarksFailedText.visibility = GONE |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private fun setupUiForFailure() { |
| 90 | + val error = arguments?.getString(ARG_BOOKMARK_FAILURE_MESSAGE) ?: getString(R.string.importBookmarksErrorGenericMessage) |
| 91 | + |
| 92 | + binding?.run { |
| 93 | + bookmarksFailedText.setPrimaryText(error) |
| 94 | + |
| 95 | + importResultTitle.text = getString(R.string.importBookmarksErrorTitle) |
| 96 | + importResultSubtitle.visibility = GONE |
| 97 | + |
| 98 | + bookmarksCountText.visibility = GONE |
| 99 | + bookmarksFailedText.visibility = VISIBLE |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + fun setOnDoneCallback(callback: () -> Unit) { |
| 104 | + onDoneCallback = callback |
| 105 | + } |
| 106 | + |
| 107 | + private fun setupToolbar() { |
| 108 | + val toolbar = activity?.findViewById<Toolbar>(com.duckduckgo.mobile.android.R.id.toolbar) |
| 109 | + toolbar?.setNavigationOnClickListener { |
| 110 | + onDoneCallback?.invoke() |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + companion object { |
| 115 | + private const val ARG_BOOKMARK_COUNT_SUCCESS = "bookmark_import_count_success" |
| 116 | + private const val ARG_BOOKMARK_IMPORT_SUCCESS = "bookmark_import_success" |
| 117 | + private const val ARG_BOOKMARK_FAILURE_MESSAGE = "bookmark_import_failure_message" |
| 118 | + |
| 119 | + fun newInstanceSuccess(bookmarksImported: Int): ImportFinishedFragment = ImportFinishedFragment().apply { |
| 120 | + arguments = |
| 121 | + Bundle().apply { |
| 122 | + putInt(ARG_BOOKMARK_COUNT_SUCCESS, bookmarksImported) |
| 123 | + putBoolean(ARG_BOOKMARK_IMPORT_SUCCESS, true) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + fun newInstanceFailure(message: String): ImportFinishedFragment = ImportFinishedFragment().apply { |
| 128 | + arguments = |
| 129 | + Bundle().apply { |
| 130 | + putString(ARG_BOOKMARK_FAILURE_MESSAGE, message) |
| 131 | + putBoolean(ARG_BOOKMARK_IMPORT_SUCCESS, false) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments