I am developing a SMS sending application in JavaFX8. I need help with tweaking my SMS counter function.
A little background info:
One SMS can contain 160 characters. If you exceed 160 characters, then it becomes multi-part SMS. So the second SMS can contain 146 characters and the third 153 characters. Starting from third SMS every SMS can contain 153 characters.
So it would be like 160 -> 146 -> 153 -> 153 -> 153 ...
Now I want to count down from 160 to 0 and then 146 to 0 and then 153 to 0.
This is what I have so far:
smsComposeArea.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
//Replace all multiple whitspaces with one whitespace
smsComposeArea.setText(smsComposeArea.getText().replaceAll("[^\\S\\r|\\n|\\r\\n]+", " "));
//GSM 03.38 Extended charset ^{}\[~]|€ take up 2 characters - count them as 2 chars. Replace multiple tabs, newlines, whitespaces with one.
int charCount = smsComposeArea.getText().replaceAll("[\\^{}\\\\\\[~\\]|€]{1}", "$0$0").replaceAll("(\\r|\\n|\\r\\n|\\s)+", " ").length();
int countDown;
if(smsComposeArea.getText() == null && smsComposeArea.getText().trim().isEmpty()) {
lblCharCounter.setText("160");
}
else if(charCount >= 1 && charCount <= 160) {
countDown = 160 - charCount;
lblCharCounter.setText(Integer.toString(countDown));
}
else if(charCount >= 160 && charCount <= 306) {
countDown = 146 - (charCount - 160);
lblCharCounter.setText(String.format("2 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 306 && charCount <= 459) {
countDown = 153 - (charCount - 306);
lblCharCounter.setText(String.format("3 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 459 && charCount <= 612) {
countDown = 153 - (charCount - 459);
lblCharCounter.setText(String.format("4 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 612 && charCount <= 765) {
countDown = 153 - (charCount - 612);
lblCharCounter.setText(String.format("5 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 765 && charCount <= 918) {
countDown = 153 - (charCount - 765);
lblCharCounter.setText(String.format("6 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 918 && charCount <= 1071) {
countDown = 153 - (charCount - 918);
lblCharCounter.setText(String.format("7 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 1071 && charCount <= 1224) {
countDown = 153 - (charCount - 1071);
lblCharCounter.setText(String.format("8 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 1224 && charCount <= 1377) {
countDown = 153 - (charCount - 1224);
lblCharCounter.setText(String.format("9 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 1377 && charCount <= 1530) {
countDown = 153 - (charCount - 1377);
lblCharCounter.setText(String.format("10 SMS %s", Integer.toString(countDown)));
}
}
);
It works as it is supposed to, but I think there should be more sophisticated or dynamic way to display the countdown. Too many IF-statements in my opinion.
Any help is appreciated!
Aucun commentaire:
Enregistrer un commentaire