Fragments

This commit is contained in:
Sergey Solovyev 2012-09-26 22:50:02 +04:00
parent 5371dbaac1
commit fe4ba72c7a
20 changed files with 266 additions and 154 deletions

View File

@ -15,6 +15,6 @@
--> -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#1f1f1f" android:endColor="#1f1f1f"/> <solid android:color="@color/metro_button_background"/>
<corners android:radius="1dp" /> <corners android:radius="1dp" />
</shape> </shape>

View File

@ -20,6 +20,7 @@
a:layout_width="0dp" a:layout_width="0dp"
a:layout_weight="1" a:layout_weight="1"
a:orientation="vertical" a:orientation="vertical"
style="?fragmentLayoutStyle"
a:layout_gravity="center"> a:layout_gravity="center">
<LinearLayout a:id="@+id/editorContainer" <LinearLayout a:id="@+id/editorContainer"
@ -72,10 +73,10 @@
</LinearLayout> </LinearLayout>
<LinearLayout a:id="@+id/main_second_pane" <LinearLayout a:id="@+id/main_second_pane"
a:orientation="vertical" a:orientation="vertical"
a:layout_height="match_parent" a:layout_height="match_parent"
style="?secondPaneStyle"
a:layout_width="0dp" a:layout_width="0dp"
a:layout_weight="1"/> a:layout_weight="1"/>

View File

@ -1,15 +1,24 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="match_parent" a:layout_height="match_parent"
a:orientation="vertical"> style="?fragmentLayoutStyle"
a:orientation="vertical">
<include layout="@layout/ad"/>
<TextView a:id="@+id/fragmentTitle"
<ListView a:layout_height="wrap_content"
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="match_parent" style="?fragmentTitleStyle"/>
a:id="@android:id/list"/>
<include layout="@layout/ad"/>
<ListView
a:layout_width="match_parent"
a:layout_height="match_parent"
a:id="@android:id/list"
a:divider="@null"
a:dividerHeight="0dp"
a:cacheColorHint="@android:color/transparent"/>
</LinearLayout> </LinearLayout>

View File

@ -5,5 +5,8 @@
<attr name="controlButtonStyle" format="reference" /> <attr name="controlButtonStyle" format="reference" />
<attr name="controlImageButtonStyle" format="reference" /> <attr name="controlImageButtonStyle" format="reference" />
<attr name="operationButtonStyle" format="reference" /> <attr name="operationButtonStyle" format="reference" />
<attr name="fragmentLayoutStyle" format="reference" />
<attr name="secondPaneStyle" format="reference" />
<attr name="fragmentTitleStyle" format="reference" />
</resources> </resources>

View File

@ -13,4 +13,6 @@
<color name="display_error_text_color">#ff393939</color> <color name="display_error_text_color">#ff393939</color>
<color name="selected_angle_unit_text_color">#ffffff99</color> <color name="selected_angle_unit_text_color">#ffffff99</color>
<color name="default_background">#ff000000</color> <color name="default_background">#ff000000</color>
<color name="second_pane_background">#ff1f1f1f</color>
<color name="metro_button_background">#ff1f1f1f</color>
</resources> </resources>

View File

@ -11,7 +11,7 @@
<item name="android:paddingTop">6dp</item> <item name="android:paddingTop">6dp</item>
<item name="android:paddingLeft">6dp</item> <item name="android:paddingLeft">6dp</item>
<item name="android:paddingRight">6dp</item> <item name="android:paddingRight">6dp</item>
<item name="android:paddingBottom">3dp</item> <item name="android:paddingBottom">0dp</item>
<item name="android:textStyle">bold</item> <item name="android:textStyle">bold</item>
<item name="android:textColor">@android:color/white</item> <item name="android:textColor">@android:color/white</item>
</style> </style>
@ -20,7 +20,7 @@
<item name="android:paddingTop">0dp</item> <item name="android:paddingTop">0dp</item>
<item name="android:paddingLeft">6dp</item> <item name="android:paddingLeft">6dp</item>
<item name="android:paddingRight">6dp</item> <item name="android:paddingRight">6dp</item>
<item name="android:paddingBottom">6dp</item> <item name="android:paddingBottom">0dp</item>
<item name="android:textColor">#bebebe</item> <item name="android:textColor">#bebebe</item>
</style> </style>

View File

@ -20,10 +20,30 @@
<item name="android:padding">6dp</item> <item name="android:padding">6dp</item>
</style> </style>
<style name="default_theme"> <style name="default_second_pane_style">
<item name="android:background">@color/second_pane_background</item>
<item name="android:layout_margin">5dp</item>
</style>
<style name="default_fragment_title_style">
<item name="android:textColor">@android:color/white</item>
<item name="android:textSize">25sp</item>
</style>
<style name="default_fragment_layout_style">
<item name="android:layout_marginBottom">5dp</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:layout_marginLeft">5dp</item>
<item name="android:layout_marginRight">0dp</item>
</style>
<style name="default_theme" parent="Theme.Sherlock">
<item name="digitButtonStyle">@style/default_digit_button_style</item> <item name="digitButtonStyle">@style/default_digit_button_style</item>
<item name="controlButtonStyle">@style/default_control_button_style</item> <item name="controlButtonStyle">@style/default_control_button_style</item>
<item name="controlImageButtonStyle">@style/default_control_image_button_style</item> <item name="controlImageButtonStyle">@style/default_control_image_button_style</item>
<item name="operationButtonStyle">@style/default_operation_button_style</item> <item name="operationButtonStyle">@style/default_operation_button_style</item>
<item name="fragmentLayoutStyle">@style/default_fragment_layout_style</item>
<item name="secondPaneStyle">@style/default_second_pane_style</item>
<item name="fragmentTitleStyle">@style/default_fragment_title_style</item>
</style> </style>
</resources> </resources>

View File

@ -23,7 +23,7 @@
<item name="android:padding">6dp</item> <item name="android:padding">6dp</item>
</style> </style>
<style name="light_blue_theme"> <style name="light_blue_theme" parent="default_theme">
<item name="digitButtonStyle">@style/light_blue_digit_button_style</item> <item name="digitButtonStyle">@style/light_blue_digit_button_style</item>
<item name="controlButtonStyle">@style/light_blue_control_button_style</item> <item name="controlButtonStyle">@style/light_blue_control_button_style</item>
<item name="controlImageButtonStyle">@style/light_blue_control_image_button_style</item> <item name="controlImageButtonStyle">@style/light_blue_control_image_button_style</item>

View File

@ -16,7 +16,7 @@
<item name="android:padding">6dp</item> <item name="android:padding">6dp</item>
</style> </style>
<style name="metro_blue_theme" parent="Theme.Sherlock"> <style name="metro_blue_theme" parent="default_theme">
<item name="digitButtonStyle">@style/metro_digit_button_style</item> <item name="digitButtonStyle">@style/metro_digit_button_style</item>
<item name="controlButtonStyle">@style/metro_control_button_style</item> <item name="controlButtonStyle">@style/metro_control_button_style</item>
<item name="controlImageButtonStyle">@style/metro_control_image_button_style</item> <item name="controlImageButtonStyle">@style/metro_control_image_button_style</item>

View File

@ -1,26 +1,26 @@
<resources> <resources>
<style name="metro_digit_button_style" parent="button_style"> <style name="metro_digit_button_style" parent="button_style">
<item name="android:background">@drawable/metro_button_dark</item> <item name="android:background">@drawable/metro_button_dark</item>
</style> </style>
<style name="metro_control_button_style" parent="metro_digit_button_style"> <style name="metro_control_button_style" parent="metro_digit_button_style">
<item name="android:background">@drawable/metro_button_light</item> <item name="android:background">@drawable/metro_button_light</item>
</style> </style>
<style name="metro_green_operation_button_style" parent="metro_digit_button_style"> <style name="metro_green_operation_button_style" parent="metro_digit_button_style">
<item name="android:background">@drawable/metro_button_green</item> <item name="android:background">@drawable/metro_button_green</item>
</style> </style>
<style name="metro_control_image_button_style" parent="metro_control_button_style"> <style name="metro_control_image_button_style" parent="metro_control_button_style">
<item name="android:padding">6dp</item> <item name="android:padding">6dp</item>
</style> </style>
<style name="metro_green_theme"> <style name="metro_green_theme" parent="default_theme">
<item name="digitButtonStyle">@style/metro_digit_button_style</item> <item name="digitButtonStyle">@style/metro_digit_button_style</item>
<item name="controlButtonStyle">@style/metro_control_button_style</item> <item name="controlButtonStyle">@style/metro_control_button_style</item>
<item name="controlImageButtonStyle">@style/metro_control_image_button_style</item> <item name="controlImageButtonStyle">@style/metro_control_image_button_style</item>
<item name="operationButtonStyle">@style/metro_green_operation_button_style</item> <item name="operationButtonStyle">@style/metro_green_operation_button_style</item>
</style> </style>
</resources> </resources>

View File

@ -1,26 +1,26 @@
<resources> <resources>
<style name="metro_digit_button_style" parent="button_style"> <style name="metro_digit_button_style" parent="button_style">
<item name="android:background">@drawable/metro_button_dark</item> <item name="android:background">@drawable/metro_button_dark</item>
</style> </style>
<style name="metro_control_button_style" parent="metro_digit_button_style"> <style name="metro_control_button_style" parent="metro_digit_button_style">
<item name="android:background">@drawable/metro_button_light</item> <item name="android:background">@drawable/metro_button_light</item>
</style> </style>
<style name="metro_purple_operation_button_style" parent="metro_digit_button_style"> <style name="metro_purple_operation_button_style" parent="metro_digit_button_style">
<item name="android:background">@drawable/metro_button_purple</item> <item name="android:background">@drawable/metro_button_purple</item>
</style> </style>
<style name="metro_control_image_button_style" parent="metro_control_button_style"> <style name="metro_control_image_button_style" parent="metro_control_button_style">
<item name="android:padding">6dp</item> <item name="android:padding">6dp</item>
</style> </style>
<style name="metro_purple_theme"> <style name="metro_purple_theme" parent="default_theme">
<item name="digitButtonStyle">@style/metro_digit_button_style</item> <item name="digitButtonStyle">@style/metro_digit_button_style</item>
<item name="controlButtonStyle">@style/metro_control_button_style</item> <item name="controlButtonStyle">@style/metro_control_button_style</item>
<item name="controlImageButtonStyle">@style/metro_control_image_button_style</item> <item name="controlImageButtonStyle">@style/metro_control_image_button_style</item>
<item name="operationButtonStyle">@style/metro_purple_operation_button_style</item> <item name="operationButtonStyle">@style/metro_purple_operation_button_style</item>
</style> </style>
</resources> </resources>

View File

@ -22,7 +22,7 @@
<item name="android:padding">6dp</item> <item name="android:padding">6dp</item>
</style> </style>
<style name="violet_theme"> <style name="violet_theme" parent="default_theme">
<item name="digitButtonStyle">@style/violet_digit_button_style</item> <item name="digitButtonStyle">@style/violet_digit_button_style</item>
<item name="controlButtonStyle">@style/violet_control_button_style</item> <item name="controlButtonStyle">@style/violet_control_button_style</item>
<item name="controlImageButtonStyle">@style/violet_control_image_button_style</item> <item name="controlImageButtonStyle">@style/violet_control_image_button_style</item>

View File

@ -64,7 +64,7 @@ public class CalculatorActivity extends SherlockFragmentActivity implements Font
final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences); final CalculatorPreferences.Gui.Layout layout = CalculatorPreferences.Gui.layout.getPreferenceNoError(preferences);
activityHelper = CalculatorApplication.getInstance().createCalculatorHistoryHelper(layout.getLayoutId(), TAG); activityHelper = CalculatorApplication.getInstance().createActivityHelper(layout.getLayoutId(), TAG);
activityHelper.logDebug("onCreate"); activityHelper.logDebug("onCreate");
activityHelper.onCreate(this, savedInstanceState); activityHelper.onCreate(this, savedInstanceState);

View File

@ -111,7 +111,12 @@ public class CalculatorApplication extends android.app.Application {
} }
@NotNull @NotNull
public CalculatorActivityHelper createCalculatorHistoryHelper(int layoutResId, @NotNull String logTag) { public CalculatorActivityHelper createActivityHelper(int layoutResId, @NotNull String logTag) {
return new CalculatorActivityHelperImpl(layoutResId, logTag); return new CalculatorActivityHelperImpl(layoutResId, logTag);
} }
@NotNull
public CalculatorFragmentHelper createFragmentHelper() {
return new CalculatorFragmentHelperImpl();
}
} }

View File

@ -0,0 +1,14 @@
package org.solovyev.android.calculator;
import android.support.v4.app.Fragment;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 9/26/12
* Time: 10:14 PM
*/
public interface CalculatorFragmentHelper {
boolean isPane(@NotNull Fragment fragment);
}

View File

@ -0,0 +1,17 @@
package org.solovyev.android.calculator;
import android.support.v4.app.Fragment;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 9/26/12
* Time: 10:14 PM
*/
public class CalculatorFragmentHelperImpl implements CalculatorFragmentHelper {
@Override
public boolean isPane(@NotNull Fragment fragment) {
return fragment.getActivity() instanceof CalculatorActivity;
}
}

View File

@ -15,6 +15,7 @@ import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockListFragment; import com.actionbarsherlock.app.SherlockListFragment;
import com.google.ads.AdView; import com.google.ads.AdView;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -43,6 +44,14 @@ import java.util.List;
*/ */
public abstract class AbstractCalculatorHistoryFragment extends SherlockListFragment implements CalculatorEventListener { public abstract class AbstractCalculatorHistoryFragment extends SherlockListFragment implements CalculatorEventListener {
/*
**********************************************************************
*
* CONSTANTS
*
**********************************************************************
*/
@NotNull @NotNull
private static final String TAG = "CalculatorHistoryFragment"; private static final String TAG = "CalculatorHistoryFragment";
@ -61,6 +70,14 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
} }
}; };
/*
**********************************************************************
*
* FIELDS
*
**********************************************************************
*/
@NotNull @NotNull
private ArrayAdapter<CalculatorHistoryState> adapter; private ArrayAdapter<CalculatorHistoryState> adapter;
@ -68,10 +85,15 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
@Nullable @Nullable
private AdView adView; private AdView adView;
@NotNull
private CalculatorFragmentHelper fragmentHelper;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
fragmentHelper = CalculatorApplication.getInstance().createFragmentHelper();
logDebug("onCreate"); logDebug("onCreate");
} }
@ -92,6 +114,13 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
logDebug("onViewCreated"); logDebug("onViewCreated");
final TextView fragmentTitle = (TextView) view.findViewById(R.id.fragmentTitle);
if (!fragmentHelper.isPane(this)) {
fragmentTitle.setVisibility(View.GONE);
} else {
fragmentTitle.setText(this.getString(getFragmentTitleResId()).toUpperCase());
}
adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), R.id.history_item, new ArrayList<CalculatorHistoryState>()); adapter = new HistoryArrayAdapter(this.getActivity(), getItemLayoutId(), R.id.history_item, new ArrayList<CalculatorHistoryState>());
setListAdapter(adapter); setListAdapter(adapter);
@ -143,7 +172,9 @@ public abstract class AbstractCalculatorHistoryFragment extends SherlockListFrag
adView = AdsController.getInstance().inflateAd(this.getActivity(), (ViewGroup)view.findViewById(R.id.ad_parent_view), R.id.ad_parent_view); adView = AdsController.getInstance().inflateAd(this.getActivity(), (ViewGroup)view.findViewById(R.id.ad_parent_view), R.id.ad_parent_view);
} }
@Override protected abstract int getFragmentTitleResId();
@Override
public void onDestroy() { public void onDestroy() {
logDebug("onDestroy"); logDebug("onDestroy");

View File

@ -1,39 +1,44 @@
/* /*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev. * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com * For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org * or visit http://se.solovyev.org
*/ */
package org.solovyev.android.calculator.history; package org.solovyev.android.calculator.history;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.CalculatorLocatorImpl; import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* User: serso * User: serso
* Date: 12/18/11 * Date: 12/18/11
* Time: 7:39 PM * Time: 7:39 PM
*/ */
public class CalculatorHistoryFragment extends AbstractCalculatorHistoryFragment { public class CalculatorHistoryFragment extends AbstractCalculatorHistoryFragment {
@Override @Override
protected int getItemLayoutId() { protected int getFragmentTitleResId() {
return R.layout.history; return R.string.c_history;
} }
@NotNull @Override
@Override protected int getItemLayoutId() {
protected List<CalculatorHistoryState> getHistoryItems() { return R.layout.history;
return new ArrayList<CalculatorHistoryState>(CalculatorLocatorImpl.getInstance().getHistory().getStates()); }
}
@NotNull
@Override @Override
protected void clearHistory() { protected List<CalculatorHistoryState> getHistoryItems() {
CalculatorLocatorImpl.getInstance().getHistory().clear(); return new ArrayList<CalculatorHistoryState>(CalculatorLocatorImpl.getInstance().getHistory().getStates());
getAdapter().clear(); }
}
} @Override
protected void clearHistory() {
CalculatorLocatorImpl.getInstance().getHistory().clear();
getAdapter().clear();
}
}

View File

@ -20,7 +20,7 @@ import org.solovyev.android.calculator.*;
public class CalculatorHistoryFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener { public class CalculatorHistoryFragmentActivity extends SherlockFragmentActivity implements CalculatorEventListener {
@NotNull @NotNull
private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createCalculatorHistoryHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName()); private final CalculatorActivityHelper activityHelper = CalculatorApplication.getInstance().createActivityHelper(R.layout.main_empty, CalculatorHistoryFragmentActivity.class.getSimpleName());
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {

View File

@ -1,39 +1,44 @@
/* /*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev. * Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com * For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org * or visit http://se.solovyev.org
*/ */
package org.solovyev.android.calculator.history; package org.solovyev.android.calculator.history;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.solovyev.android.calculator.CalculatorLocatorImpl; import org.solovyev.android.calculator.CalculatorLocatorImpl;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* User: serso * User: serso
* Date: 12/18/11 * Date: 12/18/11
* Time: 7:40 PM * Time: 7:40 PM
*/ */
public class CalculatorSavedHistoryFragment extends AbstractCalculatorHistoryFragment { public class CalculatorSavedHistoryFragment extends AbstractCalculatorHistoryFragment {
@Override @Override
protected int getItemLayoutId() { protected int getFragmentTitleResId() {
return R.layout.saved_history; return R.string.c_saved_history;
} }
@NotNull @Override
@Override protected int getItemLayoutId() {
protected List<CalculatorHistoryState> getHistoryItems() { return R.layout.saved_history;
return new ArrayList<CalculatorHistoryState>(CalculatorLocatorImpl.getInstance().getHistory().getSavedHistory()); }
}
@NotNull
@Override @Override
protected void clearHistory() { protected List<CalculatorHistoryState> getHistoryItems() {
CalculatorLocatorImpl.getInstance().getHistory().clearSavedHistory(); return new ArrayList<CalculatorHistoryState>(CalculatorLocatorImpl.getInstance().getHistory().getSavedHistory());
getAdapter().clear(); }
}
} @Override
protected void clearHistory() {
CalculatorLocatorImpl.getInstance().getHistory().clearSavedHistory();
getAdapter().clear();
}
}