-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
the template previewer was tightly coupled with the template editor, and that isn't a good design. To solve that, I decoupled the template previewer in two fragments: a view and a standalone page. All of the original style of the template previewer was fixed. I gave credits to BrayanDSO in TemplatePreviewerPage.kt because most of the code is his and I don't want the credits myself I also removed the duplicated TabLayout in the card editor because it didn't make sense to me.
- Loading branch information
1 parent
aa6022a
commit a33e6ea
Showing
14 changed files
with
269 additions
and
193 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
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
85 changes: 85 additions & 0 deletions
85
AnkiDroid/src/main/java/com/ichi2/anki/previewer/TemplatePreviewerPage.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,85 @@ | ||
/* | ||
* Copyright (c) 2024 Brayan Oliveira <[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.previewer | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.core.os.BundleCompat | ||
import androidx.core.os.bundleOf | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.commitNow | ||
import androidx.lifecycle.lifecycleScope | ||
import com.google.android.material.appbar.MaterialToolbar | ||
import com.google.android.material.tabs.TabLayout | ||
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener | ||
import com.ichi2.anki.R | ||
import com.ichi2.anki.previewer.TemplatePreviewerFragment.Companion.ARGS_KEY | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
|
||
/** | ||
* Container for [TemplatePreviewerFragment] that works as a standalone page | ||
* by including a toolbar and a TabLayout for changing the current template. | ||
*/ | ||
class TemplatePreviewerPage : Fragment(R.layout.template_previewer_container) { | ||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
view.findViewById<MaterialToolbar>(R.id.toolbar).setNavigationOnClickListener { | ||
requireActivity().onBackPressedDispatcher.onBackPressed() | ||
} | ||
|
||
val arguments = BundleCompat.getParcelable(requireArguments(), ARGS_KEY, TemplatePreviewerArguments::class.java)!! | ||
val fragment = TemplatePreviewerFragment.newInstance(arguments) | ||
childFragmentManager.commitNow { | ||
replace(R.id.fragment_container, fragment) | ||
} | ||
|
||
val viewModel = fragment.viewModel | ||
val tabLayout = view.findViewById<TabLayout>(R.id.tab_layout) | ||
|
||
lifecycleScope.launch { | ||
for (templateName in viewModel.getTemplateNames()) { | ||
tabLayout.addTab(tabLayout.newTab().setText(templateName)) | ||
} | ||
tabLayout.selectTab(tabLayout.getTabAt(viewModel.getCurrentTabIndex())) | ||
tabLayout.addOnTabSelectedListener(object : OnTabSelectedListener { | ||
override fun onTabSelected(tab: TabLayout.Tab) { | ||
Timber.v("Selected tab %d", tab.position) | ||
viewModel.onTabSelected(tab.position) | ||
} | ||
|
||
override fun onTabUnselected(tab: TabLayout.Tab) { | ||
// do nothing | ||
} | ||
|
||
override fun onTabReselected(tab: TabLayout.Tab) { | ||
// do nothing | ||
} | ||
}) | ||
} | ||
} | ||
|
||
companion object { | ||
fun getIntent(context: Context, arguments: TemplatePreviewerArguments): Intent { | ||
return CardViewerActivity.getIntent( | ||
context, | ||
TemplatePreviewerPage::class, | ||
bundleOf(ARGS_KEY to arguments) | ||
) | ||
} | ||
} | ||
} |
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
46 changes: 31 additions & 15 deletions
46
AnkiDroid/src/main/res/layout-xlarge/card_template_editor.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 |
---|---|---|
@@ -1,22 +1,38 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
<LinearLayout | ||
android:id="@+id/card_template_editor_xl_view" | ||
android:layout_height="match_parent" | ||
android:id="@+id/root_layout" | ||
xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:background="?android:attr/colorBackground" | ||
android:orientation="horizontal"> | ||
<include layout="@layout/card_template_editor_activity" | ||
android:layout_width="1dip" | ||
android:layout_weight="1" | ||
android:layout_height="match_parent"/> | ||
android:layout_height="match_parent"> | ||
|
||
<include layout="@layout/card_template_editor_top" | ||
android:id="@+id/template_editor_top" | ||
app:layout_constraintTop_toTopOf="parent"/> | ||
|
||
<include layout="@layout/card_template_editor_main" | ||
android:id="@+id/template_editor" | ||
android:layout_height="0dp" | ||
android:layout_width="0dp" | ||
app:layout_constraintTop_toBottomOf="@id/template_editor_top" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintEnd_toStartOf="@id/fragment_container"/> | ||
|
||
<androidx.fragment.app.FragmentContainerView | ||
android:id="@+id/template_previewer_fragment" | ||
android:layout_weight="1" | ||
android:layout_width="1dip" | ||
android:layout_height="match_parent"/> | ||
</LinearLayout> | ||
android:id="@+id/fragment_container" | ||
android:layout_height="0dp" | ||
android:layout_width="0dp" | ||
app:layout_constraintTop_toBottomOf="@id/template_editor_top" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintStart_toEndOf="@id/template_editor" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
/> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
|
||
</androidx.coordinatorlayout.widget.CoordinatorLayout> | ||
|
Oops, something went wrong.