Skip to content

Commit

Permalink
feat: allow users to use other badges
Browse files Browse the repository at this point in the history
  • Loading branch information
adityastic committed Jan 7, 2020
1 parent 64350b0 commit 3ed2caa
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import androidx.appcompat.widget.AppCompatSpinner
import androidx.databinding.BindingAdapter
import org.fossasia.badgemagic.viewmodels.SettingsViewModel

@BindingAdapter("createAdapterFrom")
fun setSpinnerAdapter(spinner: AppCompatSpinner, viewModel: SettingsViewModel) {
@BindingAdapter("languageAdapterFrom")
fun languageAdapterFrom(spinner: AppCompatSpinner, viewModel: SettingsViewModel) {
val list: MutableList<String> = viewModel.languageList.get() ?: mutableListOf()
spinner.adapter = ArrayAdapter<String>(spinner.context, android.R.layout.simple_spinner_dropdown_item, list)
spinner.setSelection(viewModel.getSelectedSpinnerLanguage(), false)
Expand All @@ -17,7 +17,22 @@ fun setSpinnerAdapter(spinner: AppCompatSpinner, viewModel: SettingsViewModel) {
}

override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
viewModel.setSelectedSpinnerLangauge(position)
viewModel.setSelectedSpinnerLanguage(position)
}
}
}

@BindingAdapter("badgesAdapterFrom")
fun badgesAdapterFrom(spinner: AppCompatSpinner, viewModel: SettingsViewModel) {
val list: MutableList<String> = viewModel.badgesList.get() ?: mutableListOf()
spinner.adapter = ArrayAdapter<String>(spinner.context, android.R.layout.simple_spinner_dropdown_item, list)
spinner.setSelection(viewModel.getSelectedSpinnerLanguage(), false)
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
}

override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
viewModel.setSelectedSpinnerBadge(position)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import android.content.Context
import android.content.Context.BLUETOOTH_SERVICE
import java.util.LinkedList
import org.fossasia.badgemagic.core.android.log.Timber
import org.fossasia.badgemagic.core.bluetooth.Constants.CHARACTERISTIC_UUID
import org.fossasia.badgemagic.core.bluetooth.Constants.SERVICE_UUID
import org.fossasia.badgemagic.util.BadgeUtils
import org.fossasia.badgemagic.utils.ByteArrayUtils
import org.koin.core.KoinComponent
import org.koin.core.inject

class GattClient {
class GattClient : KoinComponent {

private var bluetoothManager: BluetoothManager? = null
private var bluetoothAdapter: BluetoothAdapter? = null
Expand All @@ -24,6 +25,8 @@ class GattClient {

private val messagesToSend = LinkedList<ByteArray>()

private val badgeUtils: BadgeUtils by inject()

private val gattCallback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
if (newState == BluetoothProfile.STATE_CONNECTED) {
Expand Down Expand Up @@ -66,7 +69,9 @@ class GattClient {
val data = messagesToSend.pop()
Timber.e { "Writing: ${ByteArrayUtils.byteArrayToHexString(data)}" }

val characteristic = bluetoothGatt?.getService(SERVICE_UUID)?.getCharacteristic(CHARACTERISTIC_UUID)
val characteristic = bluetoothGatt
?.getService(badgeUtils.currentDevice.serviceID)
?.getCharacteristic(badgeUtils.currentDevice.characteristicsID)
characteristic?.value = data
if (characteristic != null)
bluetoothGatt?.writeCharacteristic(characteristic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import no.nordicsemi.android.support.v18.scanner.ScanFilter
import no.nordicsemi.android.support.v18.scanner.ScanResult
import no.nordicsemi.android.support.v18.scanner.ScanSettings
import org.fossasia.badgemagic.core.android.log.Timber
import org.fossasia.badgemagic.core.bluetooth.Constants.SERVICE_UUID
import org.fossasia.badgemagic.util.BadgeUtils
import org.koin.core.KoinComponent
import org.koin.core.inject

class ScanHelper {
class ScanHelper : KoinComponent {

companion object {
private const val SCAN_TIMEOUT_MS = 10_000L
Expand All @@ -20,6 +22,8 @@ class ScanHelper {
private var isScanning = false
private var onDeviceFoundCallback: ((BluetoothDevice?) -> Unit)? = null

private val badgeUtils: BadgeUtils by inject()

private val scanner by lazy { BluetoothLeScannerCompat.getScanner() }
private val stopScanHandler = Handler()
private val stopScanRunnable = Runnable {
Expand Down Expand Up @@ -48,7 +52,7 @@ class ScanHelper {
isScanning = true

val filters = listOf(ScanFilter.Builder()
.setServiceUuid(ParcelUuid(SERVICE_UUID))
.setServiceUuid(ParcelUuid(badgeUtils.currentDevice.serviceID))
.build())

val settings = ScanSettings.Builder()
Expand Down
14 changes: 14 additions & 0 deletions android/src/main/java/org/fossasia/badgemagic/data/badge/Badges.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.fossasia.badgemagic.data.badge

import java.util.UUID

enum class Badges(val device: DeviceID) {
LSLED(DeviceID(
UUID.fromString("0000fee0-0000-1000-8000-00805f9b34fb"),
UUID.fromString("0000fee1-0000-1000-8000-00805f9b34fb")
)),
VBLAB(DeviceID(
UUID.fromString("0000fff0-0000-1000-8000-00805f9b34fb"),
UUID.fromString("0000fff1-0000-1000-8000-00805f9b34fb")
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.fossasia.badgemagic.data.badge

import java.util.UUID

data class DeviceID(val serviceID: UUID, val characteristicsID: UUID)
2 changes: 2 additions & 0 deletions android/src/main/java/org/fossasia/badgemagic/di/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.fossasia.badgemagic.di

import org.fossasia.badgemagic.database.ClipArtService
import org.fossasia.badgemagic.database.StorageFilesService
import org.fossasia.badgemagic.util.BadgeUtils
import org.fossasia.badgemagic.util.BluetoothAdapter
import org.fossasia.badgemagic.util.PreferenceUtils
import org.fossasia.badgemagic.util.Resource
Expand Down Expand Up @@ -39,4 +40,5 @@ val utilModules = module {
single { Resource(androidContext()) }
single { StorageUtils(androidContext()) }
single { BluetoothAdapter(androidContext()) }
single { BadgeUtils() }
}
60 changes: 30 additions & 30 deletions android/src/main/java/org/fossasia/badgemagic/ui/DrawerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
if (bundle?.getString("clipart").equals("clipart")) {
val clipart = SavedClipartFragment()
supportFragmentManager.beginTransaction()
.replace(R.id.frag_container, clipart)
.commit()
.replace(R.id.frag_container, clipart)
.commit()
nav_view.setCheckedItem(R.id.saved_cliparts)
} else if (bundle?.getString("badge").equals("badge")) {
val badge = SavedBadgesFragment()
supportFragmentManager.beginTransaction()
.replace(R.id.frag_container, badge)
.commit()
.replace(R.id.frag_container, badge)
.commit()
nav_view.setCheckedItem(R.id.saved_badges)
}
}
Expand All @@ -99,7 +99,7 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
setSupportActionBar(toolbar)

val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()

Expand Down Expand Up @@ -229,19 +229,19 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi

private fun showImportDialog(uri: Uri?) {
AlertDialog.Builder(this)
.setTitle(getString(R.string.import_dialog))
.setMessage("${getString(R.string.import_dialog_message)} ${storageUtils.getFileName(this, uri
?: Uri.EMPTY)}")
.setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int ->
if (!storageUtils.checkIfFilePresent(this, uri)) {
saveImportFile(uri)
} else
showOverrideDialog(uri)
}
.setNegativeButton(android.R.string.cancel) { dialog, _ ->
dialog.cancel()
}
.show()
.setTitle(getString(R.string.import_dialog))
.setMessage("${getString(R.string.import_dialog_message)} ${storageUtils.getFileName(this, uri
?: Uri.EMPTY)}")
.setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int ->
if (!storageUtils.checkIfFilePresent(this, uri)) {
saveImportFile(uri)
} else
showOverrideDialog(uri)
}
.setNegativeButton(android.R.string.cancel) { dialog, _ ->
dialog.cancel()
}
.show()
}

override fun onPause() {
Expand Down Expand Up @@ -269,13 +269,13 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi

private fun switchFragment(fragment: BaseFragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.frag_container, fragment)
.commit()
.replace(R.id.frag_container, fragment)
.commit()
}

private fun checkManifestPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Timber.i { "Coarse permission granted" }
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_PERMISSION_CODE)
Expand Down Expand Up @@ -306,15 +306,15 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi

private fun showOverrideDialog(uri: Uri?) {
AlertDialog.Builder(this)
.setTitle(getString(R.string.save_dialog_already_present))
.setMessage(getString(R.string.save_dialog_already_present_override))
.setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int ->
saveImportFile(uri)
}
.setNegativeButton(android.R.string.cancel) { dialog, _ ->
dialog.cancel()
}
.show()
.setTitle(getString(R.string.save_dialog_already_present))
.setMessage(getString(R.string.save_dialog_already_present_override))
.setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int ->
saveImportFile(uri)
}
.setNegativeButton(android.R.string.cancel) { dialog, _ ->
dialog.cancel()
}
.show()
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class EditBadgeActivity : AppCompatActivity() {
if (it) {
val badgeConfig = SendingUtils.getBadgeFromJSON(viewModel.drawingJSON.get() ?: "{}")
badgeConfig.hexStrings = Converters.convertBitmapToLEDHex(
Converters.convertStringsToLEDHex(draw_layout.getCheckedList()),
false
Converters.convertStringsToLEDHex(draw_layout.getCheckedList()),
false
)
StoreAsync(fileName, badgeConfig, viewModel, storageUtils).execute()
Toast.makeText(this, R.string.saved_edited_badge, Toast.LENGTH_LONG).show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import com.google.android.material.snackbar.Snackbar
import org.fossasia.badgemagic.R
import org.fossasia.badgemagic.databinding.FragmentSettingsBinding
import org.fossasia.badgemagic.ui.base.BaseFragment
import org.fossasia.badgemagic.util.PreferenceUtils
import org.fossasia.badgemagic.viewmodels.SettingsViewModel
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel

class SettingsFragment : BaseFragment() {
Expand All @@ -22,6 +24,7 @@ class SettingsFragment : BaseFragment() {
}

private val viewModel by viewModel<SettingsViewModel>()
private val prefsUtils: PreferenceUtils by inject()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = DataBindingUtil.inflate<FragmentSettingsBinding>(inflater, R.layout.fragment_settings, container, false)
Expand All @@ -42,5 +45,12 @@ class SettingsFragment : BaseFragment() {
}
.show()
})

viewModel.changedBadge.observe(viewLifecycleOwner, Observer {

Snackbar
.make(view, requireContext().getString(R.string.changed_badge) + " ${prefsUtils.selectedBadge}", Snackbar.LENGTH_LONG)
.show()
})
}
}
13 changes: 13 additions & 0 deletions android/src/main/java/org/fossasia/badgemagic/util/BadgeUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.fossasia.badgemagic.util

import org.fossasia.badgemagic.data.badge.Badges
import org.fossasia.badgemagic.data.badge.DeviceID
import org.koin.core.KoinComponent
import org.koin.core.inject

class BadgeUtils : KoinComponent {
private val prefUtils: PreferenceUtils by inject()

val currentDevice: DeviceID
get() = Badges.valueOf(prefUtils.selectedBadge).device
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context.MODE_PRIVATE

const val PREFS_FILENAME = "org.fossasia.badgemagic.prefs"
const val SELECTED_LANGUAGE = "selected_language"
const val SELECTED_BADGE = "selected_badge"

class PreferenceUtils(val context: Context) {

Expand All @@ -15,4 +16,10 @@ class PreferenceUtils(val context: Context) {
set(value) {
getPrefs()?.edit()?.putInt(SELECTED_LANGUAGE, value)?.apply()
}

var selectedBadge: String
get() = getPrefs()?.getString(SELECTED_BADGE, "LSLED") ?: "LSLED"
set(value) {
getPrefs()?.edit()?.putString(SELECTED_BADGE, value)?.apply()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.fossasia.badgemagic.data.Language
import org.fossasia.badgemagic.data.badge.Badges
import org.fossasia.badgemagic.util.PreferenceUtils

class SettingsViewModel(
Expand All @@ -12,16 +13,28 @@ class SettingsViewModel(
var languageList: ObservableField<MutableList<String>> = ObservableField()
var changedLanguage: MutableLiveData<Boolean> = MutableLiveData()

var badgesList: ObservableField<MutableList<String>> = ObservableField()
var changedBadge: MutableLiveData<Boolean> = MutableLiveData()

init {
val mutableList = mutableListOf<String>()
mutableList.addAll(Language.values().map { it.toString() })
languageList.set(mutableList)
val mutableLanguageList = mutableListOf<String>()
mutableLanguageList.addAll(Language.values().map { it.toString() })
languageList.set(mutableLanguageList)

val mutableBadgeList = mutableListOf<String>()
mutableBadgeList.addAll(Badges.values().map { it.toString() })
badgesList.set(mutableBadgeList)
}

fun getSelectedSpinnerLanguage() = preferenceUtils.selectedLanguage

fun setSelectedSpinnerLangauge(position: Int) {
fun setSelectedSpinnerLanguage(position: Int) {
preferenceUtils.selectedLanguage = position
changedLanguage.value = true
}

fun setSelectedSpinnerBadge(position: Int) {
badgesList.get()?.let { preferenceUtils.selectedBadge = it[position] }
changedBadge.value = true
}
}
16 changes: 15 additions & 1 deletion android/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:createAdapterFrom="@{viewModel}" />
app:languageAdapterFrom="@{viewModel}" />

<androidx.appcompat.widget.AppCompatTextView
style="@style/TextAppearance.MaterialComponents.Headline6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/select_badge" />

<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/badge_spinner"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:badgesAdapterFrom="@{viewModel}" />

</LinearLayout>
</ScrollView>
</layout>
3 changes: 2 additions & 1 deletion android/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@
<string name="delete_badge_confirm">徽章已成功刪除</string>
<string name="delete_clipart_confirm">剪貼畫已成功刪除</string>
<string name="rate_app">評價我們</string>

<string name="select_badge">选择徽章</string>
<string name="changed_badge">徽章现在更改为</string>
</resources>
Loading

0 comments on commit 3ed2caa

Please sign in to comment.