From c9c3e0312353356871268d58826e6b792e518e37 Mon Sep 17 00:00:00 2001 From: Thodoris1999 Date: Wed, 17 Oct 2018 13:14:08 +0300 Subject: [PATCH] parse tag starts --- .../java/gr/thmmy/mthmmy/model/BBTag.java | 35 +++++++++++++++++++ .../thmmy/mthmmy/session/SessionManager.java | 10 ++---- .../thmmy/mthmmy/utils/parsing/BBParser.java | 33 +++++++++++++++++ 3 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/model/BBTag.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/BBTag.java b/app/src/main/java/gr/thmmy/mthmmy/model/BBTag.java new file mode 100644 index 00000000..65a183fd --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/model/BBTag.java @@ -0,0 +1,35 @@ +package gr.thmmy.mthmmy.model; + +public class BBTag { + private int start, end; + private String name; + + public BBTag(int start, String name) { + this.start = start; + this.name = name; + } + + public int getStart() { + return start; + } + + public void setStart(int start) { + this.start = start; + } + + public int getEnd() { + return end; + } + + public void setEnd(int end) { + this.end = end; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java index ffb11416..560c7089 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -123,16 +123,12 @@ public class SessionManager { editor.putString(USERNAME, extractUserName(document)); editor.putInt(USER_ID, extractUserId(document)); String avatar = extractAvatarLink(document); - if (avatar != null) { - editor.putBoolean(HAS_AVATAR, true); + if (avatar != null) editor.putString(AVATAR_LINK, avatar); - } else - editor.putBoolean(HAS_AVATAR, false); + editor.putBoolean(HAS_AVATAR, avatar != null); + editor.putString(LOGOUT_LINK, extractLogoutLink(document)); editor.apply(); - - sharedPrefs.edit().putString(LOGOUT_LINK, extractLogoutLink(document)).apply(); - return SUCCESS; } else { Timber.i("Login failed."); diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/BBParser.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/BBParser.java index 9719eb83..1986c1af 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/BBParser.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/BBParser.java @@ -2,11 +2,44 @@ package gr.thmmy.mthmmy.utils.parsing; import android.text.SpannableStringBuilder; import android.text.SpannedString; +import android.text.TextUtils; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import gr.thmmy.mthmmy.model.BBTag; +import timber.log.Timber; public class BBParser { public static final String[] supportedTags = {"b"}; public SpannedString bb2span(String bb) { SpannableStringBuilder builder = new SpannableStringBuilder(bb); + BBTag[] tags = getTags(bb); + } + + public BBTag[] getTags(String bb) { + Pattern bbtagPattern = Pattern.compile("[*+]"); + + LinkedList tags = new LinkedList<>(); + String searcingString = bb; + Matcher bbMatcher = bbtagPattern.matcher(searcingString); + while (bbMatcher.find()) { + String name = bbMatcher.group(0); + if (!isSupported(name)) continue; + tags.add(new BBTag(bbMatcher.start(), name)); + searcingString = searcingString.substring(bbMatcher.start() + name.length()); + bbMatcher = bbtagPattern.matcher(searcingString); + } + return tags.toArray(new BBTag[0]); + } + + public boolean isSupported(String tagName) { + for (String tag : supportedTags) + if (TextUtils.equals(tag, tagName)) return true; + return false; } }