-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into beta
- Loading branch information
Showing
10 changed files
with
260 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
161 changes: 161 additions & 0 deletions
161
android/app/src/main/java/io/github/assis10t/bobandroid/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,4 +45,4 @@ class Order( | |
status | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |