Skip to content

Commit

Permalink
Preference to enter custom format
Browse files Browse the repository at this point in the history
  • Loading branch information
igoticecream committed Oct 9, 2016
1 parent ab5eb94 commit 27353ab
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* 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 com.icecream.snorlax.app.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v7.preference.EditTextPreference;
import android.util.AttributeSet;

@SuppressWarnings({"unused", "FieldCanBeLocal", "WeakerAccess"})
public class SummaryEditTextPreference extends EditTextPreference {

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public SummaryEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

public SummaryEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public SummaryEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SummaryEditTextPreference(Context context) {
super(context);
}

@Override
public CharSequence getSummary() {
return getText();
}

@Override
public void setText(String text) {
super.setText(text);
setSummary(text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ final class RenamePreferences {
}

String getFormat() {
return "%IVP.0% %ATT%/%DEF%/%STA% %LVL%";
mPreferences.reload();

return mPreferences.getString(
mResources.getString(R.string.preference_rename_format_enable_key),
mResources.getString(R.string.preference_rename_format_enable_default)
);
}

boolean isEnabled() {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
<string name="preference_rename_already_enable_title">Show nicknamed pokemon stats</string>
<string name="preference_rename_already_enable_summary">Rename your already nicknamed pokemons with their stats</string>

<string name="preference_rename_format_enable_key">rename_format_key</string>
<string name="preference_rename_format_enable_title">Edit format</string>
<string name="preference_rename_format_enable_summary"/>
<string name="preference_rename_format_enable_default">%IVP.0% %ATT%/%DEF%/%STA% %LVL%</string>
<string name="preference_rename_format_enable_dialog_title">Enter a format</string>

<string name="preference_catch_notification">Catch Notifications</string>
<string name="preference_catch_notification_enable_key">catch_notification_key</string>
<string name="preference_catch_notification_enable_title">Enable catch notifications</string>
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
android:title="@string/preference_rename_already_enable_title"
/>

<!--suppress AndroidElementNotAllowed -->
<com.icecream.snorlax.app.widget.SummaryEditTextPreference
android:defaultValue="@string/preference_rename_format_enable_default"
android:dialogTitle="@string/preference_rename_format_enable_dialog_title"
android:key="@string/preference_rename_format_enable_key"
android:summary="@string/preference_rename_format_enable_summary"
android:title="@string/preference_rename_format_enable_title"
/>

</PreferenceCategory>

<PreferenceCategory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ public class RenameFormatTest {

private String mExpected;

private void setRenameFormat(String format) {
Mockito
.doReturn(format)
.when(mRenamePreferences)
.getFormat();
}

@Before
public void setUp() throws Exception {
// Given
Expand Down Expand Up @@ -103,6 +96,13 @@ public void testCommandCompleteFormat() throws Exception {
setRenameFormat("%NICK%");
}

private void setRenameFormat(String format) {
Mockito
.doReturn(format)
.when(mRenamePreferences)
.getFormat();
}

@Test
public void testCommandIncompleteFormat() throws Exception {
mExpected = "%NICK";
Expand Down Expand Up @@ -373,4 +373,10 @@ public void testStaminaHex() throws Exception {
setRenameFormat("%STAH%");
}
//endregion

@Test
public void testSomeCommandCombined() throws Exception {
mExpected = "Sno 006.7 1/1/1 8.5";
setRenameFormat("%NICK.3% %IVP.1% %ATT%/%DEF%/%STA% %LVL%");
}
}

0 comments on commit 27353ab

Please sign in to comment.