Skip to content

Commit

Permalink
Add UI for editing ConditionData && enable / disable ConditionPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
renyuneyun committed May 9, 2018
1 parent 581828e commit 4799c93
Show file tree
Hide file tree
Showing 18 changed files with 474 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>

<activity android:name=".core.ui.edit.EditConditionActivity"></activity>
</application>

</manifest>
9 changes: 9 additions & 0 deletions app/src/main/java/ryey/easer/core/ui/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import ryey.easer.R;
import ryey.easer.core.data.storage.StorageHelper;
import ryey.easer.core.ui.edit.ConditionListFragment;
import ryey.easer.core.ui.edit.ProfileListFragment;
import ryey.easer.core.ui.edit.ScenarioListFragment;
import ryey.easer.core.ui.edit.ScriptListFragment;
Expand All @@ -55,6 +56,7 @@ public class MainActivity extends AppCompatActivity
private static final String FRAGMENT_PROFILE = "ryey.easer.FRAGMENT.PROFILE";
private static final String FRAGMENT_EVENT = "ryey.easer.FRAGMENT.EVENT";
private static final String FRAGMENT_SCENARIO = "ryey.easer.FRAGMENT.SCENARIO";
private static final String FRAGMENT_CONDITION = "ryey.easer.FRAGMENT.CONDITION";

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -157,6 +159,13 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
manager.beginTransaction()
.replace(R.id.content_main, fragment, FRAGMENT_SCENARIO)
.commit();
} else if (id == R.id.nav_condition) {
fragment = manager.findFragmentByTag(FRAGMENT_CONDITION);
if (fragment == null)
fragment = new ConditionListFragment();
manager.beginTransaction()
.replace(R.id.content_main, fragment, FRAGMENT_CONDITION)
.commit();
} else if (id == R.id.nav_about) {
Intent intent = new Intent(this, AboutActivity.class);
startActivity(intent);
Expand Down
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);
}
}
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);
}
}
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);
}
}
}
Loading

0 comments on commit 4799c93

Please sign in to comment.