Take into account grouping separator
This commit is contained in:
parent
33c0f44a73
commit
5efa9cd3a0
@ -26,6 +26,7 @@ import android.app.Application;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import com.squareup.otto.Bus;
|
import com.squareup.otto.Bus;
|
||||||
@ -41,6 +42,7 @@ import org.solovyev.android.calculator.DisplayState;
|
|||||||
import org.solovyev.android.calculator.Editor;
|
import org.solovyev.android.calculator.Editor;
|
||||||
import org.solovyev.android.calculator.EditorState;
|
import org.solovyev.android.calculator.EditorState;
|
||||||
import org.solovyev.android.calculator.Locator;
|
import org.solovyev.android.calculator.Locator;
|
||||||
|
import org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences;
|
||||||
import org.solovyev.android.io.FileLoader;
|
import org.solovyev.android.io.FileLoader;
|
||||||
import org.solovyev.android.io.FileSaver;
|
import org.solovyev.android.io.FileSaver;
|
||||||
|
|
||||||
@ -114,16 +116,6 @@ public class History {
|
|||||||
return states;
|
return states;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
|
||||||
private File getSavedHistoryFile() {
|
|
||||||
return new File(application.getFilesDir(), "history-saved.json");
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
private File getRecentHistoryFile() {
|
|
||||||
return new File(application.getFilesDir(), "history-recent.json");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private static List<HistoryState> loadStates(@Nonnull File file) {
|
private static List<HistoryState> loadStates(@Nonnull File file) {
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
@ -142,13 +134,16 @@ public class History {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isIntermediate(@Nonnull String olderText,
|
private static boolean isIntermediate(@Nonnull String olderText,
|
||||||
@Nonnull String newerText) {
|
@Nonnull String newerText,
|
||||||
if (isEmpty(olderText)) {
|
@NonNull String groupingSeparator) {
|
||||||
|
if (TextUtils.isEmpty(olderText)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (isEmpty(newerText)) {
|
if (TextUtils.isEmpty(newerText)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
olderText = trimGroupingSeparators(olderText, groupingSeparator);
|
||||||
|
newerText = trimGroupingSeparators(newerText, groupingSeparator);
|
||||||
|
|
||||||
final int diff = newerText.length() - olderText.length();
|
final int diff = newerText.length() - olderText.length();
|
||||||
if (diff >= 1) {
|
if (diff >= 1) {
|
||||||
@ -162,6 +157,39 @@ public class History {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
static String trimGroupingSeparators(@NonNull String text, @NonNull String groupingSeparator) {
|
||||||
|
if (TextUtils.isEmpty(groupingSeparator)) {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
Check.isTrue(groupingSeparator.length() == 1);
|
||||||
|
final StringBuilder sb = new StringBuilder(text.length());
|
||||||
|
for (int i = 0; i < text.length(); i++) {
|
||||||
|
if (i == 0 || i == text.length() - 1) {
|
||||||
|
// grouping separator can't be the first and the last character
|
||||||
|
sb.append(text.charAt(i));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (Character.isDigit(text.charAt(i - 1)) && text.charAt(i) == groupingSeparator.charAt(0) && Character.isDigit(text.charAt(i + 1))) {
|
||||||
|
// grouping separator => skip
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sb.append(text.charAt(i));
|
||||||
|
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private File getSavedHistoryFile() {
|
||||||
|
return new File(application.getFilesDir(), "history-saved.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private File getRecentHistoryFile() {
|
||||||
|
return new File(application.getFilesDir(), "history-recent.json");
|
||||||
|
}
|
||||||
|
|
||||||
private void migrateOldHistory() {
|
private void migrateOldHistory() {
|
||||||
try {
|
try {
|
||||||
final String xml = preferences.getString("org.solovyev.android.calculator.CalculatorModel_history", null);
|
final String xml = preferences.getString("org.solovyev.android.calculator.CalculatorModel_history", null);
|
||||||
@ -226,6 +254,8 @@ public class History {
|
|||||||
|
|
||||||
final List<HistoryState> result = new LinkedList<>();
|
final List<HistoryState> result = new LinkedList<>();
|
||||||
|
|
||||||
|
final String groupingSeparator = Preferences.groupingSeparator.getPreference(preferences);
|
||||||
|
|
||||||
final List<HistoryState> states = recent.asList();
|
final List<HistoryState> states = recent.asList();
|
||||||
final int statesCount = states.size();
|
final int statesCount = states.size();
|
||||||
for (int i = 1; i < statesCount; i++) {
|
for (int i = 1; i < statesCount; i++) {
|
||||||
@ -233,7 +263,7 @@ public class History {
|
|||||||
final HistoryState newerState = states.get(i);
|
final HistoryState newerState = states.get(i);
|
||||||
final String olderText = olderState.editor.getTextString();
|
final String olderText = olderState.editor.getTextString();
|
||||||
final String newerText = newerState.editor.getTextString();
|
final String newerText = newerState.editor.getTextString();
|
||||||
if (!isIntermediate(olderText, newerText)) {
|
if (!isIntermediate(olderText, newerText, groupingSeparator)) {
|
||||||
result.add(0, olderState);
|
result.add(0, olderState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,6 @@ import com.squareup.otto.Bus;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.robolectric.RobolectricGradleTestRunner;
|
import org.robolectric.RobolectricGradleTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
@ -48,6 +47,11 @@ import javax.annotation.Nonnull;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.anyString;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences.groupingSeparator;
|
||||||
|
|
||||||
@Config(constants = BuildConfig.class, sdk = CalculatorTestRunner.SUPPORTED_SDK)
|
@Config(constants = BuildConfig.class, sdk = CalculatorTestRunner.SUPPORTED_SDK)
|
||||||
@RunWith(RobolectricGradleTestRunner.class)
|
@RunWith(RobolectricGradleTestRunner.class)
|
||||||
@ -57,10 +61,10 @@ public class HistoryTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
history = new History(Mockito.mock(Bus.class), Mockito.mock(Executor.class));
|
history = new History(mock(Bus.class), mock(Executor.class));
|
||||||
history.handler = Mockito.mock(Handler.class);
|
history.handler = mock(Handler.class);
|
||||||
history.preferences = Mockito.mock(SharedPreferences.class);
|
history.preferences = mock(SharedPreferences.class);
|
||||||
history.editor = Mockito.mock(Editor.class);
|
history.editor = mock(Editor.class);
|
||||||
history.application = RuntimeEnvironment.application;
|
history.application = RuntimeEnvironment.application;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,6 +90,36 @@ public class HistoryTest {
|
|||||||
assertEquals("123+3", states.get(1).editor.getTextString());
|
assertEquals("123+3", states.get(1).editor.getTextString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRecentHistoryShouldTakeIntoAccountGroupingSeparator() throws Exception {
|
||||||
|
when(history.preferences.contains(eq(groupingSeparator.getKey()))).thenReturn(true);
|
||||||
|
when(history.preferences.getString(eq(groupingSeparator.getKey()), anyString())).thenReturn(" ");
|
||||||
|
addState("1");
|
||||||
|
addState("12");
|
||||||
|
addState("123");
|
||||||
|
addState("1 234");
|
||||||
|
addState("12 345");
|
||||||
|
|
||||||
|
List<HistoryState> states = history.getRecent();
|
||||||
|
assertEquals(1, states.size());
|
||||||
|
assertEquals("12 345", states.get(0).editor.getTextString());
|
||||||
|
history.clearRecent();
|
||||||
|
|
||||||
|
when(history.preferences.getString(eq(groupingSeparator.getKey()), anyString())).thenReturn("'");
|
||||||
|
addState("1");
|
||||||
|
addState("12");
|
||||||
|
addState("123");
|
||||||
|
addState("1'234");
|
||||||
|
addState("12'345");
|
||||||
|
addState("12 345");
|
||||||
|
|
||||||
|
states = history.getRecent();
|
||||||
|
assertEquals(2, states.size());
|
||||||
|
assertEquals("12 345", states.get(0).editor.getTextString());
|
||||||
|
assertEquals("12'345", states.get(1).editor.getTextString());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRecentHistoryShouldNotContainEmptyStates() throws Exception {
|
public void testRecentHistoryShouldNotContainEmptyStates() throws Exception {
|
||||||
addState("");
|
addState("");
|
||||||
|
Loading…
Reference in New Issue
Block a user