Skip to content

Commit

Permalink
Loading spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
nukul4r committed Jun 22, 2019
1 parent a43fb55 commit 5567830
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 20 deletions.
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "net.nukular.mucwetter2"
minSdkVersion 19
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
Expand All @@ -17,6 +17,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}

dependencies {
Expand Down
31 changes: 16 additions & 15 deletions app/src/main/java/net/nukular/mucwetter2/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;

import pl.droidsonroids.gif.GifImageView;

Expand Down Expand Up @@ -66,6 +66,7 @@ protected void onCreate(Bundle savedInstanceState) {

findViewById(R.id.content_gif_view).setVisibility(View.INVISIBLE);
findViewById(R.id.content_bitmap_view).setVisibility(View.INVISIBLE);
findViewById(R.id.spinner).setVisibility(View.INVISIBLE);
}

@Override
Expand All @@ -81,30 +82,30 @@ public void onBackPressed() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
String link = links.get(item.getTitle().toString());
boolean isGif = link.endsWith(".gif");

GifImageView gifView = findViewById(R.id.content_gif_view);
ImageView bitmapView = findViewById(R.id.content_bitmap_view);
try {
if (link.endsWith(".gif")) {
gifView.setImageDrawable(new DownloadGifTask().execute(link).get());
bitmapView.setVisibility(View.INVISIBLE);
gifView.setVisibility(View.VISIBLE);
} else {
bitmapView.setImageBitmap(new DownloadBitmapTask().execute(link).get());
gifView.setVisibility(View.INVISIBLE);
bitmapView.setVisibility(View.VISIBLE);
}
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
View spinner = findViewById(R.id.spinner);
showFirstViewHideOthers(spinner, gifView, bitmapView);

if (isGif)
new DownloadGifTask(gifView, spinner).execute(link);
else {
new DownloadBitmapTask(bitmapView, spinner).execute(link);
}

DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);

return true;
}

private void showFirstViewHideOthers(View... views) {
Stream.of(views).forEach(v -> v.setVisibility(View.INVISIBLE));
Stream.of(views).findFirst().get().setVisibility(View.VISIBLE);
}

public String loadItemsJson() {
String json;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.URL;

public class DownloadBitmapTask extends AsyncTask<String, Void, Bitmap> {
private final ImageView view;
private final View spinner;

public DownloadBitmapTask(ImageView view, View spinner) {
this.view = view;
this.spinner = spinner;
}

protected Bitmap doInBackground(String... urls) {
String url = urls[0];
Bitmap bitmap = null;
Expand All @@ -21,4 +31,11 @@ protected Bitmap doInBackground(String... urls) {
}
return bitmap;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
view.setImageBitmap(bitmap);
view.setVisibility(View.VISIBLE);
spinner.setVisibility(View.INVISIBLE);
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/net/nukular/mucwetter2/task/DownloadGifTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

import android.os.AsyncTask;
import android.util.Log;
import android.view.View;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifDrawableBuilder;
import pl.droidsonroids.gif.GifImageView;

public class DownloadGifTask extends AsyncTask<String, Void, GifDrawable> {
private final GifImageView view;
private final View spinner;

public DownloadGifTask(GifImageView view, View spinner) {
this.view = view;
this.spinner = spinner;
}

protected GifDrawable doInBackground(String... urls) {
String url = urls[0];
GifDrawable gif = null;
Expand All @@ -24,4 +34,11 @@ protected GifDrawable doInBackground(String... urls) {
}
return gif;
}

@Override
protected void onPostExecute(GifDrawable gifDrawable) {
view.setImageDrawable(gifDrawable);
view.setVisibility(View.VISIBLE);
spinner.setVisibility(View.INVISIBLE);
}
}
10 changes: 6 additions & 4 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">

<WebView
android:id="@+id/home"
android:layout_width="match_parent"
<ProgressBar
android:id="@+id/spinner"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand All @@ -32,6 +34,6 @@
<pl.droidsonroids.gif.GifImageView
android:id="@+id/content_gif_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 5567830

Please sign in to comment.