|
| 1 | +package net.osmtracker.activity; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.pm.ActivityInfo; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.view.View; |
| 7 | +import android.view.View.OnClickListener; |
| 8 | +import android.view.WindowManager; |
| 9 | +import android.widget.Button; |
| 10 | +import android.widget.EditText; |
| 11 | +import android.widget.Toast; |
| 12 | + |
| 13 | +import androidx.annotation.Nullable; |
| 14 | + |
| 15 | +import com.android.volley.AuthFailureError; |
| 16 | +import com.android.volley.Request; |
| 17 | +import com.android.volley.Response; |
| 18 | +import com.android.volley.VolleyError; |
| 19 | +import com.android.volley.toolbox.JsonObjectRequest; |
| 20 | +import com.android.volley.toolbox.Volley; |
| 21 | + |
| 22 | +import net.osmtracker.GitHubUser; |
| 23 | +import net.osmtracker.R; |
| 24 | +import static net.osmtracker.github.GitHubConstants.getRepoForksUrl; |
| 25 | + |
| 26 | +import org.json.JSONException; |
| 27 | +import org.json.JSONObject; |
| 28 | + |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +public class GitHubNewFork extends Activity { |
| 33 | + EditText editTextRootUsername, editTextRootRepo; |
| 34 | + private GitHubUser gitHubUser; |
| 35 | + private String newForkFullName; |
| 36 | + |
| 37 | + @Override |
| 38 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 39 | + super.onCreate(savedInstanceState); |
| 40 | + setContentView(R.layout.git_create_fork); |
| 41 | + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); |
| 42 | + |
| 43 | + gitHubUser = new GitHubUser(this); |
| 44 | + |
| 45 | + editTextRootUsername = findViewById(R.id.git_username_to_fork_editText_user); |
| 46 | + editTextRootRepo = findViewById(R.id.git_repo_to_fork_editText_name); |
| 47 | + |
| 48 | + editTextRootUsername.setHint(R.string.upload_to_github_forked_repo_owner); |
| 49 | + editTextRootRepo.setHint(R.string.upload_to_github_forked_repo_name); |
| 50 | + |
| 51 | + final Button btnCreate = (Button) findViewById(R.id.git_create_newfork_btn_ok); |
| 52 | + btnCreate.setOnClickListener(new OnClickListener() { |
| 53 | + @Override |
| 54 | + public void onClick(View v) { |
| 55 | + String username = editTextRootUsername.getText().toString().trim(); |
| 56 | + String repo = editTextRootRepo.getText().toString().trim(); |
| 57 | + if (username.isEmpty()) { |
| 58 | + editTextRootUsername.setError(getString(R.string.error_field_required)); |
| 59 | + editTextRootUsername.requestFocus(); |
| 60 | + return; |
| 61 | + } |
| 62 | + if (repo.isEmpty()) { |
| 63 | + editTextRootRepo.setError(getString(R.string.error_field_required)); |
| 64 | + editTextRootRepo.requestFocus(); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + createNewFork(username, repo); |
| 69 | + //Toast.makeText(GitHubNewFork.this, R.string.successfully_created, Toast.LENGTH_SHORT).show(); |
| 70 | + //finish(); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + |
| 75 | + final Button btnCancel = (Button) findViewById(R.id.git_back_newfork_btn_cancel); |
| 76 | + btnCancel.setOnClickListener(new OnClickListener() { |
| 77 | + @Override |
| 78 | + public void onClick(View v) { |
| 79 | + finish(); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + // Do not show soft keyboard by default |
| 84 | + getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); |
| 85 | + } |
| 86 | + |
| 87 | + private void createNewFork(String username, String repo) { |
| 88 | + String fullURL = getRepoForksUrl(username, repo); |
| 89 | + |
| 90 | + JsonObjectRequest postResquest= new JsonObjectRequest( |
| 91 | + Request.Method.POST, |
| 92 | + fullURL, |
| 93 | + null, |
| 94 | + new Response.Listener<JSONObject>() { |
| 95 | + @Override |
| 96 | + public void onResponse(JSONObject response) { |
| 97 | + try { |
| 98 | + setNewForkFullName(response.getString("full_name")); |
| 99 | + Toast.makeText(GitHubNewFork.this, R.string.successfully_created, Toast.LENGTH_SHORT).show(); |
| 100 | + finish(); |
| 101 | + } catch (JSONException e) { |
| 102 | + Toast.makeText(GitHubNewFork.this, R.string.error_creating, Toast.LENGTH_SHORT).show(); |
| 103 | + e.printStackTrace(); |
| 104 | + } |
| 105 | + } |
| 106 | + }, new Response.ErrorListener() { |
| 107 | + @Override |
| 108 | + public void onErrorResponse(VolleyError error) { |
| 109 | + Toast.makeText(GitHubNewFork.this, R.string.error_creating, Toast.LENGTH_SHORT).show(); |
| 110 | + } |
| 111 | + }){ |
| 112 | + @Override |
| 113 | + public Map getHeaders() throws AuthFailureError |
| 114 | + { |
| 115 | + HashMap headers = new HashMap(); |
| 116 | + headers.put("Authorization", "Bearer " + gitHubUser.getToken()); |
| 117 | + return headers; |
| 118 | + } |
| 119 | + }; |
| 120 | + Volley.newRequestQueue(this).add(postResquest); |
| 121 | + } |
| 122 | + |
| 123 | + public void setNewForkFullName(String newForkFullName) { |
| 124 | + this.newForkFullName = newForkFullName; |
| 125 | + } |
| 126 | +} |
0 commit comments