Search in sources :

Example 6 with Country

use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country in project RSAndroidApp by RailwayStations.

the class BahnhofsDbAdapter method getAllCountries.

// Getting All Countries
public List<Country> getAllCountries() {
    List<Country> countryList = new ArrayList<Country>();
    // Select All Query without any baseLocationValues (myCurrentLocation)
    String selectQueryCountries = "SELECT " + Constants.DB_JSON_CONSTANTS.KEY_COUNTRYSHORTCODE + ", " + Constants.DB_JSON_CONSTANTS.KEY_COUNTRYNAME + ", " + Constants.DB_JSON_CONSTANTS.KEY_EMAIL + ", " + Constants.DB_JSON_CONSTANTS.KEY_TWITTERTAGS + " FROM " + DATABASE_TABLE_LAENDER;
    Log.d(TAG, selectQueryCountries.toString());
    Cursor cursor = db.rawQuery(selectQueryCountries, null);
    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Country country = createCountryFromCursor(cursor);
            // Adding country to list
            countryList.add(country);
            if (Log.isLoggable(TAG, Log.DEBUG))
                Log.d(TAG, "Country #" + countryList.size() + " " + country);
        } while (cursor.moveToNext());
    }
    cursor.close();
    // return country list
    return countryList;
}
Also used : ArrayList(java.util.ArrayList) Country(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country) Cursor(android.database.Cursor)

Example 7 with Country

use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country in project RSAndroidApp by RailwayStations.

the class HighScoreActivity method onCreate.

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityHighScoreBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    final BaseApplication baseApplication = (BaseApplication) getApplication();
    final String firstSelectedCountry = baseApplication.getCountryCodes().iterator().next();
    final List<Country> countries = baseApplication.getDbAdapter().getAllCountries();
    countries.add(0, new Country(getString(R.string.all_countries), "", null, null, null, null));
    int selectedItem = 0;
    for (final Country country : countries) {
        if (country.getCode().equals(firstSelectedCountry)) {
            selectedItem = countries.indexOf(country);
        }
    }
    final ArrayAdapter<Country> countryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, countries.toArray(new Country[0]));
    binding.countries.setAdapter(countryAdapter);
    binding.countries.setSelection(selectedItem);
    binding.countries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id) {
            loadHighScore(baseApplication, (Country) parent.getSelectedItem());
        }

        @Override
        public void onNothingSelected(final AdapterView<?> parent) {
        }
    });
}
Also used : View(android.view.View) AdapterView(android.widget.AdapterView) SearchView(androidx.appcompat.widget.SearchView) Country(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country) AdapterView(android.widget.AdapterView) ArrayAdapter(android.widget.ArrayAdapter)

Example 8 with Country

use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country in project RSAndroidApp by RailwayStations.

the class HighScoreActivity method loadHighScore.

private void loadHighScore(final BaseApplication baseApplication, final Country selectedCountry) {
    final RSAPIClient rsapi = baseApplication.getRsapiClient();
    final Call<HighScore> highScoreCall = selectedCountry.getCode().isEmpty() ? rsapi.getHighScore() : rsapi.getHighScore(selectedCountry.getCode());
    highScoreCall.enqueue(new Callback<>() {

        @Override
        public void onResponse(@NonNull final Call<HighScore> call, @NonNull final Response<HighScore> response) {
            if (response.isSuccessful()) {
                adapter = new HighScoreAdapter(HighScoreActivity.this, response.body().getItems());
                binding.highscoreList.setAdapter(adapter);
                binding.highscoreList.setOnItemClickListener((adapter, v, position, arg3) -> {
                    final HighScoreItem highScoreItem = (HighScoreItem) adapter.getItemAtPosition(position);
                    final StationFilter stationFilter = baseApplication.getStationFilter();
                    stationFilter.setNickname(highScoreItem.getName());
                    baseApplication.setStationFilter(stationFilter);
                    final Intent intent = new Intent(HighScoreActivity.this, MapsActivity.class);
                    startActivity(intent);
                });
            }
        }

        @Override
        public void onFailure(@NonNull final Call<HighScore> call, @NonNull final Throwable t) {
            Log.e(TAG, "Error loading highscore", t);
            Toast.makeText(getBaseContext(), getString(R.string.error_loading_highscore) + t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
}
Also used : Context(android.content.Context) Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) StationFilter(de.bahnhoefe.deutschlands.bahnhofsfotos.util.StationFilter) Intent(android.content.Intent) Response(retrofit2.Response) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) Country(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country) HighScoreAdapter(de.bahnhoefe.deutschlands.bahnhofsfotos.db.HighScoreAdapter) Toast(android.widget.Toast) Menu(android.view.Menu) View(android.view.View) HighScoreItem(de.bahnhoefe.deutschlands.bahnhofsfotos.model.HighScoreItem) AdapterView(android.widget.AdapterView) Build(android.os.Build) TargetApi(android.annotation.TargetApi) Log(android.util.Log) HighScore(de.bahnhoefe.deutschlands.bahnhofsfotos.model.HighScore) SearchView(androidx.appcompat.widget.SearchView) ArrayAdapter(android.widget.ArrayAdapter) List(java.util.List) Callback(retrofit2.Callback) ActivityHighScoreBinding(de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.ActivityHighScoreBinding) RSAPIClient(de.bahnhoefe.deutschlands.bahnhofsfotos.rsapi.RSAPIClient) SearchManager(android.app.SearchManager) Call(retrofit2.Call) HighScoreItem(de.bahnhoefe.deutschlands.bahnhofsfotos.model.HighScoreItem) HighScore(de.bahnhoefe.deutschlands.bahnhofsfotos.model.HighScore) RSAPIClient(de.bahnhoefe.deutschlands.bahnhofsfotos.rsapi.RSAPIClient) Intent(android.content.Intent) HighScoreAdapter(de.bahnhoefe.deutschlands.bahnhofsfotos.db.HighScoreAdapter) StationFilter(de.bahnhoefe.deutschlands.bahnhofsfotos.util.StationFilter)

Example 9 with Country

use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country in project RSAndroidApp by RailwayStations.

the class DetailsActivity method uploadPhoto.

private void uploadPhoto() {
    final UploadBinding uploadBinding = UploadBinding.inflate(getLayoutInflater());
    uploadBinding.etComment.setText(upload.getComment());
    uploadBinding.spActive.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.active_flag_options)));
    if (station != null) {
        uploadBinding.spActive.setVisibility(View.GONE);
    } else {
        if (upload.getActive() == null) {
            uploadBinding.spActive.setSelection(0);
        } else if (upload.getActive()) {
            uploadBinding.spActive.setSelection(1);
        } else {
            uploadBinding.spActive.setSelection(2);
        }
    }
    final boolean sameChecksum = crc32 != null && crc32.equals(upload.getCrc32());
    uploadBinding.cbChecksum.setVisibility(sameChecksum ? View.VISIBLE : View.GONE);
    if (station != null) {
        uploadBinding.spCountries.setVisibility(View.GONE);
    } else {
        final List<Country> countryList = baseApplication.getDbAdapter().getAllCountries();
        final KeyValueSpinnerItem[] items = new KeyValueSpinnerItem[countryList.size() + 1];
        items[0] = new KeyValueSpinnerItem(getString(R.string.chooseCountry), "");
        int selected = 0;
        for (int i = 0; i < countryList.size(); i++) {
            final Country country = countryList.get(i);
            items[i + 1] = new KeyValueSpinnerItem(country.getName(), country.getCode());
            if (country.getCode().equals(upload.getCountry())) {
                selected = i + 1;
            }
        }
        final ArrayAdapter<KeyValueSpinnerItem> countryAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
        countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        uploadBinding.spCountries.setAdapter(countryAdapter);
        uploadBinding.spCountries.setSelection(selected);
    }
    uploadBinding.txtPanorama.setText(Html.fromHtml(getString(R.string.panorama_info), Html.FROM_HTML_MODE_COMPACT));
    uploadBinding.txtPanorama.setMovementMethod(LinkMovementMethod.getInstance());
    uploadBinding.txtPanorama.setLinkTextColor(Color.parseColor("#c71c4d"));
    String overrideLicense = null;
    if (station != null) {
        final Country country = Country.getCountryByCode(countries, station.getCountry());
        overrideLicense = country.getOverrideLicense();
    }
    if (overrideLicense != null) {
        uploadBinding.cbSpecialLicense.setText(getString(R.string.special_license, overrideLicense));
    }
    uploadBinding.cbSpecialLicense.setVisibility(overrideLicense == null ? View.GONE : View.VISIBLE);
    final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogCustom));
    builder.setTitle(R.string.photo_upload).setView(uploadBinding.getRoot()).setIcon(R.drawable.ic_bullhorn_48px).setPositiveButton(android.R.string.ok, null).setNegativeButton(android.R.string.cancel, (dialog, id1) -> dialog.cancel());
    final AlertDialog alertDialog = builder.create();
    alertDialog.show();
    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
        if (uploadBinding.cbSpecialLicense.getText().length() > 0 && !uploadBinding.cbSpecialLicense.isChecked()) {
            Toast.makeText(this, R.string.special_license_confirm, Toast.LENGTH_LONG).show();
            return;
        }
        if (sameChecksum && !uploadBinding.cbChecksum.isChecked()) {
            Toast.makeText(this, R.string.photo_checksum, Toast.LENGTH_LONG).show();
            return;
        }
        if (station == null) {
            if (uploadBinding.spActive.getSelectedItemPosition() == 0) {
                Toast.makeText(this, R.string.active_flag_choose, Toast.LENGTH_LONG).show();
                return;
            }
            final KeyValueSpinnerItem selectedCountry = (KeyValueSpinnerItem) uploadBinding.spCountries.getSelectedItem();
            upload.setCountry(selectedCountry.getValue());
        }
        alertDialog.dismiss();
        binding.details.progressBar.setVisibility(View.VISIBLE);
        String stationTitle = binding.details.etbahnhofname.getText().toString();
        String comment = uploadBinding.etComment.getText().toString();
        upload.setTitle(stationTitle);
        upload.setComment(comment);
        try {
            stationTitle = URLEncoder.encode(binding.details.etbahnhofname.getText().toString(), String.valueOf(StandardCharsets.UTF_8));
            comment = URLEncoder.encode(comment, String.valueOf(StandardCharsets.UTF_8));
        } catch (final UnsupportedEncodingException e) {
            Log.e(TAG, "Error encoding station title or comment", e);
        }
        upload.setActive(uploadBinding.spActive.getSelectedItemPosition() == 1);
        baseApplication.getDbAdapter().updateUpload(upload);
        final File mediaFile = getStoredMediaFile(upload);
        assert mediaFile != null;
        final RequestBody file = RequestBody.create(mediaFile, MediaType.parse(URLConnection.guessContentTypeFromName(mediaFile.getName())));
        rsapiClient.photoUpload(bahnhofId, station != null ? station.getCountry() : upload.getCountry(), stationTitle, latitude, longitude, comment, upload.getActive(), file).enqueue(new Callback<>() {

            @Override
            public void onResponse(@NonNull final Call<InboxResponse> call, @NonNull final Response<InboxResponse> response) {
                binding.details.progressBar.setVisibility(View.GONE);
                final InboxResponse inboxResponse;
                if (response.isSuccessful()) {
                    inboxResponse = response.body();
                } else if (response.code() == 401) {
                    new SimpleDialogs().confirm(DetailsActivity.this, R.string.authorization_failed);
                    return;
                } else {
                    assert response.errorBody() != null;
                    final Gson gson = new Gson();
                    inboxResponse = gson.fromJson(response.errorBody().charStream(), InboxResponse.class);
                    if (inboxResponse.getState() == null) {
                        inboxResponse.setState(InboxResponse.InboxResponseState.ERROR);
                    }
                }
                assert inboxResponse != null;
                upload.setRemoteId(inboxResponse.getId());
                upload.setInboxUrl(inboxResponse.getInboxUrl());
                upload.setUploadState(inboxResponse.getState().getUploadState());
                upload.setCrc32(inboxResponse.getCrc32());
                baseApplication.getDbAdapter().updateUpload(upload);
                if (inboxResponse.getState() == InboxResponse.InboxResponseState.ERROR) {
                    new SimpleDialogs().confirm(DetailsActivity.this, String.format(getText(InboxResponse.InboxResponseState.ERROR.getMessageId()).toString(), response.message()));
                    // try to get the upload state again
                    fetchUploadStatus(upload);
                } else {
                    new SimpleDialogs().confirm(DetailsActivity.this, inboxResponse.getState().getMessageId());
                }
            }

            @Override
            public void onFailure(@NonNull final Call<InboxResponse> call, @NonNull final Throwable t) {
                Log.e(TAG, "Error uploading photo", t);
                binding.details.progressBar.setVisibility(View.GONE);
                new SimpleDialogs().confirm(DetailsActivity.this, String.format(getText(InboxResponse.InboxResponseState.ERROR.getMessageId()).toString(), t.getMessage()));
                // try to get the upload state again
                fetchUploadStatus(upload);
            }
        });
    });
}
Also used : KeyValueSpinnerItem(de.bahnhoefe.deutschlands.bahnhofsfotos.util.KeyValueSpinnerItem) AlertDialog(androidx.appcompat.app.AlertDialog) TaskStackBuilder(android.app.TaskStackBuilder) Gson(com.google.gson.Gson) UploadBinding(de.bahnhoefe.deutschlands.bahnhofsfotos.databinding.UploadBinding) SimpleDialogs(de.bahnhoefe.deutschlands.bahnhofsfotos.dialogs.SimpleDialogs) RequestBody(okhttp3.RequestBody) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Point(android.graphics.Point) ContextThemeWrapper(android.view.ContextThemeWrapper) Country(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country) InboxResponse(de.bahnhoefe.deutschlands.bahnhofsfotos.model.InboxResponse) File(java.io.File) ArrayAdapter(android.widget.ArrayAdapter)

Example 10 with Country

use of de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country in project RSAndroidApp by RailwayStations.

the class TimetableTest method createTimetableIntentWithDS100.

@Test
public void createTimetableIntentWithDS100() {
    final Country country = new Country();
    country.setTimetableUrlTemplate("https://example.com/{DS100}/blah");
    assertEquals("https://example.com/LOL/blah", new Timetable().createTimetableIntent(country, station).getData().toString());
}
Also used : Country(de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Aggregations

Country (de.bahnhoefe.deutschlands.bahnhofsfotos.model.Country)10 ArrayAdapter (android.widget.ArrayAdapter)3 MediumTest (androidx.test.filters.MediumTest)3 Test (org.junit.Test)3 Cursor (android.database.Cursor)2 View (android.view.View)2 AdapterView (android.widget.AdapterView)2 SearchView (androidx.appcompat.widget.SearchView)2 TargetApi (android.annotation.TargetApi)1 SearchManager (android.app.SearchManager)1 TaskStackBuilder (android.app.TaskStackBuilder)1 ContentValues (android.content.ContentValues)1 Context (android.content.Context)1 Intent (android.content.Intent)1 Point (android.graphics.Point)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1 NonNull (android.support.annotation.NonNull)1 Log (android.util.Log)1 ContextThemeWrapper (android.view.ContextThemeWrapper)1