-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UI for editing ConditionData && enable / disable ConditionPlugin
- Loading branch information
1 parent
581828e
commit 4799c93
Showing
18 changed files
with
474 additions
and
6 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
42 changes: 42 additions & 0 deletions
42
app/src/main/java/ryey/easer/core/ui/edit/ConditionListFragment.java
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,42 @@ | ||
/* | ||
* Copyright (c) 2016 - 2018 Rui Zhao <[email protected]> | ||
* | ||
* This file is part of Easer. | ||
* | ||
* Easer 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. | ||
* | ||
* Easer 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 Easer. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package ryey.easer.core.ui.edit; | ||
|
||
import android.content.Intent; | ||
|
||
import ryey.easer.R; | ||
import ryey.easer.core.data.storage.ConditionDataStorage; | ||
|
||
public class ConditionListFragment extends AbstractDataListFragment<ConditionDataStorage> { | ||
@Override | ||
protected String title() { | ||
return getString(R.string.title_condition); | ||
} | ||
|
||
@Override | ||
protected ConditionDataStorage retmStorage() { | ||
return ConditionDataStorage.getInstance(getContext()); | ||
} | ||
|
||
@Override | ||
protected Intent intentForEditDataActivity() { | ||
return new Intent(getContext(), EditConditionActivity.class); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
app/src/main/java/ryey/easer/core/ui/edit/ConditionPluginViewContainerFragment.java
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,103 @@ | ||
/* | ||
* Copyright (c) 2016 - 2018 Rui Zhao <[email protected]> | ||
* | ||
* This file is part of Easer. | ||
* | ||
* Easer 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. | ||
* | ||
* Easer 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 Easer. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package ryey.easer.core.ui.edit; | ||
|
||
import android.content.pm.PackageManager; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import ryey.easer.R; | ||
import ryey.easer.commons.plugindef.InvalidDataInputException; | ||
import ryey.easer.commons.plugindef.conditionplugin.ConditionData; | ||
import ryey.easer.commons.plugindef.conditionplugin.ConditionPlugin; | ||
import ryey.easer.plugins.PluginRegistry; | ||
|
||
public class ConditionPluginViewContainerFragment<T extends ConditionData> extends PluginViewContainerFragment<T> { | ||
|
||
private static final String EXTRA_PLUGIN = "plugin"; | ||
|
||
static <T extends ConditionData> ConditionPluginViewContainerFragment<T> createInstance(ConditionPlugin<T> plugin) { | ||
Bundle bundle = new Bundle(); | ||
bundle.putString(EXTRA_PLUGIN, plugin.id()); | ||
ConditionPluginViewContainerFragment<T> fragment = new ConditionPluginViewContainerFragment<>(); | ||
fragment.setArguments(bundle); | ||
return fragment; | ||
} | ||
|
||
@Override | ||
public void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
String plugin_id = getArguments().getString(EXTRA_PLUGIN); | ||
@SuppressWarnings("unchecked") ConditionPlugin<T> plugin = PluginRegistry.getInstance().condition().findPlugin(plugin_id); | ||
pluginViewFragment = plugin.view(); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||
View v = inflater.inflate(R.layout.fragment_pluginview_condition, container, false); | ||
getChildFragmentManager().beginTransaction() | ||
.replace(R.id.content_pluginview, pluginViewFragment) | ||
.commit(); | ||
return v; | ||
} | ||
|
||
@Override | ||
public void onStart() { | ||
super.onStart(); | ||
ConditionPlugin plugin = PluginRegistry.getInstance().condition().findPlugin(pluginViewFragment); | ||
//noinspection ConstantConditions | ||
if (!plugin.checkPermissions(getContext())) { | ||
setEnabled(false); | ||
//noinspection ConstantConditions | ||
plugin.requestPermissions(getActivity(), 1); | ||
} | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | ||
if (requestCode == 1) { | ||
for (int i = 0; i < grantResults.length; i++) { | ||
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) { | ||
return; | ||
} | ||
} | ||
setEnabled(true); | ||
} | ||
} | ||
|
||
/*** | ||
* {@inheritDoc} | ||
* Explicitly override and call back through to snooze compiler data type checking | ||
*/ | ||
@NonNull | ||
@Override | ||
T getData() throws InvalidDataInputException { | ||
return super.getData(); | ||
} | ||
|
||
private void setEnabled(boolean enabled) { | ||
pluginViewFragment.setEnabled(enabled); | ||
} | ||
} |
155 changes: 155 additions & 0 deletions
155
app/src/main/java/ryey/easer/core/ui/edit/ConditionPluginViewPager.java
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,155 @@ | ||
/* | ||
* Copyright (c) 2016 - 2018 Rui Zhao <[email protected]> | ||
* | ||
* This file is part of Easer. | ||
* | ||
* Easer 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. | ||
* | ||
* Easer 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 Easer. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package ryey.easer.core.ui.edit; | ||
|
||
import android.content.Context; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.app.Fragment; | ||
import android.support.v4.app.FragmentManager; | ||
import android.support.v4.app.FragmentStatePagerAdapter; | ||
import android.support.v4.view.ViewPager; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.util.AttributeSet; | ||
import android.util.SparseArray; | ||
import android.view.ViewGroup; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import ryey.easer.commons.plugindef.InvalidDataInputException; | ||
import ryey.easer.commons.plugindef.conditionplugin.ConditionData; | ||
import ryey.easer.commons.plugindef.conditionplugin.ConditionPlugin; | ||
import ryey.easer.plugins.PluginRegistry; | ||
|
||
public class ConditionPluginViewPager extends ViewPager { | ||
|
||
MyPagerAdapter mPagerAdapter; | ||
|
||
final List<ConditionPlugin> conditionPluginList = new ArrayList<>(); | ||
|
||
Integer initial_position = null; | ||
ConditionData initial_condition_data = null; | ||
|
||
public ConditionPluginViewPager(Context context) { | ||
super(context); | ||
} | ||
|
||
public ConditionPluginViewPager(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
void init(AppCompatActivity activity) { | ||
conditionPluginList.clear(); | ||
conditionPluginList.addAll(PluginRegistry.getInstance().condition().getEnabledPlugins(activity)); | ||
mPagerAdapter = new MyPagerAdapter(activity.getSupportFragmentManager(), getContext()); | ||
setAdapter(mPagerAdapter); | ||
} | ||
|
||
<T extends ConditionData> void setConditionData(T conditionData) { | ||
initial_condition_data = conditionData; | ||
int i = getPluginIndex(conditionData); | ||
initial_position = i; | ||
if (getCurrentItem() == i) { | ||
synchronized (this) { | ||
//noinspection unchecked | ||
ConditionPluginViewContainerFragment<T> fragment = mPagerAdapter.getRegisteredFragment(i); | ||
if (fragment != null) | ||
//noinspection unchecked | ||
fragment.fill((T) initial_condition_data); | ||
} | ||
} else { | ||
setCurrentItem(i); | ||
} | ||
} | ||
|
||
ConditionData getConditionData() throws InvalidDataInputException { | ||
return getConditionData(getCurrentItem()); | ||
} | ||
|
||
ConditionData getConditionData(int position) throws InvalidDataInputException { | ||
return mPagerAdapter.getRegisteredFragment(position).getData(); | ||
} | ||
|
||
private int getPluginIndex(ConditionData conditionData) { | ||
for (int i = 0; i < conditionPluginList.size(); i++) { | ||
if (conditionData.getClass() == conditionPluginList.get(i).dataFactory().dataClass()) | ||
return i; | ||
} | ||
throw new IllegalAccessError("Plugin not found???"); | ||
} | ||
|
||
class MyPagerAdapter extends FragmentStatePagerAdapter { | ||
|
||
SparseArray<ConditionPluginViewContainerFragment> registeredFragments = new SparseArray<>(); | ||
|
||
private final Context context; | ||
final String[] titles; | ||
|
||
public MyPagerAdapter(FragmentManager fm, Context context) { | ||
super(fm); | ||
this.context = context; | ||
titles = new String[conditionPluginList.size()]; | ||
for (int i = 0; i < conditionPluginList.size(); i++) { | ||
titles[i] = conditionPluginList.get(i).view().desc(getResources()); | ||
} | ||
} | ||
|
||
@Override | ||
public Fragment getItem(int position) { | ||
PluginViewContainerFragment fragment = ConditionPluginViewContainerFragment.createInstance( | ||
conditionPluginList.get(position)); | ||
return fragment; | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
return titles.length; | ||
} | ||
|
||
@Override | ||
public CharSequence getPageTitle(int position) { | ||
return titles[position]; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Object instantiateItem(ViewGroup container, int position) { | ||
ConditionPluginViewContainerFragment fragment = (ConditionPluginViewContainerFragment) super.instantiateItem(container, position); | ||
synchronized (ConditionPluginViewPager.this) { | ||
if ((initial_position != null) && (position == initial_position)) { | ||
//noinspection unchecked | ||
fragment.fill(initial_condition_data); | ||
} | ||
} | ||
registeredFragments.put(position, fragment); | ||
return fragment; | ||
} | ||
|
||
@Override | ||
public void destroyItem(ViewGroup container, int position, Object object) { | ||
registeredFragments.remove(position); | ||
super.destroyItem(container, position, object); | ||
} | ||
|
||
public ConditionPluginViewContainerFragment getRegisteredFragment(int position) { | ||
return registeredFragments.get(position); | ||
} | ||
} | ||
} |
Oops, something went wrong.