Skip to content

Commit

Permalink
open http log detail
Browse files Browse the repository at this point in the history
  • Loading branch information
Alireza Tizfahm Fard committed Jan 4, 2020
1 parent 04e4b86 commit f612b62
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 2 deletions.
3 changes: 3 additions & 0 deletions networkmonitor/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<activity
android:name=".view.NetworkMonitorActivity"
android:launchMode="singleInstance" />
<activity
android:name=".view.NetworkMonitorDetailActivity"
android:launchMode="singleInstance" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package alirezat775.library.networkmonitor

import alirezat775.library.networkmonitor.core.NetworkLogging
import alirezat775.library.networkmonitor.view.NetworkMonitorActivity
import android.content.Context
import android.content.Context.SENSOR_SERVICE
Expand Down Expand Up @@ -32,5 +33,6 @@ class NetworkMonitor(private val context: Context) {
fun unRegister() {
sensorManager = null
shakeListener = null
NetworkLogging.clear()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import okhttp3.RequestBody
*/

data class NetworkModel(
val uuid: String,
val request: RequestNetworkModel,
val response: ResponseNetworkModel
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package alirezat775.library.networkmonitor.core

import okhttp3.Interceptor
import okhttp3.Response
import java.util.*

/**
* Author: Alireza Tizfahm Fard
Expand All @@ -28,7 +29,10 @@ class NetworkMonitorInterceptor : Interceptor {
rs.headers(),
rs.body()?.string()
)
NetworkLogging.list.add(NetworkModel(rqModel, rsModel))
if (NetworkLogging.list.size > 30) NetworkLogging.clear()

val uuid = UUID.randomUUID().toString()
NetworkLogging.list.add(NetworkModel(uuid, rqModel, rsModel))
return rs
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import alirezat775.library.networkmonitor.core.NetworkModel
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.log_item.view.*

Expand All @@ -13,9 +14,14 @@ import kotlinx.android.synthetic.main.log_item.view.*
* Date: 2019-12-30
*/

class NetworkLoggingAdapter : RecyclerView.Adapter<NetworkLoggingAdapter.ViewHolder>() {
open class NetworkLoggingAdapter : RecyclerView.Adapter<NetworkLoggingAdapter.ViewHolder>() {

private var list = mutableListOf<NetworkModel>()
var clickItem: ClickItem? = null

interface ClickItem {
fun onClick(uuid: String)
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val url = itemView.network_url_log
Expand Down Expand Up @@ -46,8 +52,16 @@ class NetworkLoggingAdapter : RecyclerView.Adapter<NetworkLoggingAdapter.ViewHol
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemView.setOnClickListener { clickItem?.onClick(list[position].uuid) }
holder.url.text = list[position].request.url
holder.method.text = list[position].request.method
holder.status.text = list[position].response.code.toString()
holder.itemView.context?.let {
if (list[position].response.code < 400) {
holder.status.setTextColor(ContextCompat.getColor(it, R.color.green))
} else {
holder.status.setTextColor(ContextCompat.getColor(it, R.color.red))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package alirezat775.library.networkmonitor.view

import alirezat775.library.networkmonitor.R
import alirezat775.library.networkmonitor.core.NetworkLogging
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
Expand All @@ -24,5 +25,13 @@ class NetworkMonitorActivity : AppCompatActivity() {
recyclerView.adapter = adapter
adapter.addItems(NetworkLogging.list)
adapter.notifyDataSetChanged()
adapter.clickItem = object : NetworkLoggingAdapter.ClickItem {
override fun onClick(uuid: String) {
val myIntent =
Intent(this@NetworkMonitorActivity, NetworkMonitorDetailActivity::class.java)
myIntent.putExtra("uuid", uuid)
startActivity(myIntent)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package alirezat775.library.networkmonitor.view

import alirezat775.library.networkmonitor.R
import alirezat775.library.networkmonitor.core.NetworkLogging
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity

/**
* Author: Alireza Tizfahm Fard
* Date: 2020-01-04
*/

class NetworkMonitorDetailActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.log_details)
val uuid: String = intent.getStringExtra("uuid") ?: ""
NetworkLogging.list.forEach {
if (it.uuid == uuid)
Log.d("TEST", it.request.url)
}
}
}
9 changes: 9 additions & 0 deletions networkmonitor/src/main/res/layout/log_details.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="94dp"
android:layout_margin="2dp"
android:background="@android:drawable/editbox_background"
android:orientation="vertical">

</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 4 additions & 0 deletions networkmonitor/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<color name="red">#E91E63</color>
<color name="green">#4CAF50</color>
</resources>

0 comments on commit f612b62

Please sign in to comment.