Android Regist Execute UI

This commit is contained in:
Florian Märkl 2019-10-13 16:59:03 +02:00
commit 4a1d6b9629
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
8 changed files with 144 additions and 6 deletions

View file

@ -58,8 +58,23 @@ class ErrorCode(val value: Int)
var isSuccess = value == 0 var isSuccess = value == 0
} }
class ChiakiLog(val levelMask: Int) class ChiakiLog(val levelMask: Int, val callback: (level: Int, text: String) -> Unit)
{ {
companion object
{
fun formatLog(level: Int, text: String) =
"[${when(level)
{
Level.DEBUG.value -> "D"
Level.VERBOSE.value -> "V"
Level.INFO.value -> "I"
Level.WARNING.value -> "W"
Level.ERROR.value -> "E"
else -> "?"
}
}] $text"
}
enum class Level(val value: Int) enum class Level(val value: Int)
{ {
DEBUG(1 shl 4), DEBUG(1 shl 4),
@ -72,7 +87,7 @@ class ChiakiLog(val levelMask: Int)
private fun log(level: Int, text: String) private fun log(level: Int, text: String)
{ {
Log.i("ChiakiJavaLog", "level $level, text $text") callback(level, text)
} }
} }

View file

@ -0,0 +1,40 @@
/*
* This file is part of Chiaki.
*
* Chiaki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Chiaki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
*/
package com.metallic.chiaki.regist
import com.metallic.chiaki.lib.ChiakiLog
import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
class ChiakiRxLog(levelMask: Int)
{
private val accSubject: BehaviorSubject<String> = BehaviorSubject.create<String>().also {
it.onNext("")
}
private val accMutex = ReentrantLock()
val logText: Observable<String> get() = accSubject
val log = ChiakiLog(levelMask, callback = { level, text ->
accMutex.withLock {
val cur = accSubject.value ?: ""
accSubject.onNext(cur + (if(cur.isEmpty()) "" else "\n") + text)
}
})
}

View file

@ -18,10 +18,15 @@
package com.metallic.chiaki.regist package com.metallic.chiaki.regist
import android.os.Bundle import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.util.Log
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import com.metallic.chiaki.R import com.metallic.chiaki.R
import com.metallic.chiaki.lib.RegistInfo import com.metallic.chiaki.lib.RegistInfo
import kotlinx.android.synthetic.main.activity_regist_execute.*
import kotlin.math.max
class RegistExecuteActivity: AppCompatActivity() class RegistExecuteActivity: AppCompatActivity()
{ {
@ -42,9 +47,17 @@ class RegistExecuteActivity: AppCompatActivity()
viewModel = ViewModelProviders.of(this).get(RegistExecuteViewModel::class.java) viewModel = ViewModelProviders.of(this).get(RegistExecuteViewModel::class.java)
logTextView.setHorizontallyScrolling(true)
logTextView.movementMethod = ScrollingMovementMethod()
viewModel.logText.observe(this, Observer {
logTextView.text = it
val scrollY = logTextView.layout.getLineBottom(logTextView.lineCount - 1) - logTextView.height + logTextView.paddingTop + logTextView.paddingBottom
logTextView.scrollTo(0, max(scrollY, 0))
})
val registInfo = RegistInfo( val registInfo = RegistInfo(
intent.getStringExtra(EXTRA_HOST) ?: return, intent.getStringExtra(EXTRA_HOST) ?: return,
intent.getBooleanExtra(EXTRA_BROADCAST, false) ?: return, intent.getBooleanExtra(EXTRA_BROADCAST, false),
intent.getStringExtra(EXTRA_PSN_ID) ?: return, intent.getStringExtra(EXTRA_PSN_ID) ?: return,
intent.getIntExtra(EXTRA_PIN, 0).toUInt() intent.getIntExtra(EXTRA_PIN, 0).toUInt()
) )

View file

@ -20,6 +20,7 @@ package com.metallic.chiaki.regist
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import com.metallic.chiaki.common.ext.toLiveData
import com.metallic.chiaki.lib.* import com.metallic.chiaki.lib.*
class RegistExecuteViewModel: ViewModel() class RegistExecuteViewModel: ViewModel()
@ -36,16 +37,18 @@ class RegistExecuteViewModel: ViewModel()
private val _state = MutableLiveData<State>(State.IDLE) private val _state = MutableLiveData<State>(State.IDLE)
val state: LiveData<State> get() = _state val state: LiveData<State> get() = _state
private val log = ChiakiLog(ChiakiLog.Level.ALL.value/* and ChiakiLog.Level.VERBOSE.value.inv()*/) private val log = ChiakiRxLog(ChiakiLog.Level.ALL.value/* and ChiakiLog.Level.VERBOSE.value.inv()*/)
private var regist: Regist? = null private var regist: Regist? = null
val logText: LiveData<String> = log.logText.toLiveData()
fun start(info: RegistInfo) fun start(info: RegistInfo)
{ {
if(regist != null) if(regist != null)
return return
try try
{ {
regist = Regist(info, log, this::registEvent) regist = Regist(info, log.log, this::registEvent)
_state.value = State.RUNNING _state.value = State.RUNNING
} }
catch(error: CreateError) catch(error: CreateError)

View file

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
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>

View file

@ -1,10 +1,66 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:padding="8dp">
<ImageView
android:id="@+id/iconImageView"
android:layout_width="match_parent"
android:layout_height="32dp"
android:src="@drawable/ic_regist_console"
android:tint="?attr/colorOnPrimary"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<com.google.android.material.textview.MaterialTextView
android:id="@+id/logTextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fontFamily="monospace"
android:scrollbars="vertical"
android:scrollHorizontally="true"
tools:text="Log..."
android:background="?attr/colorPrimaryDark"
android:padding="8dp"
app:layout_constraintTop_toBottomOf="@id/iconImageView"
app:layout_constraintBottom_toTopOf="@id/shareLogButton"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/shareLogButton"
android:layout_width="wrap_content"
android:layout_height="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
style="@style/MageTheme.Button"
app:icon="@drawable/ic_share"
android:text="@string/action_share_log"/>
<ProgressBar
android:id="@+id/progressBar"
android:visibility="gone"
android:layout_width="48dp"
android:layout_height="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:id="@+id/infoTextView"
android:layout_width="0dp"
android:layout_height="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/shareLogButton"
android:gravity="center_vertical"
android:paddingStart="8dp"
android:layout_marginEnd="8dp"
tools:text="Tried, but failed miserably" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -26,4 +26,5 @@
<string name="regist_host_invalid">Please enter a valid host name</string> <string name="regist_host_invalid">Please enter a valid host name</string>
<string name="regist_psn_id_invalid">Please enter a valid PSN ID</string> <string name="regist_psn_id_invalid">Please enter a valid PSN ID</string>
<string name="regist_pin_invalid">Please enter a valid %d-digit PIN</string> <string name="regist_pin_invalid">Please enter a valid %d-digit PIN</string>
<string name="action_share_log">Share Log</string>
</resources> </resources>

View file

@ -69,6 +69,7 @@
<item name="tint">?attr/colorOnSecondary</item> <item name="tint">?attr/colorOnSecondary</item>
<item name="android:backgroundTint">?attr/colorSecondary</item> <item name="android:backgroundTint">?attr/colorSecondary</item>
<item name="android:textColor">?attr/colorOnSecondary</item> <item name="android:textColor">?attr/colorOnSecondary</item>
<item name="iconTint">?attr/colorOnSecondary</item>
</style> </style>
<style name="TextAppearanceButton" parent="TextAppearance.MaterialComponents.Button"> <style name="TextAppearanceButton" parent="TextAppearance.MaterialComponents.Button">