Browse Source

[File View] add highlight sourcecode (#187)

pull/193/head
6543 M M Arif <mmarif@swatian.com> 1 week ago
parent
commit
463961800f
5 changed files with 60 additions and 12 deletions
  1. +1
    -0
      app/build.gradle
  2. +21
    -2
      app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java
  3. +26
    -6
      app/src/main/java/org/mian/gitnex/util/AppUtil.java
  4. +11
    -3
      app/src/main/res/layout/activity_file_view.xml
  5. +1
    -1
      build.gradle

+ 1
- 0
app/build.gradle View File

@@ -68,4 +68,5 @@ dependencies {
implementation "com.github.HamidrezaAmz:BreadcrumbsView:0.2.9"
implementation "commons-io:commons-io:2.6"
implementation "com.github.chrisbanes:PhotoView:2.3.0"
implementation "com.pddstudio:highlightjs-android:1.5.0"
}

+ 21
- 2
app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java View File

@@ -16,6 +16,8 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.github.chrisbanes.photoview.PhotoView;
import com.pddstudio.highlightjs.HighlightJsView;
import com.pddstudio.highlightjs.models.Theme;
import org.apache.commons.io.FilenameUtils;
import org.mian.gitnex.R;
import org.mian.gitnex.clients.RetrofitClient;
@@ -35,6 +37,7 @@ public class FileViewActivity extends AppCompatActivity {

private View.OnClickListener onClickListener;
private TextView singleFileContents;
private HighlightJsView singleCodeContents;
private PhotoView imageView;
final Context ctx = this;
private ProgressBar mProgressBar;
@@ -61,6 +64,7 @@ public class FileViewActivity extends AppCompatActivity {

ImageView closeActivity = findViewById(R.id.close);
singleFileContents = findViewById(R.id.singleFileContents);
singleCodeContents = findViewById(R.id.singleCodeContents);
imageView = findViewById(R.id.imageView);
singleFileContents.setVisibility(View.GONE);
mProgressBar = findViewById(R.id.progress_bar);
@@ -105,19 +109,34 @@ public class FileViewActivity extends AppCompatActivity {
String fileExtension = FilenameUtils.getExtension(filename);
mProgressBar.setVisibility(View.GONE);

if(fileExtension.equals("png") || fileExtension.equals("jpg") || fileExtension.equals("jpeg") || fileExtension.equals("gif")) {
if(appUtil.imageExtension(fileExtension)) { // file is image

singleFileContents.setVisibility(View.GONE);
singleCodeContents.setVisibility(View.GONE);
imageView.setVisibility(View.VISIBLE);

imageData = Base64.decode(response.body().getContent(), Base64.DEFAULT);
Drawable imageDrawable = new BitmapDrawable(getResources(), BitmapFactory.decodeByteArray(imageData, 0, imageData.length));
imageView.setImageDrawable(imageDrawable);

}
else {
else if (appUtil.sourceCodeExtension(fileExtension)) { // file is sourcecode

imageView.setVisibility(View.GONE);
singleFileContents.setVisibility(View.GONE);
singleCodeContents.setVisibility(View.VISIBLE);

singleCodeContents.setTheme(Theme.GRUVBOX_DARK);
singleCodeContents.setShowLineNumbers(true);
singleCodeContents.setSource(appUtil.decodeBase64(response.body().getContent()));

}
else { // file type not known - plain text view

imageView.setVisibility(View.GONE);
singleCodeContents.setVisibility(View.GONE);
singleFileContents.setVisibility(View.VISIBLE);

singleFileContents.setText(appUtil.decodeBase64(response.body().getContent()));

}

+ 26
- 6
app/src/main/java/org/mian/gitnex/util/AppUtil.java View File

@@ -13,6 +13,7 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Locale;

@@ -145,16 +146,16 @@ public class AppUtil {

String sMonth;
if (Integer.parseInt(month) < 10) {
sMonth = "0"+String.valueOf(month);
sMonth = "0"+ month;
} else {
sMonth = String.valueOf(month);
sMonth = month;
}

String sDay;
if (Integer.parseInt(day) < 10) {
sDay = "0"+String.valueOf(day);
sDay = "0"+ day;
} else {
sDay = String.valueOf(day);
sDay = day;
}

return year + "-" + sMonth + "-" + sDay;
@@ -170,14 +171,14 @@ public class AppUtil {

String sMin;
if ((mMinute) < 10) {
sMin = "0"+String.valueOf(mMinute);
sMin = "0"+ mMinute;
} else {
sMin = String.valueOf(mMinute);
}

String sSec;
if ((mSeconds) < 10) {
sSec = "0"+String.valueOf(mSeconds);
sSec = "0"+ mSeconds;
} else {
sSec = String.valueOf(mSeconds);
}
@@ -210,4 +211,23 @@ public class AppUtil {

}

public Boolean sourceCodeExtension(String ext) {

String[] extValues = new String[] {"md", "json", "java", "go", "php", "c", "cc", "cpp", "cxx", "cyc", "m",
"cs", "bash", "sh", "bsh", "cv", "python", "perl", "pm", "rb", "ruby", "javascript",
"coffee", "rc", "rs", "rust", "basic", "clj", "css", "dart", "lisp", "erl", "hs", "lsp", "rkt",
"ss", "llvm", "ll", "lua", "matlab", "pascal", "r", "scala", "sql", "latex", "tex", "vb", "vbs",
"vhd", "tcl", "wiki.meta", "yaml", "yml", "markdown", "xml", "proto", "regex", "py", "pl", "js"};

return Arrays.asList(extValues).contains(ext);

}

public Boolean imageExtension(String ext) {

String[] extValues = new String[] {"jpg", "jpeg", "gif", "png", "ico"};

return Arrays.asList(extValues).contains(ext);

}
}

+ 11
- 3
app/src/main/res/layout/activity_file_view.xml View File

@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/colorPrimary">

<com.google.android.material.appbar.AppBarLayout
@@ -49,7 +49,7 @@
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:background="@color/colorPrimary"
android:orientation="vertical">

<ScrollView
@@ -70,6 +70,14 @@

</LinearLayout>

<com.pddstudio.highlightjs.HighlightJsView
android:id="@+id/singleCodeContents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginTop="42dp"
android:visibility="gone" />

<com.github.chrisbanes.photoview.PhotoView
android:id="@+id/imageView"
android:layout_centerInParent="true"

+ 1
- 1
build.gradle View File

@@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
classpath 'com.android.tools.build:gradle:3.5.2'

}
}

Loading…
Cancel
Save