Skip to content

Commit

Permalink
Merge pull request #15 from machbarschaft/home_activity
Browse files Browse the repository at this point in the history
feature: Add home fragment
  • Loading branch information
Basler182 authored Jul 12, 2020
2 parents b741c61 + 03d1375 commit 81b3c6a
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 12 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ dependencies {
implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.okHttpLoggingVersion"
implementation "io.reactivex.rxjava2:rxandroid:$rootProject.rxAndroidVersion"
implementation("com.squareup.moshi:moshi-kotlin:1.9.2")
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'

// Test dependencies
testImplementation "junit:junit:$rootProject.jUnitVersion"
Expand Down
10 changes: 4 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jetzt.machbarschaft.android">

<!-- Internet Permission -->
<uses-permission android:name="android.permission.INTERNET" />
<!--
Expand All @@ -10,24 +9,23 @@
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <!-- For verification of the User -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:allowBackup="true"
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:fullBackupContent="@xml/backup_descriptor">
android:theme="@style/AppTheme">
<activity android:name=".view.activities.HomeActivity"></activity>
<activity android:name=".view.activities.LoginActivity">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

<service
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/jetzt/machbarschaft/android/data/Order.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package jetzt.machbarschaft.android.data

// TODO add all order attributes
data class Order(val id: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package jetzt.machbarschaft.android.util

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import jetzt.machbarschaft.android.BuildConfig
import jetzt.machbarschaft.android.R

fun getBugMailIntent(context: Context) =
Intent(
Intent.ACTION_VIEW, Uri.parse(
"mailto:[email protected]" +
"?subject=" + context.getString(R.string.home_feedback_subject) +
"&body=" + context.getString(R.string.home_feedback_body1) +
"\nVersion-Name: " + BuildConfig.VERSION_NAME +
"\nVersion-Code: " + BuildConfig.VERSION_CODE +
"\nAndroid-Version: " + Build.DISPLAY +
"\nDevice: " + Build.DEVICE +
"\nManufacturer: " + Build.MANUFACTURER +
"\nModel: " + Build.MODEL +
"\n\n" + context.getString(R.string.home_feedback_body2)
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package jetzt.machbarschaft.android.view.activities

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import jetzt.machbarschaft.android.R

class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package jetzt.machbarschaft.android.view.home

import android.app.AlertDialog
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.observe
import com.google.android.material.tabs.TabLayout
import jetzt.machbarschaft.android.R
import jetzt.machbarschaft.android.util.getBugMailIntent

class HomeFragment : Fragment() {
companion object {
fun newInstance() = HomeFragment()
const val LOG_TAG = "HomeFragment"
}

private lateinit var viewModel: HomeViewModel

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val model: HomeViewModel by viewModels()
viewModel = model
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

setupSortingTabs(view)
setupBottomButtons(view)
setupObservers(view)
}

private fun setupSortingTabs(view: View) {
view.findViewById<TabLayout>(R.id.sorting_tab_layout)?.apply {
for (sortBy in HomeViewModel.SortBy.values()) {
addTab(newTab().also {
it.tag = sortBy
it.text = getString(sortBy.text)
})
}
addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
val tag = tab?.tag
check(tag is HomeViewModel.SortBy) { "Sorting tab tag is invalid!" }
Log.d(LOG_TAG, "Sorting orders by ${tag.name}")
viewModel.onSortByChanged(tag)
}

override fun onTabReselected(tab: TabLayout.Tab?) {}

override fun onTabUnselected(tab: TabLayout.Tab?) {}
})
}
}

private fun setupBottomButtons(view: View) {
view.findViewById<Button>(R.id.home_btn_faq)?.apply {
setOnClickListener {
startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse("https://machbarschaft.jetzt/faq.html"))
)
}
}

view.findViewById<Button>(R.id.home_btn_contact)?.apply {
setOnClickListener {
startActivity(
Intent(Intent.ACTION_VIEW, Uri.parse("https://machbarschaft.jetzt/#contact"))
)
}
}

view.findViewById<Button>(R.id.home_btn_bug_report)?.apply {
setOnClickListener {
AlertDialog.Builder(context).run {
setMessage(R.string.home_feedback_description)
setCancelable(false)
setPositiveButton(R.string.home_feedback_write_mail) { _, _ ->
startActivity(getBugMailIntent(context))
}
setNegativeButton(R.string.home_feedback_later) { dialog, _ -> dialog.dismiss() }
}.create().show()
}
}
}

private fun setupObservers(view: View) {
viewModel.getOrders().observe(viewLifecycleOwner) { orders ->
view.findViewById<TextView>(R.id.order_count_text)?.apply {
text = resources.getQuantityString(
R.plurals.home_order_count,
orders.size,
orders.size
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package jetzt.machbarschaft.android.view.home

import androidx.annotation.StringRes
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import jetzt.machbarschaft.android.R
import jetzt.machbarschaft.android.data.Order

class HomeViewModel : ViewModel() {
private val orders: MutableLiveData<List<Order>> by lazy {
MutableLiveData<List<Order>>(emptyList()).also {
loadOrders()
}
}

fun getOrders(): LiveData<List<Order>> {
return orders
}

fun onSortByChanged(sortBy: SortBy) {
// TODO update orders
}

private fun loadOrders() {
// TODO load orders
}

enum class SortBy(@StringRes val text: Int) {
DISTANCE(R.string.home_tab_distance),
URGENCY(R.string.home_tab_urgency)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jetzt.machbarschaft.android.view.splash

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.os.Handler
Expand All @@ -12,6 +13,7 @@ import androidx.navigation.fragment.findNavController
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import jetzt.machbarschaft.android.R
import jetzt.machbarschaft.android.view.activities.HomeActivity
import java.io.File

/**
Expand Down Expand Up @@ -51,7 +53,7 @@ class SplashFragment : Fragment() {
}

private fun startApp() {
// this.startActivity(Intent(this, HomeActivity::class.java))
startActivity(Intent(context, HomeActivity::class.java))
}

private fun startLogin() {
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/layout/activity_home.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".view.activities.HomeActivity">

<fragment
android:id="@+id/fragment_container_home"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph_home" />

</LinearLayout>
Loading

0 comments on commit 81b3c6a

Please sign in to comment.