From d047d075d556cc37a946e2dfa395335924d6cde9 Mon Sep 17 00:00:00 2001 From: Gabor Keszthelyi Date: Mon, 8 Jan 2018 22:49:05 +0100 Subject: [PATCH] Clean up TaskGroupPagerAdapter instantiation exception handling and some unused code in TaskListActivity. #621 (#622) --- .../java/org/dmfs/tasks/TaskListActivity.java | 39 +------------ .../java/org/dmfs/tasks/utils/Unchecked.java | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 opentasks/src/main/java/org/dmfs/tasks/utils/Unchecked.java diff --git a/opentasks/src/main/java/org/dmfs/tasks/TaskListActivity.java b/opentasks/src/main/java/org/dmfs/tasks/TaskListActivity.java index 8bd8ead9b..d00963542 100644 --- a/opentasks/src/main/java/org/dmfs/tasks/TaskListActivity.java +++ b/opentasks/src/main/java/org/dmfs/tasks/TaskListActivity.java @@ -27,7 +27,6 @@ import android.os.Handler; import android.support.annotation.ColorInt; import android.support.design.widget.AppBarLayout; -import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; @@ -39,7 +38,6 @@ import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView.OnQueryTextListener; import android.support.v7.widget.Toolbar; -import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -63,10 +61,7 @@ import org.dmfs.tasks.utils.BaseActivity; import org.dmfs.tasks.utils.ExpandableGroupDescriptor; import org.dmfs.tasks.utils.SearchHistoryHelper; -import org.dmfs.xmlobjects.pull.XmlObjectPullParserException; -import org.xmlpull.v1.XmlPullParserException; - -import java.io.IOException; +import org.dmfs.tasks.utils.Unchecked; /** @@ -142,12 +137,6 @@ public class TaskListActivity extends BaseActivity implements TaskListFragment.C private boolean mAutoExpandSearchView = false; - /** - * Indicates that the activity switched to detail view due to rotation. - **/ - @Retain - private boolean mSwitchedToDetail = false; - /** * The Uri of the task to display/highlight in the list view. **/ @@ -181,8 +170,6 @@ public class TaskListActivity extends BaseActivity implements TaskListFragment.C **/ private boolean mTransientState = false; - private CollapsingToolbarLayout mToolbarLayout; - private AppBarLayout mAppBarLayout; private FloatingActionButton mFloatingActionButton; @@ -191,7 +178,6 @@ public class TaskListActivity extends BaseActivity implements TaskListFragment.C @Override protected void onCreate(Bundle savedInstanceState) { - Log.d(TAG, "onCreate called again"); super.onCreate(savedInstanceState); // check for single pane activity change @@ -206,7 +192,6 @@ protected void onCreate(Bundle savedInstanceState) Intent viewTaskIntent = new Intent(Intent.ACTION_VIEW); viewTaskIntent.setData(mSelectedTaskUri); startActivity(viewTaskIntent); - mSwitchedToDetail = true; mShouldSwitchToDetail = false; mTransientState = true; } @@ -251,26 +236,7 @@ protected void onCreate(Bundle savedInstanceState) new ByList(mAuthority, this), new ByDueDate(mAuthority), new ByStartDate(mAuthority), new ByPriority(mAuthority, this), new ByProgress(mAuthority), new BySearch(mAuthority, mSearchHistoryHelper) }; - // set up pager adapter - try - { - mPagerAdapter = new TaskGroupPagerAdapter(getSupportFragmentManager(), mGroupingFactories, this, R.xml.listview_tabs); - } - catch (XmlPullParserException e) - { - // TODO Automatisch generierter Erfassungsblock - e.printStackTrace(); - } - catch (IOException e) - { - // TODO Automatisch generierter Erfassungsblock - e.printStackTrace(); - } - catch (XmlObjectPullParserException e) - { - // TODO Automatisch generierter Erfassungsblock - e.printStackTrace(); - } + mPagerAdapter = new Unchecked<>(() -> new TaskGroupPagerAdapter(getSupportFragmentManager(), mGroupingFactories, this, R.xml.listview_tabs)).value(); // Setup ViewPager mPagerAdapter.setTwoPaneLayout(mTwoPane); @@ -433,7 +399,6 @@ else if (forceReload) Intent detailIntent = new Intent(Intent.ACTION_VIEW); detailIntent.setData(uri); startActivity(detailIntent); - mSwitchedToDetail = true; mShouldSwitchToDetail = false; } } diff --git a/opentasks/src/main/java/org/dmfs/tasks/utils/Unchecked.java b/opentasks/src/main/java/org/dmfs/tasks/utils/Unchecked.java new file mode 100644 index 000000000..ef06267e8 --- /dev/null +++ b/opentasks/src/main/java/org/dmfs/tasks/utils/Unchecked.java @@ -0,0 +1,55 @@ +/* + * Copyright 2018 dmfs GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.dmfs.tasks.utils; + +import org.dmfs.jems.single.Single; + + +/** + * 'Unchecks' an Exception, i.e. turns a {@link Fragile} into a {@link Single} by rethrowing + * the possible {@link Exception} as {@link RuntimeException}. + *

+ * Note: This should be used with care for obvious reasons, only at appropriate places. + * + * @author Gabor Keszthelyi + * @deprecated use it from jems when available + */ +@Deprecated +public final class Unchecked implements Single +{ + private final Fragile mDelegate; + + + public Unchecked(Fragile delegate) + { + mDelegate = delegate; + } + + + @Override + public T value() + { + try + { + return mDelegate.value(); + } + catch (Exception e) + { + throw new RuntimeException("Exception in Unchecked", e); + } + } +}