Attempt to Avoid crash on relaunch without book downloaded

This commit is contained in:
s-ayush2903 2020-09-11 15:52:54 +05:30
parent cef1236d08
commit 710c4ffa03
No known key found for this signature in database
GPG Key ID: B4341DD08B2371CB
3 changed files with 69 additions and 53 deletions

View File

@ -37,6 +37,15 @@
<option name="PACKAGES_TO_USE_STAR_IMPORTS"> <option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value /> <value />
</option> </option>
<option name="PACKAGES_IMPORT_LAYOUT">
<value>
<package name="" alias="false" withSubpackages="true" />
<package name="java" alias="false" withSubpackages="true" />
<package name="javax" alias="false" withSubpackages="true" />
<package name="kotlin" alias="false" withSubpackages="true" />
<package name="" alias="true" withSubpackages="true" />
</value>
</option>
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" /> <option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="2147483647" />
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" /> <option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="2147483647" />
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" /> <option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />

View File

@ -89,7 +89,6 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Objects;
import javax.inject.Inject; import javax.inject.Inject;
import kotlin.Unit; import kotlin.Unit;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -160,20 +159,10 @@ public abstract class CoreReaderFragment extends BaseFragment
@BindView(R2.id.toolbar) @BindView(R2.id.toolbar)
protected Toolbar toolbar; protected Toolbar toolbar;
@BindView(R2.id.activity_main_back_to_top_fab)
FloatingActionButton backToTopButton;
@BindView(R2.id.activity_main_button_stop_tts)
Button stopTTSButton;
@BindView(R2.id.activity_main_button_pause_tts)
Button pauseTTSButton;
@BindView(R2.id.activity_main_tts_controls)
Group TTSControls;
@BindView(R2.id.fragment_main_app_bar) @BindView(R2.id.fragment_main_app_bar)
protected AppBarLayout toolbarContainer; protected AppBarLayout toolbarContainer;
@BindView(R2.id.main_fragment_progress_view) @BindView(R2.id.main_fragment_progress_view)
protected ContentLoadingProgressBar progressBar; protected ContentLoadingProgressBar progressBar;
@BindView(R2.id.activity_main_fullscreen_button)
ImageButton exitFullscreenButton;
@BindView(R2.id.navigation_fragment_main_drawer_layout) @BindView(R2.id.navigation_fragment_main_drawer_layout)
protected DrawerLayout drawerLayout; protected DrawerLayout drawerLayout;
protected NavigationView tableDrawerRightContainer; protected NavigationView tableDrawerRightContainer;
@ -181,31 +170,16 @@ public abstract class CoreReaderFragment extends BaseFragment
protected FrameLayout contentFrame; protected FrameLayout contentFrame;
@BindView(R2.id.bottom_toolbar) @BindView(R2.id.bottom_toolbar)
protected BottomAppBar bottomToolbar; protected BottomAppBar bottomToolbar;
@BindView(R2.id.bottom_toolbar_bookmark)
ImageView bottomToolbarBookmark;
@BindView(R2.id.bottom_toolbar_arrow_back)
ImageView bottomToolbarArrowBack;
@BindView(R2.id.bottom_toolbar_arrow_forward)
ImageView bottomToolbarArrowForward;
@BindView(R2.id.tab_switcher_recycler_view)
RecyclerView tabRecyclerView;
@BindView(R2.id.activity_main_tab_switcher) @BindView(R2.id.activity_main_tab_switcher)
protected View tabSwitcherRoot; protected View tabSwitcherRoot;
@BindView(R2.id.tab_switcher_close_all_tabs) @BindView(R2.id.tab_switcher_close_all_tabs)
protected FloatingActionButton closeAllTabsButton; protected FloatingActionButton closeAllTabsButton;
@BindView(R2.id.snackbar_root)
CoordinatorLayout snackbarRoot;
@BindView(R2.id.fullscreen_video_container) @BindView(R2.id.fullscreen_video_container)
protected ViewGroup videoView; protected ViewGroup videoView;
@BindView(R2.id.go_to_library_button_no_open_book) @BindView(R2.id.go_to_library_button_no_open_book)
protected Button noOpenBookButton; protected Button noOpenBookButton;
@BindView(R2.id.no_open_book_text)
TextView noOpenBookText;
@BindView(R2.id.activity_main_root) @BindView(R2.id.activity_main_root)
protected View activityMainRoot; protected View activityMainRoot;
@Inject
StorageObserver storageObserver;
@Inject @Inject
protected SharedPreferenceUtil sharedPreferenceUtil; protected SharedPreferenceUtil sharedPreferenceUtil;
@Inject @Inject
@ -222,11 +196,37 @@ public abstract class CoreReaderFragment extends BaseFragment
protected DialogShower alertDialogShower; protected DialogShower alertDialogShower;
@Inject @Inject
protected NightModeViewPainter painter; protected NightModeViewPainter painter;
protected int currentWebViewIndex = 0;
protected ActionBar actionBar;
protected MainMenu mainMenu;
@BindView(R2.id.activity_main_back_to_top_fab)
FloatingActionButton backToTopButton;
@BindView(R2.id.activity_main_button_stop_tts)
Button stopTTSButton;
@BindView(R2.id.activity_main_button_pause_tts)
Button pauseTTSButton;
@BindView(R2.id.activity_main_tts_controls)
Group TTSControls;
@BindView(R2.id.activity_main_fullscreen_button)
ImageButton exitFullscreenButton;
@BindView(R2.id.bottom_toolbar_bookmark)
ImageView bottomToolbarBookmark;
@BindView(R2.id.bottom_toolbar_arrow_back)
ImageView bottomToolbarArrowBack;
@BindView(R2.id.bottom_toolbar_arrow_forward)
ImageView bottomToolbarArrowForward;
@BindView(R2.id.tab_switcher_recycler_view)
RecyclerView tabRecyclerView;
@BindView(R2.id.snackbar_root)
CoordinatorLayout snackbarRoot;
@BindView(R2.id.no_open_book_text)
TextView noOpenBookText;
@Inject
StorageObserver storageObserver;
@Inject @Inject
MainRepositoryActions repositoryActions; MainRepositoryActions repositoryActions;
@Inject @Inject
ExternalLinkOpener externalLinkOpener; ExternalLinkOpener externalLinkOpener;
private CountDownTimer hideBackToTopTimer; private CountDownTimer hideBackToTopTimer;
private List<TableDrawerAdapter.DocumentSection> documentSections; private List<TableDrawerAdapter.DocumentSection> documentSections;
private boolean isBackToTopEnabled = false; private boolean isBackToTopEnabled = false;
@ -236,16 +236,13 @@ public abstract class CoreReaderFragment extends BaseFragment
private KiwixTextToSpeech tts; private KiwixTextToSpeech tts;
private CompatFindActionModeCallback compatCallback; private CompatFindActionModeCallback compatCallback;
private TabsAdapter tabsAdapter; private TabsAdapter tabsAdapter;
protected int currentWebViewIndex = 0;
private File file; private File file;
private ActionMode actionMode = null; private ActionMode actionMode = null;
private KiwixWebView tempWebViewForUndo; private KiwixWebView tempWebViewForUndo;
private File tempZimFileForUndo; private File tempZimFileForUndo;
private boolean isFirstRun; private boolean isFirstRun;
protected ActionBar actionBar;
private TableDrawerAdapter tableDrawerAdapter; private TableDrawerAdapter tableDrawerAdapter;
private RecyclerView tableDrawerRight; private RecyclerView tableDrawerRight;
protected MainMenu mainMenu;
private ItemTouchHelper.Callback tabCallback; private ItemTouchHelper.Callback tabCallback;
private Disposable bookmarkingDisposable; private Disposable bookmarkingDisposable;
private boolean isBookmarked; private boolean isBookmarked;
@ -453,8 +450,10 @@ public abstract class CoreReaderFragment extends BaseFragment
private void setupDocumentParser() { private void setupDocumentParser() {
documentParser = new DocumentParser(new DocumentParser.SectionsListener() { documentParser = new DocumentParser(new DocumentParser.SectionsListener() {
@Override @Override
public void sectionsLoaded(String title, List<TableDrawerAdapter.DocumentSection> sections) { public void sectionsLoaded(String title,
List<? extends TableDrawerAdapter.DocumentSection> sections) {
if (isAdded()) { if (isAdded()) {
documentSections.addAll(sections); documentSections.addAll(sections);
tableDrawerAdapter.setTitle(title); tableDrawerAdapter.setTitle(title);
@ -754,15 +753,10 @@ public abstract class CoreReaderFragment extends BaseFragment
} }
private KiwixWebView initalizeWebView(String url) { private KiwixWebView initalizeWebView(String url) {
//if(requireContext() != null) {
AttributeSet attrs = StyleUtils.getAttributes(requireActivity(), R.xml.webview); AttributeSet attrs = StyleUtils.getAttributes(requireActivity(), R.xml.webview);
KiwixWebView webView = createWebView(attrs); KiwixWebView webView = createWebView(attrs);
loadUrl(url, webView); loadUrl(url, webView);
return webView; return webView;
//} else{
// Log.e("KIWIX", "initalizeWebView: CONTEXXT NOT FOUND, NULL");
// return null;
//}
} }
@NotNull protected ToolbarScrollingKiwixWebView createWebView(AttributeSet attrs) { @NotNull protected ToolbarScrollingKiwixWebView createWebView(AttributeSet attrs) {
@ -836,18 +830,20 @@ public abstract class CoreReaderFragment extends BaseFragment
protected void selectTab(int position) { protected void selectTab(int position) {
currentWebViewIndex = position; currentWebViewIndex = position;
contentFrame.removeAllViews(); //if (contentFrame != null)
KiwixWebView webView = safelyGetWebView(position); contentFrame.removeAllViews();
if (webView.getParent() != null) { KiwixWebView webView = safelyGetWebView(position);
((ViewGroup) webView.getParent()).removeView(webView); if (webView.getParent() != null) {
} ((ViewGroup) webView.getParent()).removeView(webView);
contentFrame.addView(webView); }
tabsAdapter.setSelected(currentWebViewIndex); contentFrame.addView(webView);
updateBottomToolbarVisibility(); tabsAdapter.setSelected(currentWebViewIndex);
loadPrefs(); updateBottomToolbarVisibility();
updateUrlProcessor(); loadPrefs();
updateTableOfContents(); updateUrlProcessor();
updateTitle(); updateTableOfContents();
updateTitle();
//}
} }
protected KiwixWebView safelyGetWebView(int position) { protected KiwixWebView safelyGetWebView(int position) {
@ -1313,7 +1309,7 @@ public abstract class CoreReaderFragment extends BaseFragment
} }
private void setUpWebViewWithTextToSpeech() { private void setUpWebViewWithTextToSpeech() {
tts.initWebView(getCurrentWebView()); if (tts != null) tts.initWebView(getCurrentWebView());
} }
@OnClick(R2.id.activity_main_back_to_top_fab) @OnClick(R2.id.activity_main_back_to_top_fab)

View File

@ -32,8 +32,8 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil;
private final int toolbarHeight = DimenUtils.getToolbarHeight(getContext()); private final int toolbarHeight = DimenUtils.getToolbarHeight(getContext());
private final View toolbarView; private final View toolbarView;
private final View bottomBarView; private final View bottomBarView;
private View parentNavigationBar = null;
private final SharedPreferenceUtil sharedPreferenceUtil; private final SharedPreferenceUtil sharedPreferenceUtil;
private View parentNavigationBar = null;
private float startY; private float startY;
public ToolbarScrollingKiwixWebView(Context context, WebViewCallback callback, AttributeSet attrs, public ToolbarScrollingKiwixWebView(Context context, WebViewCallback callback, AttributeSet attrs,
@ -51,6 +51,17 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil;
this.toolbarView = toolbarView; this.toolbarView = toolbarView;
this.bottomBarView = bottomBarView; this.bottomBarView = bottomBarView;
this.sharedPreferenceUtil = sharedPreferenceUtil; this.sharedPreferenceUtil = sharedPreferenceUtil;
if (toolbarView != null) {
fixInitalScrollingIssue();
}
}
/**
* The webview needs to be scrolled with 0 to not be slightly hidden on startup.
* See https://github.com/kiwix/kiwix-android/issues/2304 for issue description.
*/
private void fixInitalScrollingIssue() {
moveToolbar(0);
} }
private boolean moveToolbar(int scrollDelta) { private boolean moveToolbar(int scrollDelta) {