Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion app/src/main/java/deakin/gopher/guardian/WelcomeActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
package deakin.gopher.guardian

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Button
import deakin.gopher.guardian.components.EmptyView
import deakin.gopher.guardian.components.ErrorView
import deakin.gopher.guardian.components.LoadingView
import androidx.appcompat.app.AppCompatActivity

class WelcomeActivity : AppCompatActivity() {

private lateinit var loadingView: LoadingView
private lateinit var emptyView: EmptyView
private lateinit var errorView: ErrorView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_welcome) // This links to your activity_welcome.xml

loadingView = findViewById(R.id.loadingView)
emptyView = findViewById(R.id.emptyView)
errorView = findViewById(R.id.errorView)

val btnShowLoading = findViewById<Button>(R.id.btnShowLoading)
val btnShowEmpty = findViewById<Button>(R.id.btnShowEmpty)
val btnShowError = findViewById<Button>(R.id.btnShowError)
val btnHideAll = findViewById<Button>(R.id.btnHideAll)

btnShowLoading.setOnClickListener {
hideAllStateViews()
loadingView.show()
}

btnShowEmpty.setOnClickListener {
hideAllStateViews()
emptyView.show("No records found")
}

btnShowError.setOnClickListener {
hideAllStateViews()
errorView.show("Failed to load data")
}

btnHideAll.setOnClickListener {
hideAllStateViews()
}

errorView.setOnRetryClick {
hideAllStateViews()
loadingView.show()

loadingView.postDelayed({
loadingView.hide()
emptyView.show("Retry completed, but no data available")
}, 2000)
}
}

private fun hideAllStateViews() {
loadingView.hide()
emptyView.hide()
errorView.hide()
}
}
}
36 changes: 36 additions & 0 deletions app/src/main/java/deakin/gopher/guardian/components/EmptyView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package deakin.gopher.guardian.components
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.TextView
import deakin.gopher.guardian.R

class EmptyView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {

private val message: TextView

init {
LayoutInflater.from(context).inflate(R.layout.view_empty, this, true)
visibility = GONE
message = findViewById(R.id.emptyMessage)
}

fun setMessage(text: String) {
message.text = text
}

fun show(messageText: String? = null) {
if (messageText != null) {
message.text = messageText
}
visibility = VISIBLE
}

fun hide() {
visibility = GONE
}
}
44 changes: 44 additions & 0 deletions app/src/main/java/deakin/gopher/guardian/components/ErrorView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package deakin.gopher.guardian.components
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.FrameLayout
import android.widget.TextView
import deakin.gopher.guardian.R

class ErrorView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {

private val message: TextView
private val retryButton: Button

init {
LayoutInflater.from(context).inflate(R.layout.view_error, this, true)
visibility = GONE

message = findViewById(R.id.errorMessage)
retryButton = findViewById(R.id.retryButton)
}

fun setMessage(text: String) {
message.text = text
}

fun setOnRetryClick(listener: () -> Unit) {
retryButton.setOnClickListener { listener() }
}

fun show(messageText: String? = null) {
if (messageText != null) {
message.text = messageText
}
visibility = VISIBLE
}

fun hide() {
visibility = GONE
}
}
25 changes: 25 additions & 0 deletions app/src/main/java/deakin/gopher/guardian/components/LoadingView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package deakin.gopher.guardian.components
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import deakin.gopher.guardian.R

class LoadingView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {

init {
LayoutInflater.from(context).inflate(R.layout.view_loading, this, true)
visibility = GONE
}

fun show() {
visibility = VISIBLE
}

fun hide() {
visibility = GONE
}
}
124 changes: 92 additions & 32 deletions app/src/main/res/layout/activity_welcome.xml
Original file line number Diff line number Diff line change
@@ -1,38 +1,98 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WelcomeActivity"
android:padding="16dp">

<!-- Welcome Message -->
<TextView
android:id="@+id/welcomeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_message"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="50dp" />

<!-- Detailed Description -->
<TextView
android:id="@+id/welcomeDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_description"
android:textSize="16sp"
app:layout_constraintTop_toBottomOf="@id/welcomeMessage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"
android:padding="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
tools:context=".WelcomeActivity">

<!-- YOUR ORIGINAL CONTENT -->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<!-- Welcome Message -->
<TextView
android:id="@+id/welcomeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_message"
android:textSize="22sp"
android:textStyle="bold"
android:layout_marginTop="50dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<!-- Description -->
<TextView
android:id="@+id/welcomeDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome_description"
android:textSize="16sp"
android:layout_marginTop="20dp"
android:padding="16dp"
app:layout_constraintTop_toBottomOf="@id/welcomeMessage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<!-- TEST BUTTONS -->
<Button
android:id="@+id/btnShowLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Loading"
app:layout_constraintTop_toBottomOf="@id/welcomeDescription"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="20dp" />

<Button
android:id="@+id/btnShowEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Empty"
app:layout_constraintTop_toBottomOf="@id/btnShowLoading"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="12dp" />

<Button
android:id="@+id/btnShowError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Error"
app:layout_constraintTop_toBottomOf="@id/btnShowEmpty"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="12dp" />

<Button
android:id="@+id/btnHideAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hide All"
app:layout_constraintTop_toBottomOf="@id/btnShowError"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="12dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

<!-- OVERLAY VIEWS (VERY IMPORTANT: OUTSIDE ConstraintLayout) -->

<deakin.gopher.guardian.components.LoadingView
android:id="@+id/loadingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<deakin.gopher.guardian.components.EmptyView
android:id="@+id/emptyView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<deakin.gopher.guardian.components.ErrorView
android:id="@+id/errorView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>
16 changes: 16 additions & 0 deletions app/src/main/res/layout/view_empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="24dp">

<TextView
android:id="@+id/emptyMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No data available"
android:textSize="16sp" />

</LinearLayout>
23 changes: 23 additions & 0 deletions app/src/main/res/layout/view_error.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="24dp">

<TextView
android:id="@+id/errorMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="Something went wrong"
android:textSize="16sp" />

<Button
android:id="@+id/retryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retry" />

</LinearLayout>
15 changes: 15 additions & 0 deletions app/src/main/res/layout/view_loading.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="center"
android:clickable="true"
android:focusable="true">

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

</FrameLayout>
Loading