~oppen/gra

aa36c10c2a1a72ae21a6b6c3dc764f2b3d200045 — Öppen 4 days ago d0a7b0d
more state goodness
M app/src/main/java/Extensions.kt => app/src/main/java/Extensions.kt +5 -0
@@ 4,6 4,7 @@ import android.content.res.Resources
import android.graphics.Color
import android.os.CountDownTimer
import android.view.View
import com.google.android.material.snackbar.Snackbar

fun Context.versionName(): String = this.packageManager.getPackageInfo(packageName, 0).versionName



@@ 13,6 14,10 @@ fun Float.dpToPx(): Float = (this * Resources.getSystem().displayMetrics.density

fun String.asColor(): Int = Color.parseColor(this)

fun String.snack(view: View){
    Snackbar.make(view, this, Snackbar.LENGTH_SHORT).show()
}

fun View.setVisible(visible: Boolean) = when {
    visible -> {
        this.visibility = View.VISIBLE

M app/src/main/java/oppen/gra/GraActivity.kt => app/src/main/java/oppen/gra/GraActivity.kt +28 -3
@@ 3,8 3,11 @@ package oppen.gra
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import kotlinx.android.synthetic.main.activity_gra.*
import oppen.editor.R
import snack

class GraActivity : AppCompatActivity() {



@@ 22,9 25,31 @@ class GraActivity : AppCompatActivity() {

  private fun render(model: GraViewModel, state: GraState){
    when(state){
      is GraState.Empty -> TODO()
      is GraState.Active -> TODO()
      is GraState.Error -> TODO()
      is GraState.Empty -> renderEmpty(model)
      is GraState.Active -> renderActive(model, state)
      is GraState.Error -> showError(state)
    }
  }

  private fun showError(state: GraState.Error) {
    when(state.error){
      is GraError.Warning -> state.error.message.snack(root)
      is GraError.Fatal -> {
        AlertDialog.Builder(this)
          .setTitle(getString(R.string.fatal_error))
          .setMessage(state.error.message)
          .setPositiveButton(getString(R.string.close_gra)){ _, _ ->
            finish()
          }
      }
    }
  }

  private fun renderActive(model: GraViewModel, state: GraState.Active) {

  }

  private fun renderEmpty(model: GraViewModel){
    
  }
}
\ No newline at end of file

M app/src/main/java/oppen/gra/GraRepository.kt => app/src/main/java/oppen/gra/GraRepository.kt +4 -2
@@ 4,15 4,17 @@ import oppen.editor.io.FileDatasource

class GraRepository(private val fileIO: FileDatasource) {

  fun initialise() {
  fun initialise(onInit: (state: GraState) -> Unit) {
    fileIO.run {
      openLastFile { filename, content, error ->
        when {
          error != null -> onInit(GraState.Error(GraError.Fatal(error)))
          hasActiveFile() -> {

            onInit(GraState.Active(filename ?: "", content ?: ""))
          }
          else -> {
            //empty state
            onInit(GraState.Empty)
          }
        }
      }

M app/src/main/java/oppen/gra/GraState.kt => app/src/main/java/oppen/gra/GraState.kt +8 -3
@@ 1,7 1,12 @@
package oppen.gra

sealed class GraState {
  data class Empty(val nothing: Nothing): GraState()
  data class Active(val title: String, val content: String): GraState()
  data class Error(val error: String): GraState()
  object Empty : GraState()
  data class Active(val filename: String, val content: String): GraState()
  data class Error(val error: GraError): GraState()
}

sealed class GraError{
  data class Warning(val message: String): GraError()
  data class Fatal(val message: String): GraError()
}
\ No newline at end of file

M app/src/main/java/oppen/gra/GraViewModel.kt => app/src/main/java/oppen/gra/GraViewModel.kt +1 -1
@@ 13,7 13,7 @@ class GraViewModel: ViewModel() {

    private val editorState: MutableLiveData<GraState> by lazy {
        MutableLiveData<GraState>().also {
            repository.initialise()
            repository.initialise(editorState::postValue)
        }
    }


M app/src/main/res/layout/activity_gra.xml => app/src/main/res/layout/activity_gra.xml +1 -0
@@ 2,6 2,7 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="oppen.gra.GraActivity">

M app/src/main/res/values/strings.xml => app/src/main/res/values/strings.xml +2 -0
@@ 18,4 18,6 @@
\nYou should have received a copy of the GNU General Public License along with this program.  If not, see www.gnu.org/licenses</string>
    <string name="inter_info">The app typeface is Inter: Copyright © Rasmus Andersson Open Font License</string>
    <string name="inter_link">rsms.me/inter</string>
    <string name="fatal_error">Fatal Error</string>
    <string name="close_gra">Close Gra</string>
</resources>