1. issue #36. Add peer url form

This commit is contained in:
vadym 2020-12-07 08:33:49 +02:00
parent 172b7e0ece
commit 1434db6f43
4 changed files with 102 additions and 1 deletions

View File

@ -5,12 +5,14 @@ import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.*
import android.webkit.URLUtil
import android.widget.Button
import android.widget.ListView
import android.widget.PopupWindow
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
@ -40,6 +42,7 @@ import java.nio.charset.Charset
class PeerListActivity : AppCompatActivity() {
companion object {
const val PEER_LIST = "PEER_LIST"
const val PEER_LIST_URL = "https://publicpeers.neilalexander.dev/publicnodes.json"
const val CACHE_NAME = "PEER_LIST_CACHE"
const val ONLINE_PEERINFO_LIST = "online_peer_info_list"
@ -59,6 +62,7 @@ class PeerListActivity : AppCompatActivity() {
}
}
private var peerListUrl = PEER_LIST_URL
private var peerListPing = true
var popup: PopupWindow? = null
var adapter: DropDownAdapter? = null
@ -70,6 +74,13 @@ class PeerListActivity : AppCompatActivity() {
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { _ ->
addNewPeer()
}
val preferences =
PreferenceManager.getDefaultSharedPreferences(this.baseContext)
var peerListUrl: String =
preferences.getString(PEER_LIST, "")!!
if(!peerListUrl.isNullOrBlank()){
peerListUrl = this@PeerListActivity.peerListUrl
}
var extras = intent.extras
var peerList = findViewById<ListView>(R.id.peerList)
var adapter = SelectPeerInfoListAdapter(this, arrayListOf(), mutableSetOf())
@ -108,7 +119,7 @@ class PeerListActivity : AppCompatActivity() {
}
}
}
var json = downloadJson(PEER_LIST_URL)
var json = downloadJson(peerListUrl)
var countries = CCPCountry.getLibraryMasterCountriesEnglish()
val mapType: Type = object :
TypeToken<Map<String?, Map<String, Status>>>() {}.type
@ -196,6 +207,27 @@ class PeerListActivity : AppCompatActivity() {
}
}
private fun editPeerListUrl() {
val view: View = LayoutInflater.from(this).inflate(R.layout.edit_peer_list_url_dialog, null)
val ab: AlertDialog.Builder = AlertDialog.Builder(this)
ab.setCancelable(true).setView(view)
var ad = ab.show()
var saveButton = view.findViewById<Button>(R.id.save)
saveButton.setOnClickListener{
var urlInput = view.findViewById<TextView>(R.id.urlInput)
var url = urlInput.text.toString()
if(!URLUtil.isValidUrl(url)){
urlInput.error = "The URL is invalid!"
return@setOnClickListener;
}
peerListUrl = url
val preferences =
PreferenceManager.getDefaultSharedPreferences(this.baseContext)
preferences.edit().putString(PEER_LIST, peerListUrl).apply()
ad.dismiss()
}
}
private fun addNewPeer() {
val view: View = LayoutInflater.from(this).inflate(R.layout.new_peer_dialog, null)
val countryCode: String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@ -315,6 +347,14 @@ class PeerListActivity : AppCompatActivity() {
setResult(Activity.RESULT_OK, result)
finish()
}
val editUrl = menu.findItem(R.id.editUrlItem) as MenuItem
item.setActionView(R.layout.menu_edit_url)
val editUrlButton = editUrl
.actionView.findViewById<Button>(R.id.editUrlButton)
editUrlButton.setOnClickListener {
editPeerListUrl()
}
return true
}

View File

@ -0,0 +1,44 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
android:background="@color/grey">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="10dp"
app:boxBackgroundMode="none"
android:background="@drawable/edit_text_rounded_corner"
android:textColorHint="@color/white"
style="@style/EditText.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/urlInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="URL"
android:textColor="@color/white"
android:inputType="number"
android:textCursorDrawable="@null"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:layout_constraintTop_toBottomOf="@+id/url"
android:background="@drawable/button_selector"
app:backgroundTint="@null"
android:text="SAVE"
android:textColor="@color/white"/>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/editUrlButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="EDIT URL"
android:background="@android:color/transparent"/>
</RelativeLayout>

View File

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/editUrlItem"
android:title=""
app:actionLayout="@layout/menu_edit_url"
app:showAsAction="always"/>
<item android:id="@+id/saveItem"
android:title=""
app:actionLayout="@layout/menu_save"