Skip to content

Commit debcb59

Browse files
committed
Add a BaseFragment, use an inline star to show default status
1 parent 7ee2eac commit debcb59

File tree

7 files changed

+99
-74
lines changed

7 files changed

+99
-74
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2013 Prateek Srivastava (@f2prateek)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.f2prateek.dfg.ui;
18+
19+
import android.app.Fragment;
20+
import android.os.Bundle;
21+
import android.view.View;
22+
import butterknife.Views;
23+
import com.f2prateek.dfg.DFGApplication;
24+
import com.squareup.otto.Bus;
25+
import javax.inject.Inject;
26+
27+
public class BaseFragment extends Fragment {
28+
@Inject Bus bus;
29+
30+
@Override
31+
public void onCreate(Bundle savedInstanceState) {
32+
super.onCreate(savedInstanceState);
33+
((DFGApplication) getActivity().getApplication()).inject(this);
34+
}
35+
36+
@Override
37+
public void onResume() {
38+
super.onResume();
39+
bus.register(this);
40+
}
41+
42+
@Override
43+
public void onPause() {
44+
bus.unregister(this);
45+
super.onPause();
46+
}
47+
48+
@Override
49+
public void onViewCreated(View view, Bundle savedInstanceState) {
50+
super.onViewCreated(view, savedInstanceState);
51+
Views.inject(this, view);
52+
}
53+
}

app/src/main/java/com/f2prateek/dfg/ui/DeviceFragment.java

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,36 @@
1717
package com.f2prateek.dfg.ui;
1818

1919
import android.app.Activity;
20-
import android.app.Fragment;
2120
import android.content.Intent;
2221
import android.content.SharedPreferences;
2322
import android.net.Uri;
2423
import android.os.Bundle;
2524
import android.view.LayoutInflater;
26-
import android.view.Menu;
27-
import android.view.MenuInflater;
28-
import android.view.MenuItem;
2925
import android.view.View;
3026
import android.view.ViewGroup;
3127
import android.widget.ImageView;
3228
import android.widget.TextView;
3329
import butterknife.InjectView;
3430
import butterknife.OnClick;
35-
import butterknife.Views;
3631
import com.f2prateek.dfg.AppConstants;
37-
import com.f2prateek.dfg.DFGApplication;
3832
import com.f2prateek.dfg.Events;
3933
import com.f2prateek.dfg.R;
4034
import com.f2prateek.dfg.core.GenerateFrameService;
4135
import com.f2prateek.dfg.model.Device;
4236
import com.f2prateek.dfg.model.DeviceProvider;
43-
import com.squareup.otto.Bus;
37+
import com.squareup.otto.Subscribe;
4438
import com.squareup.picasso.Picasso;
4539
import javax.inject.Inject;
4640

47-
public class DeviceFragment extends Fragment {
41+
public class DeviceFragment extends BaseFragment {
4842

4943
private static final int RESULT_SELECT_PICTURE = 542;
50-
@Inject Bus bus;
5144
@Inject SharedPreferences sharedPreferences;
5245
@InjectView(R.id.tv_device_resolution) TextView tv_device_resolution;
5346
@InjectView(R.id.tv_device_size) TextView tv_device_size;
5447
@InjectView(R.id.tv_device_name) TextView tv_device_name;
5548
@InjectView(R.id.iv_device_thumbnail) ImageView iv_device_thumbnail;
49+
@InjectView(R.id.iv_device_default) ImageView iv_device_default;
5650
private Device device;
5751
private int deviceNum;
5852

@@ -70,68 +64,50 @@ public void onCreate(Bundle savedInstanceState) {
7064
super.onCreate(savedInstanceState);
7165
deviceNum = getArguments() != null ? getArguments().getInt("num", 0) : 0;
7266
device = DeviceProvider.getDevices().get(deviceNum);
73-
((DFGApplication) getActivity().getApplication()).inject(this);
7467
setHasOptionsMenu(true);
7568
}
7669

77-
@Override
78-
public void onResume() {
79-
super.onResume();
80-
bus.register(this);
81-
}
82-
8370
@Override
8471
public View onCreateView(LayoutInflater inflater, ViewGroup container,
8572
Bundle savedInstanceState) {
86-
View v = inflater.inflate(R.layout.fragment_device, container, false);
87-
Views.inject(this, v);
88-
return v;
73+
return inflater.inflate(R.layout.fragment_device, container, false);
8974
}
9075

9176
@Override
9277
public void onViewCreated(View view, Bundle savedInstanceState) {
9378
super.onViewCreated(view, savedInstanceState);
9479
Picasso.with(getActivity()).load(device.getThumbnail()).into(iv_device_thumbnail);
80+
iv_device_default.bringToFront();
81+
iv_device_default.setImageResource(
82+
isDefault() ? R.drawable.ic_action_star_selected : R.drawable.ic_action_star);
9583
tv_device_size.setText(device.getPhysicalSize() + "\" @ " + device.getDensity() + "dpi");
9684
tv_device_name.setText(device.getName());
9785
tv_device_resolution.setText(device.getRealSize()[0] + "x" + device.getRealSize()[1]);
9886
}
9987

100-
@Override
101-
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
102-
super.onCreateOptionsMenu(menu, inflater);
103-
inflater.inflate(R.menu.fragment_device, menu);
88+
@OnClick(R.id.iv_device_default)
89+
public void updateDefaultDevice() {
10490
if (isDefault()) {
105-
MenuItem item = menu.findItem(R.id.menu_default_device);
106-
item.setIcon(R.drawable.ic_action_star_selected);
91+
return;
10792
}
108-
}
109-
110-
@Override
111-
public boolean onOptionsItemSelected(MenuItem item) {
112-
switch (item.getItemId()) {
113-
case R.id.menu_default_device:
114-
updateDefaultDevice();
115-
return true;
116-
}
117-
return super.onOptionsItemSelected(item);
118-
}
119-
120-
private boolean isDefault() {
121-
return deviceNum == sharedPreferences.getInt(AppConstants.KEY_PREF_DEFAULT_DEVICE, 0);
122-
}
123-
124-
public void updateDefaultDevice() {
12593
SharedPreferences.Editor editor = sharedPreferences.edit();
12694
editor.putInt(AppConstants.KEY_PREF_DEFAULT_DEVICE, deviceNum);
12795
editor.commit();
12896
bus.post(new Events.DefaultDeviceUpdated(deviceNum));
12997
}
13098

131-
@Override
132-
public void onPause() {
133-
bus.unregister(this);
134-
super.onPause();
99+
@Subscribe
100+
public void onDefaultDeviceUpdated(Events.DefaultDeviceUpdated event) {
101+
iv_device_default.post(new Runnable() {
102+
@Override public void run() {
103+
iv_device_default.setImageResource(
104+
isDefault() ? R.drawable.ic_action_star_selected : R.drawable.ic_action_star);
105+
}
106+
});
107+
}
108+
109+
private boolean isDefault() {
110+
return deviceNum == sharedPreferences.getInt(AppConstants.KEY_PREF_DEFAULT_DEVICE, 0);
135111
}
136112

137113
@OnClick(R.id.iv_device_thumbnail)

app/src/main/res/layout-land/fragment_device.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@
3434
<RelativeLayout android:layout_width="0dp"
3535
android:layout_height="match_parent"
3636
android:layout_weight="1"
37-
android:padding="16dp">
37+
android:padding="@dimen/small_padding">
38+
39+
<ImageView
40+
android:id="@+id/iv_device_default"
41+
android:layout_width="wrap_content"
42+
android:layout_height="wrap_content"
43+
android:layout_alignParentTop="true"
44+
android:layout_alignParentRight="true"
45+
style="@style/StarToggle"/>
3846

3947
<TextView
4048
android:id="@+id/tv_device_name"

app/src/main/res/layout/fragment_device.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@
2222
android:layout_height="match_parent"
2323
android:orientation="vertical">
2424

25+
<ImageView
26+
android:id="@+id/iv_device_default"
27+
android:layout_width="wrap_content"
28+
android:layout_height="wrap_content"
29+
android:layout_alignParentTop="true"
30+
android:layout_alignParentRight="true"
31+
android:padding="@dimen/small_padding"
32+
style="@style/StarToggle"/>
33+
2534
<com.f2prateek.dfg.ui.ForegroundImageView
2635
android:id="@+id/iv_device_thumbnail"
2736
android:layout_width="match_parent"
2837
android:layout_height="match_parent"
2938
android:layout_above="@+id/tv_device_name"
30-
android:layout_alignParentTop="true"
3139
android:contentDescription="@string/description_device_thumbnail"
3240
android:foreground="?android:selectableItemBackground"
3341
style="@style/DeviceThumbnail"/>

app/src/main/res/menu/fragment_device.xml

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/src/main/res/values/dimens.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717

1818
<resources>
1919

20+
<dimen name="small_padding">16dp</dimen>
2021

2122
</resources>

app/src/main/res/values/styles.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
<item name="android:textSize">16sp</item>
3939
</style>
4040

41+
<style name="StarToggle">
42+
<item name="android:background">?android:selectableItemBackground</item>
43+
<item name="android:scaleType">fitXY</item>
44+
</style>
45+
4146
<style name="AboutText" parent="android:TextAppearance.DeviceDefault.Small">
4247
<item name="android:gravity">center</item>
4348
</style>

0 commit comments

Comments
 (0)