Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
oktay-sen committed Mar 1, 2019
2 parents 156a131 + b0ab9b1 commit d82c799
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .idea/assis10t.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package io.github.assis10t.bobandroid

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.CardView
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.*
import android.widget.TextView
import io.github.assis10t.bobandroid.pojo.Item
import io.github.assis10t.bobandroid.pojo.Order
import kotlinx.android.synthetic.main.activity_main.*
import timber.log.Timber
import android.R.string.cancel
import android.content.DialogInterface
import android.support.v7.app.AlertDialog
import android.text.InputType
import android.widget.EditText



class MainActivity : AppCompatActivity() {

var loggedIn = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

loggedIn = intent.getBooleanExtra("loggedIn", false)

container.isRefreshing = true
container.setOnRefreshListener { refreshItems() }
ServerConnection().connect {
container.isRefreshing = false
}
item_list.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
item_list.adapter = ItemAdapter { selected ->
if (selected.isEmpty())
make_order.hide()
else
make_order.show()
}

make_order.hide()
make_order.setOnClickListener {
container.isRefreshing = true
make_order.hide()
val adapter = item_list.adapter as ItemAdapter
ServerConnection().makeOrder(Order(null, adapter.selectedItems)) { success ->
if (!success)
Timber.e("Could not make order.")
else {
Timber.d("Order made.")
refreshItems()
}
}
}
}

override fun onResume() {
super.onResume()

refreshItems()
}

fun refreshItems() {
container.isRefreshing = true
ServerConnection().getItems { success, items ->
container.isRefreshing = false
if (!success) {
Timber.e("getItems failed.")
return@getItems
}
Timber.d("GetItems success. Items: ${items?.size}")
val adapter = item_list.adapter as ItemAdapter
adapter.updateItems(items!!)
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_toolbar, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle item selection
return when (item.itemId) {
R.id.login -> {
startActivity(Intent(this, LoginActivity::class.java))
true
}
R.id.zeroconf_bypass -> {
val builder = AlertDialog.Builder(this)
builder.setTitle("What's the address of the server?")
val input = EditText(this)
input.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_URI or InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE
input.text.insert(0, "192.168.")
builder.setView(input)
builder.setPositiveButton("Set") { dialog, which ->
ServerConnection.zeroconfBypass(input.text.toString())
}
builder.setNegativeButton("Cancel") { dialog, which ->
dialog.cancel()
}
builder.show()
true
}
else -> super.onOptionsItemSelected(item)
}
}

class ItemAdapter(var onSelectionChanged: (selected: List<Item>) -> Unit): RecyclerView.Adapter<ItemAdapter.ViewHolder>() {
var itemList: List<Item> = listOf()
val selectedItems: MutableList<Item> = mutableListOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.fragment_shop_item, parent, false)
return ViewHolder(view)
}

override fun getItemCount(): Int = itemList.size

override fun onBindViewHolder(vh: ViewHolder, pos: Int) {
val item = itemList[pos]
val context = vh.container.context
vh.title.text = item.name
vh.container.setCardBackgroundColor(
if (selectedItems.contains(item))
vh.container.context.getColor(R.color.selectHighlight)
else
vh.container.context.getColor(R.color.white)
)
vh.container.cardElevation =
if (selectedItems.contains(item))
dp(context, 4f)
else
dp(context, 1f)
vh.container.setOnClickListener {
if (selectedItems.contains(item))
selectedItems.remove(item)
else
selectedItems.add(item)
onSelectionChanged(itemList)
notifyItemChanged(pos)
}
}

fun updateItems(items: List<Item>) {
this.itemList = items
this.selectedItems.clear()
notifyDataSetChanged()
}

class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
val title: TextView = view.findViewById(R.id.title)
val quantity: TextView = view.findViewById(R.id.quantity)
val container: CardView = view.findViewById(R.id.container)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class Item (
fun getPriceText() = "£${"%.2f".format(price)}/${unit?:"item"}"

override fun toString() = Gson().toJson(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ class Order(
status
)
}
}
}
31 changes: 31 additions & 0 deletions android/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:tools="http:https://schemas.android.com/tools"
xmlns:app="http:https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/item_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/fragment_shop_item"
tools:itemCount="5"/>
</android.support.v4.widget.SwipeRefreshLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/make_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
android:src="@drawable/ic_send_black_24dp"
android:tint="@color/white"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"/>
</FrameLayout>
25 changes: 25 additions & 0 deletions android/app/src/main/res/layout/fragment_shop_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:app="http:https://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="64dp"
app:cardElevation="1dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bananas"
android:layout_gravity="start|center_vertical"
android:layout_margin="8dp"
/>
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x1"
android:layout_gravity="end|center_vertical"
android:layout_margin="8dp"
/>
</android.support.v7.widget.CardView>
11 changes: 11 additions & 0 deletions android/app/src/main/res/menu/menu_toolbar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http:https://schemas.android.com/apk/res/android" xmlns:app="http:https://schemas.android.com/apk/res-auto">
<item
android:id="@+id/login"
android:title="Login"
app:showAsAction="never"/>
<item
android:id="@+id/zeroconf_bypass"
android:title="Zeroconf Bypass"
app:showAsAction="never"/>
</menu>

0 comments on commit d82c799

Please sign in to comment.