Selaa lähdekoodia

Add share button

Infinite 3 vuotta sitten
vanhempi
sitoutus
dfb2c9edf8

+ 19 - 1
app/src/main/java/club/thepenguins/android/activities/PostActivity.java

@@ -7,6 +7,7 @@ import androidx.webkit.WebSettingsCompat;
 import androidx.webkit.WebViewFeature;
 
 import android.content.Context;
+import android.content.Intent;
 import android.content.res.Configuration;
 import android.os.Bundle;
 import android.os.Handler;
@@ -52,7 +53,7 @@ public class PostActivity extends AppCompatActivity {
     private LinearLayoutManager layoutManager;
     private TextView textView;
     private ShimmerFrameLayout loader;
-    private ImageView imageView;
+    private ImageView imageView, share;
 
 
     @Override
@@ -61,6 +62,7 @@ public class PostActivity extends AppCompatActivity {
         setContentView(R.layout.activity_post);
 
         imageView = findViewById(R.id.back);
+        share = findViewById(R.id.share);
 
         imageView.setOnClickListener(new View.OnClickListener() {
             @Override
@@ -74,6 +76,7 @@ public class PostActivity extends AppCompatActivity {
         String title = getIntent().getStringExtra("title");
         String author = getIntent().getStringExtra("author");
         String image = getIntent().getStringExtra("image");
+        String link = getIntent().getStringExtra("link");
 
         loader = (ShimmerFrameLayout) findViewById(R.id.shimmer_view_container);
 
@@ -169,6 +172,13 @@ public class PostActivity extends AppCompatActivity {
             }
         }, 5000);
 
+        share.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                sharePost(PostActivity.this, link);
+            }
+        });
+
 
     }
 
@@ -250,4 +260,12 @@ public class PostActivity extends AppCompatActivity {
 
     }
 
+    public static void sharePost(Context context, String uri) {
+        Intent sendIntent = new Intent();
+        sendIntent.setAction(Intent.ACTION_SEND);
+        sendIntent.putExtra(Intent.EXTRA_TEXT, uri);
+        sendIntent.setType("text/plain");
+        context.startActivity(sendIntent);
+    }
+
 }

+ 1 - 0
app/src/main/java/club/thepenguins/android/adapters/PostRecyclerAdapter.java

@@ -95,6 +95,7 @@ public class PostRecyclerAdapter extends RecyclerView.Adapter<PostRecyclerAdapte
                 intent.putExtra("author", object.author);
                 intent.putExtra("image", object.Image);
                 intent.putExtra("title", object.title);
+                intent.putExtra("link", object.link);
                 ctx.startActivity(intent);
 
                 /*

+ 3 - 2
app/src/main/java/club/thepenguins/android/data/Model.java

@@ -1,7 +1,7 @@
 package club.thepenguins.android.data;
 
 public class Model {
-    public String title, subtitle, Image, Content, author;
+    public String title, subtitle, Image, Content, author, link;
 
     public Model(String title, String subtitle, String image, String content) {
         this.title = title;
@@ -10,11 +10,12 @@ public class Model {
         this.Content = content;
     }
 
-    public Model(String title, String subtitle, String image, String content, String author) {
+    public Model(String title, String subtitle, String image, String content, String author, String link) {
         this.title = title;
         this.subtitle = subtitle;
         this.Image = image;
         this.Content = content;
         this.author = author;
+        this.link = link;
     }
 }

+ 2 - 2
app/src/main/java/club/thepenguins/android/fragments/AuthorPostFragment.java

@@ -132,10 +132,10 @@ public class AuthorPostFragment extends Fragment {
             @Override
             public void onResponse(Call<List<Posts>> call, Response<List<Posts>> response) {
 
-              //  mListPost = response.body();
+                //  mListPost = response.body();
                 for (int i = 0; i < response.body().size(); i++) {
 
-                    list.add(new Model(response.body().get(i).getTitle().getRendered(), response.body().get(i).getContent().getRendered(), response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl(), response.body().get(i).getLinks().getSelf().get(0).getHref(), response.body().get(i).getEmbedded().getAuthor().get(0).getName()));
+                    list.add(new Model(response.body().get(i).getTitle().getRendered(), response.body().get(i).getContent().getRendered(), response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl(), response.body().get(i).getLinks().getSelf().get(0).getHref(), response.body().get(i).getEmbedded().getAuthor().get(0).getName(), response.body().get(i).getLink()));
                     //Log.d("Home", "onResponse: " + response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl());
                 }
                 adapter.notifyDataSetChanged();

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

@@ -160,7 +160,7 @@ public class HomeFragment extends Fragment {
                 mListPost = response.body();
                 for (int i = 0; i < response.body().size(); i++) {
 
-                    list.add(new Model(Parser.unescapeEntities(response.body().get(i).getTitle().getRendered(), false), response.body().get(i).getContent().getRendered(), response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl(), response.body().get(i).getLinks().getSelf().get(0).getHref(), response.body().get(i).getEmbedded().getAuthor().get(0).getName()));
+                    list.add(new Model(Parser.unescapeEntities(response.body().get(i).getTitle().getRendered(), false), response.body().get(i).getContent().getRendered(), response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl(), response.body().get(i).getLinks().getSelf().get(0).getHref(), response.body().get(i).getEmbedded().getAuthor().get(0).getName(), response.body().get(i).getLink()));
 
                 }
                 adapter.notifyDataSetChanged();

+ 1 - 1
app/src/main/java/club/thepenguins/android/fragments/LinuxFragment.java

@@ -143,7 +143,7 @@ public class LinuxFragment extends Fragment {
                 mListPost = response.body();
                 for (int i = 0; i < response.body().size(); i++) {
 
-                    list.add(new Model(Parser.unescapeEntities(response.body().get(i).getTitle().getRendered(), false), response.body().get(i).getContent().getRendered(), response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl(), response.body().get(i).getLinks().getSelf().get(0).getHref(), response.body().get(i).getEmbedded().getAuthor().get(0).getName()));
+                    list.add(new Model(Parser.unescapeEntities(response.body().get(i).getTitle().getRendered(), false), response.body().get(i).getContent().getRendered(), response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl(), response.body().get(i).getLinks().getSelf().get(0).getHref(), response.body().get(i).getEmbedded().getAuthor().get(0).getName(), response.body().get(i).getLink()));
                     //Log.d("Linux", "onResponse: " + response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl());
                 }
                 adapter.notifyDataSetChanged();

+ 114 - 14
app/src/main/java/club/thepenguins/android/fragments/NewsFragment.java

@@ -3,12 +3,33 @@ package club.thepenguins.android.fragments;
 import android.os.Bundle;
 
 import androidx.fragment.app.Fragment;
+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 com.facebook.shimmer.ShimmerFrameLayout;
+
+import org.jsoup.parser.Parser;
+
+import java.util.ArrayList;
+import java.util.List;
+
 import club.thepenguins.android.R;
+import club.thepenguins.android.adapters.PostRecyclerAdapter;
+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 retrofit2.Call;
+import retrofit2.Callback;
+import retrofit2.Response;
+import retrofit2.Retrofit;
+import retrofit2.converter.gson.GsonConverterFactory;
 
 /**
  * A simple {@link Fragment} subclass.
@@ -17,28 +38,25 @@ import club.thepenguins.android.R;
  */
 public class NewsFragment extends Fragment {
 
-    // TODO: Rename parameter arguments, choose names that match
-    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
+
     private static final String ARG_PARAM1 = "param1";
     private static final String ARG_PARAM2 = "param2";
 
-    // TODO: Rename and change types of parameters
     private String mParam1;
     private String mParam2;
+    private RecyclerView recyclerView;
+    private LinearLayoutManager LayoutManager;
+    private ArrayList<Model> list;
+    private PostRecyclerAdapter adapter;
+    public static List<Posts> mListPost;
+    private SwipeRefreshLayout swipeContainer;
+    private ShimmerFrameLayout loader;
+
 
     public NewsFragment() {
         // Required empty public constructor
     }
 
-    /**
-     * Use this factory method to create a new instance of
-     * this fragment using the provided parameters.
-     *
-     * @param param1 Parameter 1.
-     * @param param2 Parameter 2.
-     * @return A new instance of fragment NewsFragment.
-     */
-    // TODO: Rename and change types and number of parameters
     public static NewsFragment newInstance(String param1, String param2) {
         NewsFragment fragment = new NewsFragment();
         Bundle args = new Bundle();
@@ -60,7 +78,89 @@ public class NewsFragment extends Fragment {
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
-        // Inflate the layout for this fragment
-        return inflater.inflate(R.layout.fragment_news, container, false);
+        View rootView = inflater.inflate(R.layout.fragment_news, container, false);
+
+        getActivity().setTitle("Choose Fragment");
+
+
+        recyclerView = rootView.findViewById(R.id.recycler_view);
+        loader = rootView.findViewById(R.id.shimmer_view_container);
+
+
+        LayoutManager = new LinearLayoutManager(rootView.getContext(), LinearLayoutManager.VERTICAL, false);
+        recyclerView.setLayoutManager(LayoutManager);
+
+        swipeContainer = rootView.findViewById(R.id.swiperefresh);
+
+        list = new ArrayList<>();
+
+        getRetrofit();
+
+
+        adapter = new PostRecyclerAdapter(list, rootView.getContext());
+
+        recyclerView.setAdapter(adapter);
+
+        swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
+
+            @Override
+
+            public void onRefresh() {
+
+                adapter.clear();
+                getRetrofit();
+            }
+
+        });
+
+
+        swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright,
+
+                android.R.color.holo_green_light,
+
+                android.R.color.holo_orange_light,
+
+                android.R.color.holo_red_light);
+
+        return rootView;
+    }
+
+    private void getRetrofit() {
+
+        swipeContainer.setRefreshing(true);
+        loader.setVisibility(View.VISIBLE);
+        loader.startShimmer();
+
+        Retrofit retrofit = new Retrofit.Builder()
+                .baseUrl(Constants.BaseUrl)
+                .addConverterFactory(GsonConverterFactory.create())
+                .build();
+
+        APIService service = retrofit.create(APIService.class);
+        Call<List<Posts>> call = service.getCategoryPosts("4");
+
+
+        call.enqueue(new Callback<List<Posts>>() {
+            @Override
+            public void onResponse(Call<List<Posts>> call, Response<List<Posts>> response) {
+
+                mListPost = response.body();
+                for (int i = 0; i < response.body().size(); i++) {
+
+                    list.add(new Model(Parser.unescapeEntities(response.body().get(i).getTitle().getRendered(), false), response.body().get(i).getContent().getRendered(), response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl(), response.body().get(i).getLinks().getSelf().get(0).getHref(), response.body().get(i).getEmbedded().getAuthor().get(0).getName(), response.body().get(i).getLink()));
+                    //Log.d("Linux", "onResponse: " + response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl());
+                }
+                adapter.notifyDataSetChanged();
+                swipeContainer.setRefreshing(false);
+                loader.setVisibility(View.GONE);
+
+            }
+
+            @Override
+            public void onFailure(Call<List<Posts>> call, Throwable t) {
+
+                Log.d("Linux", "onFailure: ", t);
+            }
+        });
     }
 }

+ 1 - 1
app/src/main/java/club/thepenguins/android/fragments/NixFragment.java

@@ -150,7 +150,7 @@ public class NixFragment extends Fragment {
                 mListPost = response.body();
                 for (int i = 0; i < response.body().size(); i++) {
 
-                    list.add(new Model(Parser.unescapeEntities(response.body().get(i).getTitle().getRendered(), false), response.body().get(i).getContent().getRendered(), response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl(), response.body().get(i).getLinks().getSelf().get(0).getHref(), response.body().get(i).getEmbedded().getAuthor().get(0).getName()));
+                    list.add(new Model(Parser.unescapeEntities(response.body().get(i).getTitle().getRendered(), false), response.body().get(i).getContent().getRendered(), response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl(), response.body().get(i).getLinks().getSelf().get(0).getHref(), response.body().get(i).getEmbedded().getAuthor().get(0).getName(), response.body().get(i).getLink()));
 
                     //Log.d("Linux", "onResponse: " + response.body().get(i).getEmbedded().getWpFeaturedmedia().get(0).getSourceUrl());
                 }

+ 10 - 0
app/src/main/res/drawable/ic_baseline_share_24.xml

@@ -0,0 +1,10 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24"
+    android:tint="?attr/colorControlNormal">
+  <path
+      android:fillColor="@color/textColor"
+      android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -2.92,-2.92z"/>
+</vector>

+ 4 - 4
app/src/main/res/layout/activity_post.xml

@@ -48,11 +48,11 @@
                 <ImageView
                     android:layout_width="0dp"
                     android:layout_height="match_parent"
-                    android:layout_marginTop="20dp"
-                    android:layout_marginBottom="20dp"
+                    android:layout_marginTop="15dp"
+                    android:layout_marginBottom="15dp"
                     android:layout_weight="1"
-                    android:alpha="0.6"
-                    android:src="#00FFFFFF" />
+                    android:id="@+id/share"
+                    android:src="@drawable/ic_baseline_share_24" />
             </LinearLayout>
 
         </androidx.cardview.widget.CardView>