diff --git a/app/src/main/java/com/sjapps/jsonlist/FileSystem.java b/app/src/main/java/com/sjapps/jsonlist/FileSystem.java index e7fb928..2f8719c 100644 --- a/app/src/main/java/com/sjapps/jsonlist/FileSystem.java +++ b/app/src/main/java/com/sjapps/jsonlist/FileSystem.java @@ -1,13 +1,16 @@ package com.sjapps.jsonlist; +import android.content.res.AssetFileDescriptor; import android.net.Uri; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; + +import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStreamReader; import java.nio.file.Path; -import java.util.Scanner; public class FileSystem { @@ -30,39 +33,29 @@ public static String LoadDataFromFile(MainActivity mainActivity, Uri uri) { } StringBuilder sb = new StringBuilder(); - FileInputStream inputStream = null; - Scanner sc = null; try { - inputStream = (FileInputStream) mainActivity.getContentResolver().openInputStream(uri); - sc = new Scanner(inputStream, "UTF-8"); + FileInputStream inputStream = (FileInputStream) mainActivity.getContentResolver().openInputStream(uri); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - while (sc.hasNextLine()) { - String line = sc.nextLine(); - sb.append(line); - } + AssetFileDescriptor fileDescriptor = mainActivity.getContentResolver().openAssetFileDescriptor(uri , "r"); - if (sc.ioException() != null) { - throw sc.ioException(); + long currentBytes = 0; + long fileSize = fileDescriptor.getLength(); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line); + currentBytes += line.length(); + mainActivity.updateProgress((int)((currentBytes/(float)fileSize)*100)); } + fileDescriptor.close(); + inputStream.close(); + reader.close(); + return sb.toString(); } catch (IOException e) { e.printStackTrace(); return null; - } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (sc != null) { - sc.close(); - } - return sb.toString(); - //todo fix this - } } } diff --git a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java index a6f2c1c..a75fc33 100644 --- a/app/src/main/java/com/sjapps/jsonlist/MainActivity.java +++ b/app/src/main/java/com/sjapps/jsonlist/MainActivity.java @@ -5,9 +5,12 @@ import androidx.activity.OnBackPressedCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; +import androidx.annotation.AnimRes; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -17,8 +20,15 @@ import android.util.Log; import android.view.View; import android.view.ViewGroup; +import android.view.animation.Animation; +import android.view.animation.AnimationUtils; +import android.view.animation.DecelerateInterpolator; +import android.view.animation.Interpolator; +import android.view.animation.OvershootInterpolator; import android.widget.Button; import android.widget.ImageButton; +import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; @@ -44,12 +54,13 @@ public class MainActivity extends AppCompatActivity { final String TAG = "MainActivity"; ImageButton backBtn, menuBtn; + ImageView fileImg; Button openFileBtn; TextView titleTxt, emptyListTxt; ListView list; JsonData data = new JsonData(); + LinearLayout progressView; ProgressBar progressBar; - TextView loadingFileTxt; boolean isMenuOpen; ListAdapter adapter; View menu, dim_bg; @@ -69,6 +80,10 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initialize(); + + setAnimation(this,fileImg,R.anim.scale_in_file_img, new DecelerateInterpolator()); + setAnimation(this,openFileBtn,R.anim.button_pop, new OvershootInterpolator()); + autoTransition.setDuration(150); menuBtn.setOnClickListener(view -> open_closeMenu()); @@ -147,8 +162,9 @@ private void initialize() { viewGroup = findViewById(R.id.content); menu = findViewById(R.id.menu); dim_bg = findViewById(R.id.dim_layout); + progressView = findViewById(R.id.loadingView); progressBar = findViewById(R.id.progressBar); - loadingFileTxt = findViewById(R.id.LoadFileTxt); + fileImg = findViewById(R.id.fileImg); dim_bg.bringToFront(); menu.bringToFront(); menuBtn.bringToFront(); @@ -173,7 +189,7 @@ private void open_closeMenu() { private void LoadData(String Data) { - progressBar.setVisibility(View.VISIBLE); + loadingStarted("loading json"); emptyListTxt.setVisibility(View.GONE); readFileThread = new Thread(() -> { @@ -196,17 +212,16 @@ private void LoadData(String Data) { return; } + loadingStarted("creating list"); try { if (element instanceof JsonObject) { Log.d(TAG, "run: Json object"); JsonObject object = FileSystem.loadDataToJsonObj(element); - Log.d(TAG, "LoadData: " + object); data.setRootList(getJsonObject(object)); } if (element instanceof JsonArray) { Log.d(TAG, "run: Json array"); JsonArray array = FileSystem.loadDataToJsonArray(element); - Log.d(TAG, "LoadData: " + array); data.setRootList(getJsonArrayRoot(array)); } } catch (Exception e){ @@ -220,7 +235,12 @@ private void LoadData(String Data) { TransitionManager.beginDelayedTransition(viewGroup, autoTransition); adapter = new ListAdapter(data.getRootList(), MainActivity.this, ""); list.setAdapter(adapter); + fileImg.clearAnimation(); + openFileBtn.clearAnimation(); + fileImg.setVisibility(View.GONE); openFileBtn.setVisibility(View.GONE); + setAnimation(MainActivity.this,list,R.anim.scale_in2,new DecelerateInterpolator()); + list.setVisibility(View.VISIBLE); backBtn.setVisibility(View.GONE); titleTxt.setText(""); data.clearPath(); @@ -228,7 +248,7 @@ private void LoadData(String Data) { } else data.setRootList(temp); - handler.post(() -> progressBar.setVisibility(View.GONE)); + handler.post(() -> loadingFinished(true)); }); readFileThread.start(); @@ -292,8 +312,7 @@ void ReadFile(Uri uri){ if (readFileThread != null && readFileThread.isAlive()){ return; } - progressBar.setVisibility(View.VISIBLE); - loadingFileTxt.setVisibility(View.VISIBLE); + loadingStarted("Reading file"); readFileThread = new Thread(() -> { @@ -304,8 +323,6 @@ void ReadFile(Uri uri){ return; } handler.post(() -> { - progressBar.setVisibility(View.GONE); - loadingFileTxt.setVisibility(View.GONE); LoadData(Data); }); @@ -314,6 +331,65 @@ void ReadFile(Uri uri){ } + void loadingStarted(){ + loadingStarted("loading..."); + + } + + void loadingStarted(String txt){ + TextView text = progressView.findViewById(R.id.loadingTxt); + progressBar.setIndeterminate(true); + text.setText(txt); + handler.postDelayed(() -> { + if (progressView.getVisibility() != View.VISIBLE) { + setAnimation(this, progressView, R.anim.scale_in); + text.setVisibility(View.VISIBLE); + progressView.setVisibility(View.VISIBLE); + } + },300); + + } + public void updateProgress(int val){ + progressBar.setIndeterminate(false); + progressBar.setProgress(val); + } + + void loadingFinished(boolean isFinished){ + + if (!isFinished){ + handler.postDelayed(()-> { + setAnimation(this, progressView,R.anim.scale_out); + progressView.setVisibility(View.INVISIBLE); + },300); + return; + } + + progressBar.setIndeterminate(false); + progressBar.setProgress(100); + + TextView text = progressView.findViewById(R.id.loadingTxt); + handler.postDelayed(() -> text.setText( "finished"),500); + handler.postDelayed(() -> { + },700); + handler.postDelayed(() -> text.setVisibility(View.INVISIBLE),900); + handler.postDelayed(() -> { + setAnimation(this, progressView,R.anim.scale_out); + progressView.setVisibility(View.INVISIBLE); + },1000); + } + + + public static void setAnimation(Context context, @NonNull View view, @AnimRes int animationRes) { + setAnimation(context,view,animationRes,null); + } + + public static void setAnimation(Context context, @NonNull View view, @AnimRes int animationRes, Interpolator interpolator) { + Animation animation = AnimationUtils.loadAnimation(context, animationRes); + if (interpolator != null) + animation.setInterpolator(interpolator); + view.startAnimation(animation); + } + void fileTooLargeException(){ postMessageException("File is too large"); } @@ -326,7 +402,7 @@ void creatingListException(){ void postMessageException(String msg){ handler.post(() -> { Toast.makeText(MainActivity.this,msg, Toast.LENGTH_SHORT).show(); - progressBar.setVisibility(View.GONE); + loadingFinished(false); }); } } \ No newline at end of file diff --git a/app/src/main/res/anim/button_pop.xml b/app/src/main/res/anim/button_pop.xml new file mode 100644 index 0000000..05b7596 --- /dev/null +++ b/app/src/main/res/anim/button_pop.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + diff --git a/app/src/main/res/anim/scale_in.xml b/app/src/main/res/anim/scale_in.xml new file mode 100644 index 0000000..7580a87 --- /dev/null +++ b/app/src/main/res/anim/scale_in.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/anim/scale_in2.xml b/app/src/main/res/anim/scale_in2.xml new file mode 100644 index 0000000..11d602b --- /dev/null +++ b/app/src/main/res/anim/scale_in2.xml @@ -0,0 +1,24 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/anim/scale_in_file_img.xml b/app/src/main/res/anim/scale_in_file_img.xml new file mode 100644 index 0000000..03f955c --- /dev/null +++ b/app/src/main/res/anim/scale_in_file_img.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/anim/scale_out.xml b/app/src/main/res/anim/scale_out.xml new file mode 100644 index 0000000..b3440f4 --- /dev/null +++ b/app/src/main/res/anim/scale_out.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/background_transparent.xml b/app/src/main/res/drawable/background_transparent.xml new file mode 100644 index 0000000..79dbd43 --- /dev/null +++ b/app/src/main/res/drawable/background_transparent.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index aea940e..18320d3 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -83,8 +83,22 @@ android:divider="@null" android:layout_below="@+id/titlePanel" android:listSelector="@drawable/transparent_drawable" + android:visibility="gone" /> + +