Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Shizen39 committed Jul 19, 2018
1 parent be5e23b commit 7df37c8
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 64 deletions.
2 changes: 1 addition & 1 deletion GeoNews/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import kotlinx.android.synthetic.main.row_comments.*
/**
* Created by giorgio on 03/07/18.
* Fragment attached to ArticleDetailActivity that show latest user comments in a RV, calling fetchComments and using CommentsUtils and UserUtils
* -> Networking.fetchComments()
* -> CommentsUtils.CreateComment() / .UpdateComment() / .DeleteComment() / .RetrieveUsrID()
* (*) this -> fetchComments.onResponse() -> RV_Adapter.RecyclerViewAdapter
* this -> Networking.fetchComments()
* this (onClick) -> CommentsUtils.CreateComment() / .UpdateComment() / .RetrieveUsrID()
*
* (* Adapter) this.onViewCreated -> fetchComments.onResponse() -> RV_Adapter.RecyclerViewAdapter
* (3 Update) this.onClick -> fetchComments.onResponse().adapter.onItemClick ( also (4 Delete) -> CommentsUtils.DeleteComment() )
* GeoNews
*/

Expand All @@ -39,7 +41,8 @@ class ArticleCommentFragment : Fragment(), View.OnClickListener {
lateinit var my_img: TextView // ImgView for user image id near commentInput
lateinit var android_id: String // User android HW id
lateinit var articleUrl: String // url of selected article
var updating= false // boolean that check if a user is typing in commentInput because it's updating or creating a new comment
var updating= false /* boolean that check if a user is typing in commentInput because it's updating or creating a new comment
from fetchComments.onResponse().adapter.onItemClick */
lateinit var oldItem: UsrComment // old comment for query purpose, in case user has updated the comment

/** OnCreateView func */
Expand Down Expand Up @@ -67,7 +70,7 @@ class ArticleCommentFragment : Fragment(), View.OnClickListener {
val mLayoutManager = LinearLayoutManager(this.context)
mRecyclerView.layoutManager = mLayoutManager

/* Sets up a SwipeRefreshLayout.OnRefreshListener invoked when the user performs a swipe-to-refresh gesture. */
/* Sets up a SwipeRefreshLayout.OnRefreshListener when user performs a swipe */
val mSwipeRefreshLayout = view.findViewById(R.id.swiperefreshComment) as SwipeRefreshLayout
mSwipeRefreshLayout.setOnRefreshListener({
Commenting.fetchComments(this.context, articleUrl) // (*) this -> fetchComments.onResponse() -> RV_Adapter.RecyclerViewAdapter
Expand All @@ -83,24 +86,25 @@ class ArticleCommentFragment : Fragment(), View.OnClickListener {
if(!updating) { // User start typing a new comment (not already existing)
if (!commentInput.text.isBlank()) { // If user has written something in editText, crate comment (else do nothing)
if (CheckNetworking.isNetworkAvailable(this.context)) {
val usrId = getUsrID() // get comments user id by fetching on DB ->
val usrId = getUsrID() // get comments user id by fetching on DB -> (1)
my_img.text = usrId
CreateComment.createComment(context, commentInput.text.toString(),
articleUrl, android_id, usrId) //make a createComment request
} else Toast.makeText(this.context, "No internet connection. Please check and try again.", Toast.LENGTH_LONG).show()
CreateComment.createComment(context, commentInput.text.toString(), /** -> CommentsUtils.createComment() */
articleUrl, android_id, usrId)
} else Toast.makeText(this.context, "No internet connection. " +
"Please check and try again.", Toast.LENGTH_LONG).show()
}
}
else { // User is updating his comment
println(commentInput.text.toString())
if (commentInput.text.toString() != oldItem.comment) { // If user has updated the comment in editText, crate comment (else do nothing)
else { /** (3) <- fetchComments.onResponse().adapter.onItemClick */
if (commentInput.text.toString() != oldItem.comment) { // If user has updated the comment in editText, crate comment (else do nothing)
if (CheckNetworking.isNetworkAvailable(this.context))
UpdateComment.updateComment(this.context, commentInput.text.toString(),
UpdateComment.updateComment(this.context, commentInput.text.toString(), /** (3) -> CommentsUtils.updateComment() */
oldItem.id, articleUrl)
else Toast.makeText(this.context, "No internet connection. Please check and try again.", Toast.LENGTH_LONG).show()
else Toast.makeText(this.context, "No internet connection. " +
"Please check and try again.", Toast.LENGTH_LONG).show()
}
updating=false // updated. Change state
updating=false // updated. Change state
}
commentInput.text.clear() // clear edit text input
commentInput.text.clear() // clear edit text input
/* Hide keyboard after send comment */
val editV= this.activity.currentFocus
if(editV!=null){
Expand All @@ -112,7 +116,7 @@ class ArticleCommentFragment : Fragment(), View.OnClickListener {


/**
* --> onClick.usrId =
* (1) -> onClick
* Get comments user id by it's android_id + article url
*/
private fun getUsrID() : String {
Expand All @@ -122,17 +126,18 @@ class ArticleCommentFragment : Fragment(), View.OnClickListener {
my_img.background.setTint(backgroundColor)

/* get and set comments user id */
var result= RetrieveUsrID.MakeNetworkRequestAsyncTask().execute(articleUrl, android_id).get()
var result= RetrieveUsrID.MakeNetworkRequestAsyncTask()
.execute(articleUrl, android_id).get() /** -> CommentsUtils.RetrieveUsrID() */

return if(result != "") // Usr has already written another comment
return if(result != "") // Usr has already written another comment
result
else{ // Usr has not already written
else{ // Usr has not already written
result=RetrieveUsrID.MakeNetworkRequestAsyncTask().execute(articleUrl, null).get()

if(result != ""){ // Another usr has already written -> get last Usr id + 1
if(result != ""){ // Another usr has already written -> get last Usr id + 1
(result.toInt()+1).toString()
}
else{ // usr comment is first comment -> get 1
else{ // usr comment is first comment -> get 1
"1"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import kotlinx.android.synthetic.main.activity_detail_webview.*
/**
* Created by giorgio on 02/07/18.
* Activity that shows the selected article with a webView
* ArticleDetailActivity -> ArticleCommentFragment
* ArticleDetailActivity -> Commenting.fetchComments (articleUrl)
* THIS -> ArticleCommentFragment
* THIS -> Commenting.fetchComments (articleUrl)
* GeoNews
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ import kotlinx.android.synthetic.main.row_comments.view.*
* Created by giorgio on 03/07/18.
* The adapter creates new items in the form of ViewHolders,
* populates the ViewHolders with data, and returns information about the data.
* (* fetchComments.onResponse()) --> onBindViewHolder() -> customViewHolder.bind() -> onItemLongClickListener().onItemClick()
* (# clickInterface) (#1) this.onBindViewHolder() calls
* -> (#2) customViewHolder.bind() that sets
* -> (#3) onItemLongClickListener().onItemClick()
* defined in (#0)
* --> implemented in (#) fetchComments.onResponse().adapter(object)
* GeoNews
*/
class RecyclerViewAdapter(val social: Social, val listener: OnItemLongClickListener): RecyclerView.Adapter<RecyclerViewAdapter.CustomViewHolder>() {

/** Interface to handle clicks and passing items on customViewHolde !!!!
* Interface passed in: CustomViewHolder.bind()
* Function implemented in: Commenting.fetchComments().onResponse()
* Function implemented in: fetchComments().onResponse().adapter(interface)
*/
interface OnItemLongClickListener {
interface OnItemLongClickListener { /** (#0) <- */
fun onItemClick(item: UsrComment, update: Boolean, view: View?)
}

Expand All @@ -49,7 +53,7 @@ class RecyclerViewAdapter(val social: Social, val listener: OnItemLongClickListe
holder.view.date.text=formatDate(comment.date)
holder.view.user_image.background.setTint(getColor(comment.android_id, comment.url)) //Get usr_color by comment url and usr_android_id

holder.bind(social.comments[position], listener) //bind listener interface with selected comment
holder.bind(social.comments[position], listener) /** (#1) -> */ //bind listener interface with selected comment
}

/* this method give size of the list */
Expand All @@ -62,23 +66,23 @@ class RecyclerViewAdapter(val social: Social, val listener: OnItemLongClickListe
*
*/
class CustomViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
fun bind(item: UsrComment, listener: OnItemLongClickListener) { // !Called in onBindViewHolder(), bind selected item and an itemCliclLIstener
fun bind(item: UsrComment, listener: OnItemLongClickListener) { /** -> (#2) -> */ // !Called in onBindViewHolder(), bind selected item and an itemCliclLIstener
view.setOnLongClickListener {
/* Create a popupMenu */
val popupMenu= PopupMenu(view.context, it)
popupMenu.setOnMenuItemClickListener { mItem ->
popupMenu.setOnMenuItemClickListener { mItem -> /** -> (#3) -> fetchComments.onResult.adapter(itemListener) */
when(mItem.itemId){
R.id.update -> { /** UPDATE */
if(item.android_id== getAndroidID(view.context)){ // comment was written by usr
listener.onItemClick(item, true, view) // set costumed click listener -> in fetchComments().onResponse()
listener.onItemClick(item, true, view) /** (3) <- Implemented in fetchComments.onResponse().adapter */
Toast.makeText(view.context,"Comment updated!",Toast.LENGTH_LONG).show()
}
else Toast.makeText(view.context,"Can't update other users comments",Toast.LENGTH_LONG).show()
true
}
R.id.delete -> { /** DELETE */
if(item.android_id== getAndroidID(view.context)){ //comment was written by usr
listener.onItemClick(item, false, null) //set costumed click listener -> in fetchComments().onResponse()
listener.onItemClick(item, false, null) /** (4) <- Implemented in fetchComments.onResponse().adapter */
Toast.makeText(view.context,"Comment deleted!",Toast.LENGTH_LONG).show()
}
else Toast.makeText(view.context,"Can't delete other users comments",Toast.LENGTH_LONG).show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import kotlinx.android.synthetic.main.row_articles.view.*
/**
* Created by giorgio on 01/07/18.
* The adapter creates new items in the form of ViewHolders, populates the ViewHolders with data, and returns information about the data.
* (* fetchArticles.onResponse()) --> onBindViewHolder() -> customViewHolder -> onItemClick() -> ArticleDetailActivity (articleUrl)
*
* (# fetchArticles.onResponse()) --> onBindViewHolder() -> customViewHolder -> onItemClick()
* GeoNews
*/

Expand Down Expand Up @@ -62,7 +63,7 @@ class CustomViewHolder(val view: View, var article: Article?=null): RecyclerView
const val ARTICLE_LINK_KEY= "ARTICLE_LINK"
}
init {
view.setOnClickListener {
view.setOnClickListener { /** (#) -> ArticleDetailActivity (Intent) */
val intent= Intent(view.context, ArticleDetailActivity::class.java)
intent.putExtra(ARTICLE_LINK_KEY, article?.url)
view.context.startActivity(intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,16 @@ class MapsActivity : AppCompatActivity(), GoogleMap.OnMarkerClickListener, OnMap
}
} else Toast.makeText(baseContext, "No internet connection. Check and try again.", Toast.LENGTH_LONG).show()

mMap.setOnMarkerClickListener(this) // click listener on map's markers. Override ->
mMap.setOnMarkerClickListener(this) // click listener on map's markers. Override ->
}

/**
* Retrieve Address object from country name, that contains latitude and longitude (and others stuff)
*/
private fun getLatLng(i:Int): Address { // for getting latitude and longitude
val geocoder = Geocoder(this) // used to retrieve position from location name
private fun getLatLng(i:Int): Address { // for getting latitude and longitude
val geocoder = Geocoder(this) // used to retrieve position from location name
val address= Locale("", countriesISO[i]).displayCountry // get country Name from countryIso
return geocoder.getFromLocationName(address, 1)[0] // from country Name return LatLong
return geocoder.getFromLocationName(address, 1)[0] // from country Name return LatLong
}

/**
Expand Down Expand Up @@ -212,25 +212,25 @@ class MapsActivity : AppCompatActivity(), GoogleMap.OnMarkerClickListener, OnMap
+ Locale("", marker.title).displayCountry,
Toast.LENGTH_SHORT).show()

if(clickCount==2){ //was already clicked, clicked another time
val intent= Intent(this, ListArticlesActivity::class.java) //Send intent with selected country to articleDetailActivity
if(clickCount==2){ // was already clicked, clicked another time
val intent= Intent(this, ListArticlesActivity::class.java) // Send intent with selected country to articleDetailActivity
val e=Bundle()
e.putString(QUERIES_KEY, queries)
e.putString(COUNTRY_KEY, marker.title.toLowerCase())
intent.putExtras(e) //send country iso code and eventually the queries to fetchnews's query
intent.putExtras(e) // send country iso code and eventually the queries to fetchnews's query
this.startActivity(intent)
clickCount -= 1 //restore clicked status
marker.tag = clickCount //bind with marker data
clickCount -= 1 // restore clicked status
marker.tag = clickCount // bind with marker data
}
mMap.setOnMapClickListener { //Clicked the map, not a marker
mMap.setOnMapClickListener { // Clicked the map, not a marker
onWindowFocusChanged(true)
clickCount -= 1
marker.tag = clickCount
if(blue) marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
else marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
}
}
// Return false: we have not consumed the event and we wish for the default behavior to occur
/* Return false: we have not consumed the event and we wish for the default behavior to occur */
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class SplashActivity : AppCompatActivity() {
//Set fullscreen
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) window.decorView.systemUiVisibility= (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY //fullscreen mode
if (hasFocus) window.decorView.systemUiVisibility= (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY //fullscreen mode
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // Set the content to appear under the system bars
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN) // Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN) // Hide the nav bar and status bar
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

//Launch map activity
/* Launch map activity */
val intent= Intent(this, MapsActivity::class.java)
this.startActivity(intent)
this.finish()
Expand Down
Loading

0 comments on commit 7df37c8

Please sign in to comment.