Skip to content

Commit

Permalink
build 14 - global newsfeed, about instance and URL links in post and …
Browse files Browse the repository at this point in the history
…messages
  • Loading branch information
tretdm committed Feb 11, 2023
1 parent 30da85f commit e9f40dc
Show file tree
Hide file tree
Showing 29 changed files with 364 additions and 72 deletions.
17 changes: 0 additions & 17 deletions .idea/deploymentTargetDropDown.xml

This file was deleted.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "uk.openvk.android.refresh"
minSdk 21
targetSdk 33
versionCode 13
versionName "0.0.13.alpha"
versionCode 14
versionName "0.0.14.alpha"
}

buildTypes {
Expand Down
90 changes: 73 additions & 17 deletions app/src/main/java/uk/openvk/android/refresh/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Window;
import android.os.Build;
import android.text.Html;
import android.text.Spanned;

import androidx.annotation.ColorInt;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -15,11 +17,14 @@
import com.google.android.material.imageview.ShapeableImageView;
import com.google.android.material.shape.CornerFamily;
import com.google.android.material.shape.ShapeAppearanceModel;
import com.kieronquinn.monetcompat.app.MonetCompatActivity;
import com.kieronquinn.monetcompat.core.MonetCompat;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import uk.openvk.android.refresh.api.models.OvkLink;

public class Global {
public static String bytesToHex(byte[] bytes) {
Expand Down Expand Up @@ -160,21 +165,28 @@ public static Typeface getFlexibleTypeface(Context ctx, int weight) {
SharedPreferences global_prefs = PreferenceManager.getDefaultSharedPreferences(ctx.getApplicationContext());
String value = global_prefs.getString("interface_font", "system");
Typeface family = null;
if(value.equals("inter")) {
family = ResourcesCompat.getFont(ctx, R.font.inter);
} else if(value.equals("open_sans")) {
family = ResourcesCompat.getFont(ctx, R.font.open_sans);
} else if(value.equals("raleway")) {
family = ResourcesCompat.getFont(ctx, R.font.raleway);
} else if(value.equals("roboto")) {
family = ResourcesCompat.getFont(ctx, R.font.roboto);
} else if(value.equals("rubik")) {
family = ResourcesCompat.getFont(ctx, R.font.rubik);
} else {
if(weight < 700)
family = Typeface.DEFAULT;
else
family = Typeface.DEFAULT_BOLD;
switch (value) {
case "inter":
family = ResourcesCompat.getFont(ctx, R.font.inter);
break;
case "open_sans":
family = ResourcesCompat.getFont(ctx, R.font.open_sans);
break;
case "raleway":
family = ResourcesCompat.getFont(ctx, R.font.raleway);
break;
case "roboto":
family = ResourcesCompat.getFont(ctx, R.font.roboto);
break;
case "rubik":
family = ResourcesCompat.getFont(ctx, R.font.rubik);
break;
default:
if (weight < 700)
family = Typeface.DEFAULT;
else
family = Typeface.DEFAULT_BOLD;
break;
}
boolean italic = false;
if(weight >= 800) {
Expand All @@ -200,4 +212,48 @@ public static Typeface getFlexibleTypeface(Context ctx, int weight) {
}
return TypefaceCompat.create(ctx, family, weight, italic);
}

@SuppressWarnings("deprecation")
public static Spanned formatLinksAsHtml(String original_text) {
String[] lines = original_text.split("\r\n|\r|\n");
StringBuilder text_llines = new StringBuilder();
Pattern pattern = Pattern.compile("\\[(.+?)\\]|" +
"((http|https)://)(www.)?[a-zA-Z0-9@:%._\\+~#?&//=]{1,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%._\\+~#?&//=]*)");
Matcher matcher = pattern.matcher(original_text);
boolean regexp_search = matcher.find();
String text = original_text.replaceAll("&lt;", "<").replaceAll("&gt;", ">")
.replaceAll("&amp;", "&").replaceAll("&quot;", "\"");
text = text.replace("\r\n", "<br>").replace("\n", "<br>");
int regexp_results = 0;
while(regexp_search) {
String block = matcher.group();
if(block.startsWith("[") && block.endsWith("]")) {
OvkLink link = new OvkLink();
String[] markup = block.replace("[", "").replace("]", "").split("\\|");
link.screen_name = markup[0];
if (markup.length == 2) {
if (markup[0].startsWith("id")) {
link.url = String.format("openvk://profile/%s", markup[0]);
link.name = markup[1];
} else if (markup[0].startsWith("club")) {
link.url = String.format("openvk://group/%s", markup[0]);
link.name = markup[1];
}
link.name = markup[1];
if (markup[0].startsWith("id") || markup[0].startsWith("club")) {
text = text.replace(block, String.format("<a href=\"%s\">%s</a>", link.url, link.name));
}
}
} else if(block.startsWith("https://") || block.startsWith("http://")) {
text = text.replace(block, String.format("<a href=\"%s\">%s</a>", block, block));
}
regexp_results = regexp_results + 1;
regexp_search = matcher.find();
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT);
} else {
return Html.fromHtml(text);
}
}
}
26 changes: 26 additions & 0 deletions app/src/main/java/uk/openvk/android/refresh/OvkApplication.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package uk.openvk.android.refresh;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;

import com.kieronquinn.monetcompat.core.MonetCompat;

import java.util.Locale;

import uk.openvk.android.refresh.api.wrappers.OvkAPIWrapper;

public class OvkApplication extends Application {
Expand Down Expand Up @@ -54,4 +58,26 @@ public SharedPreferences getGlobalPreferences() {
public SharedPreferences getInstancePreferences() {
return instance_prefs;
}
public static Locale getLocale(Context ctx) {
SharedPreferences global_prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
String language = global_prefs.getString("ui_language", "System");
String language_code = "en";
if(language.equals("English")) {
language_code = "en";
} else if(language.equals("Русский")) {
language_code = "ru";
// } else if(language.equals("Украïнська")) {
// language_code = "uk"; (later)
} else {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
language_code = ctx.getResources().getConfiguration().getLocales().get(0).getLanguage();
} else {
language_code = ctx.getResources().getConfiguration().locale.getLanguage();
}
}


return new Locale(language_code);
}

}
6 changes: 5 additions & 1 deletion app/src/main/java/uk/openvk/android/refresh/api/Groups.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ public void writeToParcel(Parcel parcel, int i) {
}

public void getGroupByID(OvkAPIWrapper ovk, long id) {
ovk.sendAPIMethod("Groups.getById", String.format("group_id=%s&fields=verified,photo_200,photo_400,photo_max_orig,is_member,members_count,site,description,contacts", id));
if (id < 0) {
ovk.sendAPIMethod("Groups.getById", String.format("group_id=%s&fields=verified,photo_200,photo_400,photo_max_orig,is_member,members_count,site,description,contacts", -id));
} else {
ovk.sendAPIMethod("Groups.getById", String.format("group_id=%s&fields=verified,photo_200,photo_400,photo_max_orig,is_member,members_count,site,description,contacts", id));
}
}

public void getGroups(OvkAPIWrapper ovk, long user_id, long count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ public LongPollServer parseLongPollServer(String response) {
}

public void delete(OvkAPIWrapper ovk, long id) {
ovk.sendAPIMethod("Messages.delete", String.format("message_ids=%d", id));
ovk.sendAPIMethod("Messages.delete", String.format("message_ids=%s", id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class OvkAPIWrapper {
public Error error;
private Context ctx;
private String access_token;

private OkHttpClient httpClient = null;
private boolean logging_enabled = true; // default for beta releases
private String client_name = "openvk_refresh_android";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.openvk.android.refresh.user_interface.core.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
Expand Down Expand Up @@ -46,6 +47,7 @@
import com.kieronquinn.monetcompat.core.MonetCompat;

import java.util.ArrayList;
import java.util.Locale;
import java.util.Objects;

import dev.kdrag0n.monet.theme.ColorScheme;
Expand Down Expand Up @@ -78,6 +80,7 @@
import uk.openvk.android.refresh.user_interface.core.fragments.app.ProfileFragment;
import uk.openvk.android.refresh.user_interface.list.adapters.NewsfeedToolbarSpinnerAdapter;
import uk.openvk.android.refresh.user_interface.list.items.ToolbarSpinnerItem;
import uk.openvk.android.refresh.user_interface.wrappers.LocaleContextWrapper;

public class AppActivity extends MonetCompatActivity {
public Handler handler;
Expand Down Expand Up @@ -141,6 +144,12 @@ protected void onCreate(Bundle savedInstanceState) {
createFragments();
}

@Override
protected void attachBaseContext(Context newBase) {
Locale languageType = OvkApplication.getLocale(newBase);
super.attachBaseContext(LocaleContextWrapper.wrap(newBase, languageType));
}

private void createFragments() {
newsfeedFragment = new NewsfeedFragment();
friendsFragment = new FriendsFragment();
Expand Down Expand Up @@ -695,12 +704,12 @@ public void refreshNewsfeed(boolean progress) {
if (account != null) {
if (pos == 0) {
newsfeed.get(ovk_api, 25);
if (progress) {
newsfeedFragment.showProgress();
}
} else {
newsfeed.getGlobal(ovk_api, 25);
}
if (progress) {
newsfeedFragment.showProgress();
}
else
Toast.makeText(this, R.string.not_implemented, Toast.LENGTH_LONG).show();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.openvk.android.refresh.user_interface.core.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
Expand All @@ -25,8 +26,11 @@
import com.kieronquinn.monetcompat.app.MonetCompatActivity;
import com.kieronquinn.monetcompat.core.MonetCompat;

import java.util.Locale;

import dev.kdrag0n.monet.theme.ColorScheme;
import uk.openvk.android.refresh.Global;
import uk.openvk.android.refresh.OvkApplication;
import uk.openvk.android.refresh.R;
import uk.openvk.android.refresh.api.Authorization;
import uk.openvk.android.refresh.api.enumerations.HandlerMessages;
Expand All @@ -36,6 +40,7 @@
import uk.openvk.android.refresh.user_interface.core.fragments.auth.AuthTwoFactorFragment;
import uk.openvk.android.refresh.user_interface.view.layouts.XConstraintLayout;
import uk.openvk.android.refresh.user_interface.core.listeners.OnKeyboardStateListener;
import uk.openvk.android.refresh.user_interface.wrappers.LocaleContextWrapper;

public class AuthActivity extends MonetCompatActivity {
public Handler handler;
Expand Down Expand Up @@ -95,6 +100,12 @@ public void onKeyboardStateChanged(boolean state) {
}
}

@Override
protected void attachBaseContext(Context newBase) {
Locale languageType = OvkApplication.getLocale(newBase);
super.attachBaseContext(LocaleContextWrapper.wrap(newBase, languageType));
}

private void setAPIWrapper() {
ovk_api = new OvkAPIWrapper(this);
handler = new Handler(Looper.myLooper()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.openvk.android.refresh.user_interface.core.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
Expand Down Expand Up @@ -28,17 +29,20 @@
import com.kieronquinn.monetcompat.core.MonetCompat;

import java.util.ArrayList;
import java.util.Locale;
import java.util.Objects;

import dev.kdrag0n.monet.theme.ColorScheme;
import uk.openvk.android.refresh.Global;
import uk.openvk.android.refresh.OvkApplication;
import uk.openvk.android.refresh.R;
import uk.openvk.android.refresh.api.enumerations.HandlerMessages;
import uk.openvk.android.refresh.api.models.Conversation;
import uk.openvk.android.refresh.api.wrappers.DownloadManager;
import uk.openvk.android.refresh.api.wrappers.OvkAPIWrapper;
import uk.openvk.android.refresh.user_interface.view.layouts.SendTextBottomPanel;
import uk.openvk.android.refresh.user_interface.list.adapters.MessagesAdapter;
import uk.openvk.android.refresh.user_interface.wrappers.LocaleContextWrapper;

public class ConversationActivity extends MonetCompatActivity {
public OvkAPIWrapper ovk_api;
Expand Down Expand Up @@ -88,6 +92,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setBottomPanel();
}

@Override
protected void attachBaseContext(Context newBase) {
Locale languageType = OvkApplication.getLocale(newBase);
super.attachBaseContext(LocaleContextWrapper.wrap(newBase, languageType));
}

private void setMonetTheme() {
if(Global.checkMonet(this)) {
MaterialToolbar toolbar = findViewById(R.id.app_toolbar);
Expand Down
Loading

0 comments on commit e9f40dc

Please sign in to comment.