Browse Source

Add network check & Toasty

Infinite 3 years ago
parent
commit
47cbbb8fed

+ 5 - 0
.idea/jarRepositories.xml

@@ -21,5 +21,10 @@
       <option name="name" value="Google" />
       <option name="url" value="https://dl.google.com/dl/android/maven2/" />
     </remote-repository>
+    <remote-repository>
+      <option name="id" value="maven" />
+      <option name="name" value="maven" />
+      <option name="url" value="https://jitpack.io" />
+    </remote-repository>
   </component>
 </project>

+ 1 - 0
app/build.gradle

@@ -51,6 +51,7 @@ dependencies {
     implementation 'com.facebook.shimmer:shimmer:0.5.0'
     implementation platform('com.google.firebase:firebase-bom:28.2.1')
     implementation 'com.google.firebase:firebase-messaging'
+    implementation 'com.github.GrenderG:Toasty:1.5.0'
 
 
 }

+ 0 - 7
app/src/main/java/club/thepenguins/android/api/Listener.java

@@ -1,7 +0,0 @@
-package club.thepenguins.android.api;
-
-public interface Listener {
-
-     void onData(String sourceURL);
-     void onFailed();
-}

+ 11 - 9
app/src/main/java/club/thepenguins/android/fragments/HomeFragment.java

@@ -1,5 +1,6 @@
 package club.thepenguins.android.fragments;
 
+import android.content.Context;
 import android.os.Bundle;
 
 import androidx.fragment.app.Fragment;
@@ -7,10 +8,11 @@ import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
-import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.Toast;
+
 import com.facebook.shimmer.ShimmerFrameLayout;
 
 import org.jsoup.parser.Parser;
@@ -24,12 +26,15 @@ import club.thepenguins.android.api.APIService;
 import club.thepenguins.android.data.Model;
 import club.thepenguins.android.data.Posts;
 import club.thepenguins.android.utils.Constants;
+import es.dmoral.toasty.Toasty;
 import retrofit2.Call;
 import retrofit2.Callback;
 import retrofit2.Response;
 import retrofit2.Retrofit;
 import retrofit2.converter.gson.GsonConverterFactory;
 
+import static club.thepenguins.android.utils.Constants.noInternet;
+
 
 public class HomeFragment extends Fragment {
 
@@ -76,9 +81,6 @@ public class HomeFragment extends Fragment {
 
         View rootView = inflater.inflate(R.layout.fragment_home, container, false);
 
-        getActivity().setTitle("Choose Fragment");
-
-
         recyclerView = rootView.findViewById(R.id.recycler_view);
 
         LayoutManager = new LinearLayoutManager(rootView.getContext(), LinearLayoutManager.VERTICAL, false);
@@ -91,7 +93,7 @@ public class HomeFragment extends Fragment {
 
         list = new ArrayList<>();
 
-        getRetrofit("100");
+        getRetrofit("100", rootView.getContext());
 
 
         adapter = new PostRecyclerAdapter(list, rootView.getContext());
@@ -106,7 +108,7 @@ public class HomeFragment extends Fragment {
             public void onRefresh() {
 
                 adapter.clear();
-                getRetrofit("100");
+                getRetrofit("100", rootView.getContext());
             }
 
         });
@@ -136,7 +138,7 @@ public class HomeFragment extends Fragment {
         super.onPause();
     }
 
-    private void getRetrofit(String perpage) {
+    private void getRetrofit(String perPage, Context context) {
 
         swipeContainer.setRefreshing(true);
         loader.setVisibility(View.VISIBLE);
@@ -148,7 +150,7 @@ public class HomeFragment extends Fragment {
                 .build();
 
         APIService service = retrofit.create(APIService.class);
-        Call<List<Posts>> call = service.getPostsPerPage(perpage);
+        Call<List<Posts>> call = service.getPostsPerPage(perPage);
 
 
         call.enqueue(new Callback<List<Posts>>() {
@@ -171,7 +173,7 @@ public class HomeFragment extends Fragment {
             @Override
             public void onFailure(Call<List<Posts>> call, Throwable t) {
 
-                Log.d("Home", "onFailure: ", t);
+                Toasty.error(context, noInternet, Toast.LENGTH_LONG, true).show();
             }
         });
     }

+ 8 - 5
app/src/main/java/club/thepenguins/android/fragments/PostFragment.java

@@ -1,5 +1,6 @@
 package club.thepenguins.android.fragments;
 
+import android.content.Context;
 import android.os.Bundle;
 
 import androidx.fragment.app.Fragment;
@@ -12,7 +13,6 @@ import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.webkit.WebView;
-import android.widget.ProgressBar;
 import android.widget.TextView;
 import android.widget.Toast;
 
@@ -35,11 +35,14 @@ import club.thepenguins.android.data.Comments;
 import club.thepenguins.android.data.IndividualPost;
 import club.thepenguins.android.data.PostContent;
 import club.thepenguins.android.utils.Constants;
+import es.dmoral.toasty.Toasty;
 import retrofit2.Call;
 import retrofit2.Response;
 import retrofit2.Retrofit;
 import retrofit2.converter.gson.GsonConverterFactory;
 
+import static club.thepenguins.android.utils.Constants.noInternet;
+
 
 public class PostFragment extends Fragment {
 
@@ -100,7 +103,7 @@ public class PostFragment extends Fragment {
 
         postData.clear();
 
-        getRetrofit(mParam1);
+        getRetrofit(mParam1, rootView.getContext());
 
         Log.d("TAG", "onCreateView: " + mParam1);
 
@@ -153,7 +156,7 @@ public class PostFragment extends Fragment {
                     recyclerView.setVisibility(View.VISIBLE);
 
                 } catch (Exception e) {
-                    Toast.makeText(rootView.getContext(), "Please retry", Toast.LENGTH_SHORT).show();
+                    Toasty.error(rootView.getContext(), noInternet, Toast.LENGTH_LONG, true).show();
                     Log.d("PostFragment", "run: " + e);
                 }
 
@@ -184,7 +187,7 @@ public class PostFragment extends Fragment {
         return rootView;
     }
 
-    private void getRetrofit(String postUrl) {
+    private void getRetrofit(String postUrl, Context context) {
 
         loader.startShimmer();
 
@@ -216,7 +219,7 @@ public class PostFragment extends Fragment {
             @Override
             public void onFailure(Call<IndividualPost> call, Throwable t) {
 
-                Log.d("PostRecyclerAdapter", "onFailure: ", t);
+                Toasty.error(context, noInternet, Toast.LENGTH_LONG, true).show();
             }
         });
     }

+ 3 - 11
app/src/main/java/club/thepenguins/android/services/NotificationService.java

@@ -68,28 +68,20 @@ public class NotificationService extends FirebaseMessagingService {
                 .setOnlyAlertOnce(true)
                 .setContentIntent(pendingIntent);
 
-        // A customized design for the notification can be
-        // set only for Android versions 4.1 and above. Thus
-        // condition for the same is checked here.
+
         if (Build.VERSION.SDK_INT
                 >= Build.VERSION_CODES.JELLY_BEAN) {
             builder = builder.setContent(
                     getCustomDesign(title, message));
-        } // If Android Version is lower than Jelly Beans,
-        // customized layout cannot be used and thus the
-        // layout is set as follows
-        else {
+        } else {
             builder = builder.setContentTitle(title)
                     .setContentText(message)
                     .setSmallIcon(R.drawable.ic_launcher_background);
         }
-        // Create an object of NotificationManager class to
-        // notify the
-        // user of events that happen in the background.
+
         NotificationManager notificationManager
                 = (NotificationManager) getSystemService(
                 Context.NOTIFICATION_SERVICE);
-        // Check if the Android Version is greater than Oreo
         if (Build.VERSION.SDK_INT
                 >= Build.VERSION_CODES.O) {
             NotificationChannel notificationChannel

+ 1 - 0
app/src/main/java/club/thepenguins/android/utils/Constants.java

@@ -3,5 +3,6 @@ package club.thepenguins.android.utils;
 public interface Constants {
 
     String BaseUrl = "https://thepenguins.club/";
+    String noInternet = "Please check your internet connection & try again!";
 
 }

+ 0 - 61
app/src/main/java/club/thepenguins/android/utils/TouchyWebView.java

@@ -1,61 +0,0 @@
-package club.thepenguins.android.utils;
-
-import android.content.Context;
-import android.util.AttributeSet;
-import android.view.MotionEvent;
-import android.view.ViewParent;
-import android.webkit.WebView;
-
-import androidx.annotation.Nullable;
-
-public class TouchyWebView extends WebView {
-    ViewParent mViewParent;
-
-    public TouchyWebView(Context context) {
-        super(context);
-    }
-
-    public TouchyWebView(Context context, AttributeSet attrs) {
-        super(context, attrs);
-    }
-
-    public TouchyWebView(Context context, AttributeSet attrs, int defStyle) {
-        super(context, attrs, defStyle);
-    }
-
-    public void setViewParent(@Nullable final ViewParent viewParent) { //any ViewGroup
-        mViewParent = viewParent;
-    }
-
-
-    @Override
-    public boolean onTouchEvent(MotionEvent event) {
-
-        switch (event.getAction()) {
-            case MotionEvent.ACTION_DOWN:
-                if (null == mViewParent) {
-                    getParent().requestDisallowInterceptTouchEvent(true);
-                } else {
-                    mViewParent.requestDisallowInterceptTouchEvent(true);
-                }
-                break;
-            case MotionEvent.ACTION_UP:
-                if (null == mViewParent) {
-                    getParent().requestDisallowInterceptTouchEvent(false);
-                } else {
-                    mViewParent.requestDisallowInterceptTouchEvent(false);
-                }
-                break;
-            default:
-                break;
-        }
-        return super.onTouchEvent(event);
-    }
-
-    @Override
-    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
-        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
-        requestDisallowInterceptTouchEvent(true);
-
-
-    }}

+ 1 - 0
build.gradle

@@ -16,6 +16,7 @@ allprojects {
     repositories {
         google()
         jcenter()
+        maven { url "https://jitpack.io" }
     }
 }