Skip to content

Commit

Permalink
Add interactive settings
Browse files Browse the repository at this point in the history
  • Loading branch information
nominalista committed Jul 31, 2019
1 parent f153481 commit a87ab7e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.google.android.material.snackbar.Snackbar
import android.content.Intent
import androidx.lifecycle.Observer
import androidx.work.WorkManager
import com.nominalista.expenses.util.extensions.startActivitySafely
import com.nominalista.expenses.util.isGranted
import com.nominalista.expenses.util.isPermissionGranted
import java.util.*
Expand Down Expand Up @@ -96,9 +97,12 @@ class SettingsFragment : Fragment() {
compositeDisposable += model.showExpensesDeletionMessage
.toObservable()
.subscribe { showExpenseDeletionMessage(it) }
compositeDisposable += model.showWebsite
compositeDisposable += model.showActivity
.toObservable()
.subscribe { showWebsite(it) }
.subscribe { showActivity(it) }
compositeDisposable += model.shareData
.toObservable()
.subscribe { shareData(it) }

compositeDisposable += model.requestWriteExternalStoragePermission
.toObservable()
Expand Down Expand Up @@ -132,9 +136,18 @@ class SettingsFragment : Fragment() {
Snackbar.make(containerLayout, messageId, Snackbar.LENGTH_LONG).show()
}

private fun showWebsite(uri: Uri) {
val browserIntent = Intent(Intent.ACTION_VIEW, uri)
startActivity(browserIntent)
private fun showActivity(uri: Uri) {
val intent = Intent(Intent.ACTION_VIEW, uri)
requireActivity().startActivitySafely(intent)
}

private fun shareData(text: String) {
val sharingIntent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, text)
}

startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_with)))
}

private fun requestWriteExternalStoragePermission(requestCode: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class SettingsFragmentModel(
val showExpenseExportMessage = DataEvent<Int>()
val showExpensesDeletionMessage = DataEvent<Int>()

val showWebsite = DataEvent<Uri>()
val showActivity = DataEvent<Uri>()
val shareData = DataEvent<String>()

val requestWriteExternalStoragePermission = DataEvent<Int>()

Expand Down Expand Up @@ -114,6 +115,9 @@ class SettingsFragmentModel(

itemModels += createGeneralHeader(context)
itemModels += createViewSourceCode(context)
itemModels += createShareApp(context)
itemModels += createRateApp(context)
itemModels += createContactMe(context)

return itemModels
}
Expand All @@ -125,7 +129,32 @@ class SettingsFragmentModel(
val title = context.getString(R.string.view_source_code)

return ActionSettingItemModel(title).apply {
click = { showWebsite.next(GITHUB_URI) }
click = { showActivity.next(GITHUB_URI) }
}
}

private fun createShareApp(context: Context): SettingItemModel {
val title = context.getString(R.string.share_app)

return ActionSettingItemModel(title).apply {
click = { shareData.next(GOOGLE_PLAY_URI.toString()) }
}
}

private fun createRateApp(context: Context): SettingItemModel {
val title = context.getString(R.string.rate_app)

return ActionSettingItemModel(title).apply {
click = { showActivity.next(GOOGLE_PLAY_URI) }
}
}

private fun createContactMe(context: Context): SettingItemModel {
val title = context.getString(R.string.contact_me)
val summary = context.getString(R.string.feedback_is_welcome)

return SummaryActionSettingItemModel(title, summary).apply {
click = { showActivity.next(EMAIL_URI) }
}
}

Expand Down Expand Up @@ -213,6 +242,13 @@ class SettingsFragmentModel(

private const val REQUEST_CODE_WRITE_EXTERNAL_STORAGE = 1

private val GITHUB_URI = Uri.parse("https://github.com/Nominalista/Expenses")
private val GITHUB_URI =
Uri.parse("https://github.com/Nominalista/Expenses")

private val GOOGLE_PLAY_URI =
Uri.parse("https://play.google.com/store/apps/details?id=com.nominalista.expenses")

private val EMAIL_URI =
Uri.parse("mailto:[email protected]")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.nominalista.expenses.util.extensions

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import android.view.inputmethod.InputMethodManager

Expand All @@ -28,4 +29,11 @@ private fun Activity.getCurrentFocusOrPlaceholder() = currentFocus ?: View(this)
fun Activity.toggleKeyboard() {
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(0, 0)
}

/**
* Try to start activity if no sure that this activity exits.
*/
fun Activity.startActivitySafely(intent: Intent) {
if (intent.resolveActivity(packageManager) != null) startActivity(intent)
}
5 changes: 5 additions & 0 deletions app/src/main/res/values-pl-rPL/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<string name="column_tags">Tagi</string>
<string name="column_title">Tytuł</string>
<string name="confirm">Potwierdź</string>
<string name="contact_me">Skontaktuj się ze mną</string>
<string name="delete">Usuń</string>
<string name="default_currency">Domyślna waluta</string>
<string name="delete_all">Usuń wszystkie</string>
Expand All @@ -30,6 +31,7 @@
<string name="expense_export_success_message">Pomyślnie wyeksportowano wydatki do folderu Pobrane.</string>
<string name="expenses">Wydatki</string>
<string name="export_to_excel">Eksportuj do Excela (.xls)</string>
<string name="feedback_is_welcome">Opinia mile widziana!</string>
<string name="filter">Filtruj</string>
<string name="filter_tags">Filtruj tagi</string>
<string name="general">Ogólne</string>
Expand All @@ -41,10 +43,13 @@
<string name="no_tags">Brak tagów</string>
<string name="notes_title">Notatki</string>
<string name="notes_hint">Notatki (opcjonalnie)</string>
<string name="rate_app">Oceń aplikację (4.8 &#x1F31F;)</string>
<string name="save">Zapisz</string>
<string name="select_currency">Wybierz walutę</string>
<string name="select_tags">Wybierz tagi</string>
<string name="settings">Ustawienia</string>
<string name="share_app">Podziel się aplikacją</string>
<string name="share_with">Udostępnij przez</string>
<string name="summary_title">Wydałaś/eś</string>
<string name="ok">OK</string>
<string name="this_month">Ten miesiąc</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<string name="column_tags">Tags</string>
<string name="column_title">Title</string>
<string name="confirm">Confirm</string>
<string name="contact_me">Contact me</string>
<string name="currency_abbreviation" translatable="false">%1$s\u0020\u0020%2$s</string>
<string name="delete">Delete</string>
<string name="default_currency">Default currency</string>
Expand All @@ -31,6 +32,7 @@
<string name="expense_export_success_message">Expenses exported successfully into Downloads folder.</string>
<string name="expenses">Expenses</string>
<string name="export_to_excel">Export to Excel (.xls)</string>
<string name="feedback_is_welcome">Feedback is welcome!</string>
<string name="filter">Filter</string>
<string name="filter_tags">Filter tags</string>
<string name="general">General</string>
Expand All @@ -42,10 +44,13 @@
<string name="no_tags">No tags</string>
<string name="notes_title">Notes</string>
<string name="notes_hint">Notes (optional)</string>
<string name="rate_app">Rate the app (4.8 &#x1F31F;)</string>
<string name="save">Save</string>
<string name="select_currency">Select currency</string>
<string name="select_tags">Select tags</string>
<string name="settings">Settings</string>
<string name="share_app">Share app</string>
<string name="share_with">Share with</string>
<string name="summary_title">You spent</string>
<string name="ok">OK</string>
<string name="this_month">This month</string>
Expand Down

0 comments on commit a87ab7e

Please sign in to comment.