From 84bc2f1fd49f5242e626c00a40b19abb6769773d Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 6 Aug 2018 15:02:48 +0200 Subject: [PATCH] Added post web links support --- .../comunic/client/data/enums/PostTypes.java | 5 + .../client/data/helpers/PostsHelper.java | 65 ++++++++----- .../comunic/client/data/models/Post.java | 17 ++++ .../comunic/client/data/models/WebLink.java | 71 ++++++++++++++ .../client/ui/adapters/PostsAdapter.java | 13 +++ .../comunic/client/ui/views/WebImageView.java | 9 +- .../comunic/client/ui/views/WebLinkView.java | 92 ++++++++++++++++++ app/src/main/res/drawable/bg_weblink.xml | 14 +++ app/src/main/res/drawable/world.png | Bin 0 -> 2568 bytes app/src/main/res/layout/post_item.xml | 6 ++ app/src/main/res/layout/view_web_link.xml | 46 +++++++++ app/src/main/res/values/styles.xml | 6 ++ 12 files changed, 321 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/models/WebLink.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/views/WebLinkView.java create mode 100644 app/src/main/res/drawable/bg_weblink.xml create mode 100644 app/src/main/res/drawable/world.png create mode 100644 app/src/main/res/layout/view_web_link.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/enums/PostTypes.java b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/PostTypes.java index b1d5841..c3a3565 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/enums/PostTypes.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/PostTypes.java @@ -24,6 +24,11 @@ public enum PostTypes { */ MOVIE, + /** + * Web link + */ + WEBLINK, + /** * PDF */ diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java index 20cb86f..f4f1e48 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/PostsHelper.java @@ -15,6 +15,7 @@ import org.communiquons.android.comunic.client.data.enums.PostTypes; import org.communiquons.android.comunic.client.data.enums.PostUserAccess; import org.communiquons.android.comunic.client.data.enums.PostVisibilityLevels; import org.communiquons.android.comunic.client.data.arrays.PostsList; +import org.communiquons.android.comunic.client.data.models.WebLink; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -329,28 +330,7 @@ public class PostsHelper { post.setVisibilityLevel(api_to_visibility_levels(json.getString("visibility_level"))); //Determine the type of the post - switch (json.getString("kind")){ - - case "text": - post.setType(PostTypes.TEXT); - break; - - case "image": - post.setType(PostTypes.IMAGE); - break; - - case "movie": - post.setType(PostTypes.MOVIE); - break; - - case "pdf": - post.setType(PostTypes.PDF); - break; - - default: - post.setType(PostTypes.UNKNOWN); - - } + post.setType(api_to_post_types(json.getString("kind"))); //Determine the user access level to the post switch (json.getString("user_access")){ @@ -385,6 +365,16 @@ public class PostsHelper { post.setMovie(new MoviesHelper(mContext) .parse_json_object(json.getJSONObject("video_info"))); + //Get information about web link (if any) + if(post.getType() == PostTypes.WEBLINK){ + WebLink webLink = new WebLink(); + webLink.setUrl(json.getString("link_url")); + webLink.setTitle(json.getString("link_title")); + webLink.setDescription(json.getString("link_description")); + webLink.setImageURL(json.getString("link_image")); + post.setWebLink(webLink); + } + return post; } @@ -415,6 +405,37 @@ public class PostsHelper { } } + /** + * Turn API post kind to PostTypes + * + * @param kind The kind to match + */ + private PostTypes api_to_post_types(String kind){ + + switch (kind){ + + case "text": + return PostTypes.TEXT; + + case "image": + return PostTypes.IMAGE; + + case "movie": + return PostTypes.MOVIE; + + case "weblink": + return PostTypes.WEBLINK; + + case "pdf": + return PostTypes.PDF; + + default: + return PostTypes.UNKNOWN; + + } + + } + /** * Turn a POST visibility level into a string ready for the API * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/Post.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/Post.java index ae4cf35..ed90753 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/models/Post.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/Post.java @@ -46,6 +46,9 @@ public class Post { //Movie private Movie movie; + //Web link + private WebLink webLink; + //Set and get the ID of the post public void setId(int id) { @@ -211,5 +214,19 @@ public class Post { public void setMovie(Movie movie) { this.movie = movie; } + + + //Set and get web link information + public WebLink getWebLink() { + return webLink; + } + + public boolean hasWebLink() { + return webLink != null; + } + + public void setWebLink(WebLink webLink) { + this.webLink = webLink; + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/models/WebLink.java b/app/src/main/java/org/communiquons/android/comunic/client/data/models/WebLink.java new file mode 100644 index 0000000..a5b37fd --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/models/WebLink.java @@ -0,0 +1,71 @@ +package org.communiquons.android.comunic.client.data.models; + +/** + * Weblink model + * + * @author Pierre HUBERT + */ +public class WebLink { + + //Private fields + private String url; + private String title; + private String description; + private String imageURL; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getTitle() { + return title; + } + + public boolean hasTitle() { + return title != null; + } + + public void setTitle(String title) { + this.title = title; + + if(title != null) + if(title.equals("null")) + this.title = null; + } + + public String getDescription() { + return description; + } + + public boolean hasDescription() { + return description != null; + } + + public void setDescription(String description) { + this.description = description; + + if(description != null) + if(description.equals("null")) + this.description = null; + } + + public String getImageURL() { + return imageURL; + } + + public boolean hasImageURL(){ + return imageURL != null; + } + + public void setImageURL(String imageURL) { + this.imageURL = imageURL; + + if(imageURL != null) + if(imageURL.equals("null")) + this.imageURL = null; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java index a8edcee..9332be2 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/adapters/PostsAdapter.java @@ -30,6 +30,7 @@ import org.communiquons.android.comunic.client.ui.views.LikeButtonView; import org.communiquons.android.comunic.client.ui.views.MovieView; import org.communiquons.android.comunic.client.ui.views.PDFLinkButtonView; import org.communiquons.android.comunic.client.ui.views.WebImageView; +import org.communiquons.android.comunic.client.ui.views.WebLinkView; import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage; import java.util.ArrayList; @@ -192,6 +193,18 @@ public class PostsAdapter extends ArrayAdapter{ } + //Set post weblink (if any) + WebLinkView webLinkView = convertView.findViewById(R.id.post_web_link); + + if(post.hasWebLink()){ + webLinkView.setVisibility(View.VISIBLE); + webLinkView.setLink(post.getWebLink()); + } + else { + webLinkView.setVisibility(View.GONE); + } + + //Set post file PDF (if any) PDFLinkButtonView pdfLinkButtonView = convertView.findViewById(R.id.btn_pdf_link); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/WebImageView.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/WebImageView.java index 05c9f93..c23e599 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/WebImageView.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/WebImageView.java @@ -57,7 +57,7 @@ public class WebImageView extends android.support.v7.widget.AppCompatImageView { //Reset image loader ImageLoadHelper.remove(this); - setImageDrawable(UiUtils.getDrawable(getContext(), mDefaultDrawable)); + applyDefaultDrawable(); ImageLoadHelper.load(getContext(), url, this); //Save image URL @@ -91,6 +91,13 @@ public class WebImageView extends android.support.v7.widget.AppCompatImageView { this.mDefaultDrawable = defaultDrawable; } + /** + * Apply the default drawable to the view + */ + public void applyDefaultDrawable() { + setImageDrawable(UiUtils.getDrawable(getContext(), mDefaultDrawable)); + } + /** * Get the current URL used in this image view * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/WebLinkView.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/WebLinkView.java new file mode 100644 index 0000000..a68570c --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/WebLinkView.java @@ -0,0 +1,92 @@ +package org.communiquons.android.comunic.client.ui.views; + +import android.app.SearchManager; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.AttributeSet; +import android.view.View; +import android.widget.FrameLayout; +import android.widget.TextView; + +import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.models.WebLink; + +/** + * WebLink view + * + * Display a web link + * + * @author Pierre HUBERT + */ +public class WebLinkView extends FrameLayout implements View.OnClickListener { + + /** + * Current link associated with the view + */ + private WebLink mLink; + + /** + * Container views + */ + private WebImageView mLinkIcon; + private TextView mLinkTitle; + private TextView mLinkURL; + private TextView mLinkDescription; + + public WebLinkView(@NonNull Context context) { + super(context); + init(); + } + + public WebLinkView(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + init(); + } + + public WebLinkView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(); + } + + private void init(){ + + //Initialize view + View view = inflate(getContext(), R.layout.view_web_link, null); + addView(view); + + mLinkIcon = findViewById(R.id.link_image); + mLinkTitle = findViewById(R.id.link_title); + mLinkURL = findViewById(R.id.link_url); + mLinkDescription = findViewById(R.id.link_description); + + mLinkIcon.setDefaultDrawable(R.drawable.world); + mLinkIcon.applyDefaultDrawable(); + + setOnClickListener(this); + } + + public void setLink(@NonNull WebLink link) { + this.mLink = link; + + if(link.hasImageURL()) mLinkIcon.loadURL(link.getImageURL()); + else { + mLinkIcon.removeImage(); + mLinkIcon.applyDefaultDrawable(); + } + + mLinkTitle.setText(link.hasTitle() ? link.getTitle() : ""); + mLinkURL.setText(link.getUrl()); + mLinkDescription.setText(link.hasDescription() ? link.getDescription() : ""); + } + + @Override + public void onClick(View view) { + if(mLink == null) return; + + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mLink.getUrl())); + getContext().startActivity(intent); + } +} diff --git a/app/src/main/res/drawable/bg_weblink.xml b/app/src/main/res/drawable/bg_weblink.xml new file mode 100644 index 0000000..9078934 --- /dev/null +++ b/app/src/main/res/drawable/bg_weblink.xml @@ -0,0 +1,14 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/world.png b/app/src/main/res/drawable/world.png new file mode 100644 index 0000000000000000000000000000000000000000..644d9d53a7543f12e9893d0ff49bf8882e5df340 GIT binary patch literal 2568 zcmV+j3itJiP)B(6_~7)tc>NzGP!(mD z|Ai%6Xg&S^Az&|jzP-O;LRsmyMOA1l>x5gBpct1R8wp$ttVL20k=8YYlI3&+J{tz& z!XL3@>SM^Wm0+T0A?#*7ezJchz8($^k8JE40r zbuGywzu!sg8#jW9w}Kb79_(M=Jhgmf!$gdC*)SNM0}VrDe)to!pp(y3QzJ8>8Uhh; zLX`Rl^e}8f0Xi=q!uzMsKvnGsYRAzPeHLry?8T~Cx#i=YKitnS{+kD+lAr(i?fR;U z1#4zcMP05*yEKVCEkwYoDMQvpx>ih}cF3BDxF(^h_{S(Md=169d*F7Q!f1L50*UeX zXWuWOn+fE3jv=g_#O(4q3DPAE#reL#o!5?o#EbT#l_WNk4d>l)r-H+nzvxv%d zlpWQO$ZKzQj>o3&bOY|4?u7}w0_T0};B|IkaqTKl zdZ1;&i+D$F*hQG%WuoLB@XmX%$l>MrI08)w z(Bl2jj8VuG7L9-uBaNo&8hknQ0`jtkQIgXENz!OnDL8CV3gbA~Z3-@o`q5l_0D_}% zzIWc*qOlN2S?F&iP9D%rPJ*Rak{HNFfIvq{`B4teyb+a4;fppQt(3wbtu&0^ipfR0sN&e^ zw_vG^vPBdXIHWkb{c91H6*Nrfgy}APlwmUhMBh=D<;|s+#+08i)#ToYt1R^{Pz8(= zFe4Db!Lf zn~BiIosyB6J?rG&a}~W(9T&%CbRqHU9`l{(t$z86`c{q#i))X`IC7ft{7w{A~H~1@GJ+`OxA?- zu^z~Q?b0a!$-*!YT8rM`I$m`Rx@lxcnzT6O6?#MW)7uJxast->>}BR=-4M|mA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Hfys>%6Mlc;(wLcLR#O-K2v6C-F(o68!DNb8yMY(3KsF zR?@ocBPDR-cz)|xZtzk?-j8(bYxmU7Eu)2MXtsf3bMT6$=~hEAIVM3NqR_du=IIUpWlRp!4iyL%T zHdM(4)#jp|vw6p`Qj~IDqdcEq`5wCZ7DG(gA;kyJMnBs5a + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 9c6b6a5..a25497e 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -88,6 +88,12 @@ center_horizontal + + +